11package com.omega_r.libs.views
22
3+ import android.annotation.SuppressLint
34import android.content.Context
5+ import android.content.res.Resources
6+ import android.content.res.TypedArray
7+ import android.graphics.Typeface
8+ import android.os.Build
49import android.util.AttributeSet
510import androidx.appcompat.widget.AppCompatTextView
11+ import androidx.appcompat.widget.TintTypedArray
12+ import androidx.core.content.res.ResourcesCompat
613import com.omega_r.libs.omegatypes.*
714
815
@@ -54,52 +61,73 @@ open class OmegaTextView @JvmOverloads constructor(
5461 init {
5562 initData = false
5663 if (attrs != null ) {
57- val a = context.obtainStyledAttributes(attrs, R .styleable.OmegaTextView , defStyleAttr, 0 )
64+ initWithAttributes(context, attrs, defStyleAttr)
65+ }
66+ initData = true
67+ updateAllText()
68+ }
5869
59- for (i in 0 until a.indexCount) {
60- when (val attr = a.getIndex(i)) {
61- R .styleable.OmegaTextView_startText -> {
62- startText = a.getText(attr).toText()
63- }
64- R .styleable.OmegaTextView_endText -> {
65- endText = a.getText(attr).toText()
66- }
67- R .styleable.OmegaTextView_startTextSize -> {
68- startTextStyle.textSize = Size .from(a.getDimension(attr, 0f ), Size .Unit .PX )
69- }
70- R .styleable.OmegaTextView_endTextSize -> {
71- endTextStyle.textSize = Size .from(a.getDimension(attr, 0f ), Size .Unit .PX )
72- }
73- R .styleable.OmegaTextView_startTextStyle -> {
74- startTextStyle.style = a.getInt(attr, STYLE_NONE )
75- }
76- R .styleable.OmegaTextView_endTextStyle -> {
77- endTextStyle.style = a.getInt(attr, STYLE_NONE )
78- }
79- R .styleable.OmegaTextView_startTextColor -> {
80- startTextStyle.textColor = a.getColor(attr, startTextStyle.textColor)
81- }
82- R .styleable.OmegaTextView_endTextColor -> {
83- endTextStyle.textColor = a.getColor(attr, endTextStyle.textColor)
84- }
85- R .styleable.OmegaTextView_startTextFont -> {
86- startTextStyle.fontFamily = a.getString(attr)
87- }
88- R .styleable.OmegaTextView_endTextFont -> {
89- endTextStyle.fontFamily = a.getString(attr)
90- }
91- R .styleable.OmegaTextView_includeTextSpace -> {
92- if (a.getBoolean(attr, false )) {
93- spaceText = Text .from(" " )
94- }
95- }
70+ @SuppressLint(" RestrictedApi" )
71+ private fun initWithAttributes (context : Context , attrs : AttributeSet ? = null, defStyleAttr : Int = android.R .attr.textViewStyle) {
72+ val a = TintTypedArray .obtainStyledAttributes(context, attrs, R .styleable.OmegaTextView , defStyleAttr, 0 )
9673
74+ for (i in 0 until a.indexCount) {
75+ when (val attr = a.getIndex(i)) {
76+ R .styleable.OmegaTextView_startText -> {
77+ startText = a.getText(attr).toText()
78+ }
79+ R .styleable.OmegaTextView_endText -> {
80+ endText = a.getText(attr).toText()
81+ }
82+ R .styleable.OmegaTextView_startTextSize -> {
83+ startTextStyle.textSize = Size .from(a.getDimension(attr, 0f ), Size .Unit .PX )
84+ }
85+ R .styleable.OmegaTextView_endTextSize -> {
86+ endTextStyle.textSize = Size .from(a.getDimension(attr, 0f ), Size .Unit .PX )
87+ }
88+ R .styleable.OmegaTextView_startTextStyle -> {
89+ startTextStyle.style = a.getInt(attr, STYLE_NONE )
90+ }
91+ R .styleable.OmegaTextView_endTextStyle -> {
92+ endTextStyle.style = a.getInt(attr, STYLE_NONE )
93+ }
94+ R .styleable.OmegaTextView_startTextColor -> {
95+ startTextStyle.textColor = a.getColor(attr, startTextStyle.textColor)
9796 }
97+ R .styleable.OmegaTextView_endTextColor -> {
98+ endTextStyle.textColor = a.getColor(attr, endTextStyle.textColor)
99+ }
100+ R .styleable.OmegaTextView_startTextFontFamily -> {
101+ obtainFont(intoStyle = startTextStyle, typedArray = a, attr = attr)
102+ }
103+ R .styleable.OmegaTextView_endTextFontFamily -> {
104+ obtainFont(intoStyle = endTextStyle, typedArray = a, attr = attr)
105+ }
106+ R .styleable.OmegaTextView_includeTextSpace -> {
107+ if (a.getBoolean(attr, false )) {
108+ spaceText = Text .from(" " )
109+ }
110+ }
111+
98112 }
99- a.recycle()
100113 }
101- initData = true
102- updateAllText()
114+ a.recycle()
115+ }
116+
117+ @SuppressLint(" RestrictedApi" )
118+ private fun obtainFont (intoStyle : Style , typedArray : TintTypedArray , attr : Int ) {
119+ if (! context.isRestricted) {
120+ try {
121+ intoStyle.fontTypeface = typedArray.getFont(attr, intoStyle.style, null )
122+ } catch (e: UnsupportedOperationException ) {
123+ // Expected if it is not a font resource.
124+ } catch (e: Resources .NotFoundException ) {
125+ // Expected if it is not a font resource.
126+ }
127+ }
128+ if (intoStyle.fontTypeface == null ) {
129+ intoStyle.fontFamily = typedArray.getString(attr)
130+ }
103131 }
104132
105133 override fun getText (): CharSequence? {
@@ -144,6 +172,12 @@ open class OmegaTextView @JvmOverloads constructor(
144172 updateAllText()
145173 }
146174
175+ var fontTypeface: Typeface ? = null
176+ set(value) {
177+ field = value
178+ updateAllText()
179+ }
180+
147181 fun createTextStyle (): TextStyle ? {
148182 var result: TextStyle ? = null
149183 textSize?.let {
@@ -169,6 +203,10 @@ open class OmegaTextView @JvmOverloads constructor(
169203 result + = TextStyle .color(textColor)
170204 }
171205
206+ fontTypeface?.let {
207+ result + = TextStyle .font(it)
208+ }
209+
172210 fontFamily?.let {
173211 result + = TextStyle .font(it)
174212 }
0 commit comments