Add the ability to use custom fonts with TextViewEx
This commit is contained in:
parent
56ceb6a666
commit
52b48ce6ef
6 changed files with 114 additions and 1 deletions
1
OsmAnd-telegram/.gitignore
vendored
1
OsmAnd-telegram/.gitignore
vendored
|
@ -12,6 +12,7 @@ obj/
|
||||||
out/
|
out/
|
||||||
use/
|
use/
|
||||||
src/org/drinkless/td/
|
src/org/drinkless/td/
|
||||||
|
assets/fonts/*
|
||||||
|
|
||||||
# Android Studio
|
# Android Studio
|
||||||
/.idea
|
/.idea
|
||||||
|
|
|
@ -111,10 +111,19 @@ task downloadPrebuiltTelegram {
|
||||||
dependsOn downloadTdLibzip, copyNativeLibs, copyJavaSources
|
dependsOn downloadTdLibzip, copyNativeLibs, copyJavaSources
|
||||||
}
|
}
|
||||||
|
|
||||||
|
task collectFonts(type: Copy) {
|
||||||
|
from "../../resources/fonts"
|
||||||
|
into "assets/fonts"
|
||||||
|
include "*.ttf"
|
||||||
|
}
|
||||||
|
|
||||||
|
task collectExternalResources {
|
||||||
|
dependsOn collectFonts
|
||||||
|
}
|
||||||
|
|
||||||
afterEvaluate {
|
afterEvaluate {
|
||||||
android.applicationVariants.all { variant ->
|
android.applicationVariants.all { variant ->
|
||||||
variant.javaCompiler.dependsOn(downloadPrebuiltTelegram)
|
variant.javaCompiler.dependsOn(downloadPrebuiltTelegram, collectExternalResources)
|
||||||
variant.preBuild.dependsOn(downloadPrebuiltTelegram)
|
variant.preBuild.dependsOn(downloadPrebuiltTelegram)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,4 +12,8 @@
|
||||||
<attr name="primary_btn_text_color" format="reference" />
|
<attr name="primary_btn_text_color" format="reference" />
|
||||||
<attr name="secondary_btn_bg" format="reference" />
|
<attr name="secondary_btn_bg" format="reference" />
|
||||||
</declare-styleable>
|
</declare-styleable>
|
||||||
|
|
||||||
|
<declare-styleable name="TextViewEx">
|
||||||
|
<attr name="typeface" format="reference|string" />
|
||||||
|
</declare-styleable>
|
||||||
</resources>
|
</resources>
|
5
OsmAnd-telegram/res/values/fonts.xml
Normal file
5
OsmAnd-telegram/res/values/fonts.xml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<resources>
|
||||||
|
<string name="font_roboto_regular">fonts/Roboto-Regular.ttf</string>
|
||||||
|
<string name="font_roboto_medium">fonts/Roboto-Medium.ttf</string>
|
||||||
|
</resources>
|
45
OsmAnd-telegram/src/net/osmand/telegram/helpers/FontCache.kt
Normal file
45
OsmAnd-telegram/src/net/osmand/telegram/helpers/FontCache.kt
Normal file
|
@ -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<String, Typeface>()
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue