From 52b48ce6efaeb9d05f046e507b18f15c68760f66 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Wed, 27 Jun 2018 14:56:45 +0300 Subject: [PATCH] Add the ability to use custom fonts with TextViewEx --- OsmAnd-telegram/.gitignore | 1 + OsmAnd-telegram/build.gradle | 11 ++++- OsmAnd-telegram/res/values/attrs.xml | 4 ++ OsmAnd-telegram/res/values/fonts.xml | 5 ++ .../net/osmand/telegram/helpers/FontCache.kt | 45 +++++++++++++++++ .../osmand/telegram/ui/views/TextViewEx.kt | 49 +++++++++++++++++++ 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 OsmAnd-telegram/res/values/fonts.xml create mode 100644 OsmAnd-telegram/src/net/osmand/telegram/helpers/FontCache.kt create mode 100644 OsmAnd-telegram/src/net/osmand/telegram/ui/views/TextViewEx.kt diff --git a/OsmAnd-telegram/.gitignore b/OsmAnd-telegram/.gitignore index 7ef6f23e16..540de9ded1 100644 --- a/OsmAnd-telegram/.gitignore +++ b/OsmAnd-telegram/.gitignore @@ -12,6 +12,7 @@ obj/ out/ use/ src/org/drinkless/td/ +assets/fonts/* # Android Studio /.idea diff --git a/OsmAnd-telegram/build.gradle b/OsmAnd-telegram/build.gradle index 3355892eb1..fdc07cacb8 100644 --- a/OsmAnd-telegram/build.gradle +++ b/OsmAnd-telegram/build.gradle @@ -111,10 +111,19 @@ task downloadPrebuiltTelegram { dependsOn downloadTdLibzip, copyNativeLibs, copyJavaSources } +task collectFonts(type: Copy) { + from "../../resources/fonts" + into "assets/fonts" + include "*.ttf" +} + +task collectExternalResources { + dependsOn collectFonts +} afterEvaluate { android.applicationVariants.all { variant -> - variant.javaCompiler.dependsOn(downloadPrebuiltTelegram) + variant.javaCompiler.dependsOn(downloadPrebuiltTelegram, collectExternalResources) variant.preBuild.dependsOn(downloadPrebuiltTelegram) } } diff --git a/OsmAnd-telegram/res/values/attrs.xml b/OsmAnd-telegram/res/values/attrs.xml index f4c7b2187d..9726e41f4c 100644 --- a/OsmAnd-telegram/res/values/attrs.xml +++ b/OsmAnd-telegram/res/values/attrs.xml @@ -12,4 +12,8 @@ + + + + \ No newline at end of file diff --git a/OsmAnd-telegram/res/values/fonts.xml b/OsmAnd-telegram/res/values/fonts.xml new file mode 100644 index 0000000000..b6a1c3d72c --- /dev/null +++ b/OsmAnd-telegram/res/values/fonts.xml @@ -0,0 +1,5 @@ + + + fonts/Roboto-Regular.ttf + fonts/Roboto-Medium.ttf + diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/FontCache.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/FontCache.kt new file mode 100644 index 0000000000..4b17006ab6 --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/FontCache.kt @@ -0,0 +1,45 @@ +package net.osmand.telegram.helpers + +import android.content.Context +import android.graphics.Typeface +import android.util.Log +import java.util.concurrent.ConcurrentHashMap + +private const val TAG = "FontCache" +private const val ROBOTO_MEDIUM = "fonts/Roboto-Medium.ttf" +private const val ROBOTO_REGULAR = "fonts/Roboto-Regular.ttf" + +object FontCache { + + private val fontsMap = ConcurrentHashMap() + + fun getRobotoMedium(context: Context): Typeface? { + return getFont(context, ROBOTO_MEDIUM) + } + + fun getRobotoRegular(context: Context): Typeface? { + return getFont(context, ROBOTO_REGULAR) + } + + fun getFont(context: Context, fontName: String): Typeface? { + var typeface: Typeface? = fontsMap[fontName] + if (typeface != null) { + return typeface + } + + try { + typeface = Typeface.createFromAsset(context.assets, fontName) + } catch (e: Exception) { + Log.e(TAG, "Failed to create typeface from asset '$fontName'", e) + return null + } + + if (typeface == null) { + return null + } + + fontsMap[fontName] = typeface + + return typeface + } +} diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/views/TextViewEx.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/views/TextViewEx.kt new file mode 100644 index 0000000000..6d4998208b --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/views/TextViewEx.kt @@ -0,0 +1,49 @@ +package net.osmand.telegram.ui.views + +import android.content.Context +import android.content.res.TypedArray +import android.support.v7.widget.AppCompatTextView +import android.util.AttributeSet +import net.osmand.telegram.R +import net.osmand.telegram.helpers.FontCache + +class TextViewEx : AppCompatTextView { + + constructor(context: Context) : super(context) + + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { + parseAttrs(attrs, 0) + } + + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( + context, + attrs, + defStyleAttr + ) { + parseAttrs(attrs, defStyleAttr) + } + + private fun parseAttrs(attrs: AttributeSet?, defStyleAttr: Int) { + if (attrs == null) { + return + } + + val resolvedAttrs = + context.theme.obtainStyledAttributes(attrs, R.styleable.TextViewEx, defStyleAttr, 0) + applyAttrTypeface(resolvedAttrs) + resolvedAttrs.recycle() + } + + private fun applyAttrTypeface(resolvedAttrs: TypedArray) { + if (isInEditMode || !resolvedAttrs.hasValue(R.styleable.TextViewEx_typeface)) { + return + } + + val typefaceName = resolvedAttrs.getString(R.styleable.TextViewEx_typeface) + val typeface = FontCache.getFont(context, typefaceName) + if (typeface != null) { + val style = getTypeface()?.style ?: 0 + setTypeface(typeface, style) + } + } +}