-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathRSystemFontEngine.kt
104 lines (84 loc) · 3.14 KB
/
RSystemFontEngine.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package pyxis.uzuki.live.richutilskt.utils
import android.app.Application
import android.content.Context
import android.graphics.Paint
import android.graphics.Typeface
import android.support.annotation.LayoutRes
import android.text.Spannable
import android.text.SpannableString
import android.text.TextPaint
import android.text.style.MetricAffectingSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
internal class RSystemFontEngine private constructor(private val mApplication: Application) {
internal var use = false
val refresh: RSystemFontEngine?
@Synchronized get() = sHelper
private fun <V : TextView> setTypefaceTextView(view: V) {
view.typeface = Typeface.DEFAULT
}
private fun <V : ViewGroup> setTypefaceViewGroup(viewGroup: V) {
val count = viewGroup.childCount
for (i in 0 until count) {
val child = viewGroup.getChildAt(i)
if (child is ViewGroup) {
setTypefaceViewGroup(child)
continue
}
if (child !is TextView) {
continue
}
setTypefaceTextView(child)
}
}
@JvmOverloads fun setTypeface(context: Context, @LayoutRes layoutRes: Int, parent: ViewGroup? = null, isAttachedRoot: Boolean = false): View {
val view = LayoutInflater.from(context).inflate(layoutRes, parent, isAttachedRoot) as ViewGroup
setTypefaceViewGroup(view)
return view
}
@Suppress("UNUSED_PARAMETER")
fun setTypeface(context: Context, view: ViewGroup, parent: ViewGroup? = null, isAttachedRoot: Boolean = false): View {
setTypefaceViewGroup(view)
return view
}
private val typefaceSpan: TypefaceSpan
get() = TypefaceSpan(typeface)
fun String.getStringTypeface(): SpannableString {
val s = SpannableString(this)
s.setSpan(typefaceSpan, 0, s.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
return s
}
val typeface: Typeface
get() = Typeface.DEFAULT
/**
* Style a [Spannable] with a custom [Typeface].
* @author Tristan Waddington
*/
inner class TypefaceSpan(private val mTypeface: Typeface) : MetricAffectingSpan() {
override fun updateMeasureState(p: TextPaint) {
p.typeface = mTypeface
p.flags = p.flags or Paint.FILTER_BITMAP_FLAG
}
override fun updateDrawState(tp: TextPaint) {
tp.typeface = mTypeface
tp.flags = tp.flags or Paint.FILTER_BITMAP_FLAG
}
}
companion object {
val TAG = "RSystemFontEngine"
private var sHelper: RSystemFontEngine? = null
@JvmStatic fun initialize(application: Application) {
sHelper = RSystemFontEngine(application)
}
@JvmStatic fun destroy() = sHelper?.let { sHelper = null }
@JvmStatic val instance: RSystemFontEngine?
get() {
if (sHelper == null) {
throw NullPointerException("Are you put initialize in your application class?")
}
return sHelper
}
}
}