@@ -7,11 +7,13 @@ import android.graphics.Typeface
77import android.util.AttributeSet
88import androidx.appcompat.widget.AppCompatTextView
99import androidx.appcompat.widget.TintTypedArray
10+ import androidx.appcompat.widget.TintTypedArray.obtainStyledAttributes
1011import com.omega_r.libs.omegatypes.*
1112
1213/* *
1314 * Created by Anton Knyazev on 18.05.2019.
1415 */
16+ @SuppressLint(" RestrictedApi" )
1517open class OmegaTextView @JvmOverloads constructor(
1618 context : Context , attrs : AttributeSet ? = null , defStyleAttr : Int = android.R .attr.textViewStyle
1719) : AppCompatTextView(context, attrs, defStyleAttr) {
@@ -63,47 +65,48 @@ open class OmegaTextView @JvmOverloads constructor(
6365
6466 init {
6567 initData = false
66- if (attrs != null ) {
67- initWithAttributes(context, attrs, defStyleAttr)
68- }
68+ if (attrs != null ) initWithAttributes(context, attrs, defStyleAttr)
6969 initData = true
7070 updateAllText()
7171 }
7272
73- @SuppressLint(" RestrictedApi" )
7473 private fun initWithAttributes (
7574 context : Context ,
7675 attrs : AttributeSet ? = null,
7776 defStyleAttr : Int = android.R .attr.textViewStyle
7877 ) {
79- val a = TintTypedArray .obtainStyledAttributes(context, attrs, R .styleable.OmegaTextView , defStyleAttr, 0 )
78+ initWithAttributes(obtainStyledAttributes(context, attrs, R .styleable.OmegaTextView , defStyleAttr, 0 ))
79+ }
8080
81+ private fun initWithAttributes (a : TintTypedArray ) {
82+ if (a.hasValue(R .styleable.OmegaTextView_startTextAppearance )) {
83+ updateTextAppearance(
84+ a.getResourceId(R .styleable.OmegaTextView_startTextAppearance , - 1 ),
85+ startTextStyle
86+ )
87+ }
88+ if (a.hasValue(R .styleable.OmegaTextView_middleTextAppearance )) {
89+ updateTextAppearance(
90+ a.getResourceId(R .styleable.OmegaTextView_middleTextAppearance , - 1 ),
91+ middleTextStyle
92+ )
93+ }
94+ if (a.hasValue(R .styleable.OmegaTextView_endTextAppearance )) {
95+ updateTextAppearance(
96+ a.getResourceId(R .styleable.OmegaTextView_endTextAppearance , - 1 ),
97+ endTextStyle
98+ )
99+ }
81100 for (i in 0 until a.indexCount) {
82101 when (val attr = a.getIndex(i)) {
83- R .styleable.OmegaTextView_startText -> {
84- startText = a.getText(attr).toText()
85- }
86- R .styleable.OmegaTextView_endText -> {
87- endText = a.getText(attr).toText()
88- }
89- R .styleable.OmegaTextView_startTextSize -> {
90- startTextStyle.textSize = Size .from(a.getDimension(attr, 0f ), Size .Unit .PX )
91- }
92- R .styleable.OmegaTextView_middleTextSize -> {
93- middleTextStyle.textSize = Size .from(a.getDimension(attr, 0f ), Size .Unit .PX )
94- }
95- R .styleable.OmegaTextView_endTextSize -> {
96- endTextStyle.textSize = Size .from(a.getDimension(attr, 0f ), Size .Unit .PX )
97- }
98- R .styleable.OmegaTextView_startTextStyle -> {
99- startTextStyle.style = a.getInt(attr, STYLE_NONE )
100- }
101- R .styleable.OmegaTextView_middleTextStyle -> {
102- middleTextStyle.style = a.getInt(attr, STYLE_NONE )
103- }
104- R .styleable.OmegaTextView_endTextStyle -> {
105- endTextStyle.style = a.getInt(attr, STYLE_NONE )
106- }
102+ R .styleable.OmegaTextView_startText -> startText = a.getText(attr).toText()
103+ R .styleable.OmegaTextView_endText -> endText = a.getText(attr).toText()
104+ R .styleable.OmegaTextView_startTextSize -> startTextStyle.textSize = a.getSize(attr)
105+ R .styleable.OmegaTextView_middleTextSize -> middleTextStyle.textSize = a.getSize(attr)
106+ R .styleable.OmegaTextView_endTextSize -> endTextStyle.textSize = a.getSize(attr)
107+ R .styleable.OmegaTextView_startTextStyle -> startTextStyle.style = a.getStyle(attr)
108+ R .styleable.OmegaTextView_middleTextStyle -> middleTextStyle.style = a.getStyle(attr)
109+ R .styleable.OmegaTextView_endTextStyle -> endTextStyle.style = a.getStyle(attr)
107110 R .styleable.OmegaTextView_startTextColor -> {
108111 startTextStyle.textColor = a.getColor(attr, startTextStyle.textColor)
109112 }
@@ -113,26 +116,30 @@ open class OmegaTextView @JvmOverloads constructor(
113116 R .styleable.OmegaTextView_endTextColor -> {
114117 endTextStyle.textColor = a.getColor(attr, endTextStyle.textColor)
115118 }
116- R .styleable.OmegaTextView_startTextFontFamily -> {
117- obtainFont(intoStyle = startTextStyle, typedArray = a, attr = attr)
118- }
119- R .styleable.OmegaTextView_middleTextFontFamily -> {
120- obtainFont(intoStyle = middleTextStyle, typedArray = a, attr = attr)
121- }
122- R .styleable.OmegaTextView_endTextFontFamily -> {
123- obtainFont(intoStyle = endTextStyle, typedArray = a, attr = attr)
124- }
119+ R .styleable.OmegaTextView_startTextFontFamily -> obtainFont(startTextStyle, a, attr)
120+ R .styleable.OmegaTextView_middleTextFontFamily -> obtainFont(middleTextStyle, a, attr)
121+ R .styleable.OmegaTextView_endTextFontFamily -> obtainFont(endTextStyle, a, attr)
125122 R .styleable.OmegaTextView_startDelimiter -> {
126- a.getString(attr)?.let {
127- startSpaceText = Text .from(it)
128- }
123+ a.getString(attr)?.let { startSpaceText = Text .from(it) }
129124 }
130125 R .styleable.OmegaTextView_endDelimiter -> {
131- a.getString(attr)?.let {
132- endSpaceText = Text .from(it)
133- }
126+ a.getString(attr)?.let { endSpaceText = Text .from(it) }
134127 }
128+ }
129+ }
130+ a.recycle()
131+ }
135132
133+ @SuppressLint(" PrivateResource" )
134+ private fun updateTextAppearance (resId : Int , style : Style ) {
135+ if (resId == - 1 ) return
136+ val a = obtainStyledAttributes(context, resId, R .styleable.TextAppearance )
137+ for (i in 0 until a.indexCount) {
138+ when (val attr = a.getIndex(i)) {
139+ R .styleable.TextAppearance_android_textColor -> style.textColor = a.getColor(attr, style.textColor)
140+ R .styleable.TextAppearance_android_textSize -> style.textSize = a.getSize(attr)
141+ R .styleable.TextAppearance_android_fontFamily -> obtainFont(style, a, attr)
142+ R .styleable.TextAppearance_android_textStyle -> style.fontTypeface = getFontTypeface(style, a, attr)
136143 }
137144 }
138145 a.recycle()
@@ -141,19 +148,25 @@ open class OmegaTextView @JvmOverloads constructor(
141148 @SuppressLint(" RestrictedApi" )
142149 private fun obtainFont (intoStyle : Style , typedArray : TintTypedArray , attr : Int ) {
143150 if (! context.isRestricted) {
144- try {
145- intoStyle.fontTypeface = typedArray.getFont(attr, intoStyle.style, null )
146- } catch (e: UnsupportedOperationException ) {
147- // Expected if it is not a font resource.
148- } catch (e: Resources .NotFoundException ) {
149- // Expected if it is not a font resource.
150- }
151+ intoStyle.fontTypeface = getFontTypeface(intoStyle, typedArray, attr)
151152 }
152153 if (intoStyle.fontTypeface == null ) {
153154 intoStyle.fontFamily = typedArray.getString(attr)
154155 }
155156 }
156157
158+ private fun getFontTypeface (intoStyle : Style , typedArray : TintTypedArray , attr : Int ): Typeface ? {
159+ return try {
160+ typedArray.getFont(attr, intoStyle.style, null )
161+ } catch (e: UnsupportedOperationException ) {
162+ // Expected if it is not a font resource.
163+ null
164+ } catch (e: Resources .NotFoundException ) {
165+ // Expected if it is not a font resource.
166+ null
167+ }
168+ }
169+
157170 override fun getText (): CharSequence? {
158171 super .getText()
159172 return text?.getCharSequence(context)
@@ -163,6 +176,23 @@ open class OmegaTextView @JvmOverloads constructor(
163176 this .text = text?.toText()
164177 }
165178
179+ override fun setTextAppearance (context : Context , resId : Int ) {
180+ super .setTextAppearance(context, resId)
181+ invalidateTextAppearance(resId)
182+ }
183+
184+ override fun setTextAppearance (resId : Int ) {
185+ super .setTextAppearance(resId)
186+ invalidateTextAppearance(resId)
187+ }
188+
189+ private fun invalidateTextAppearance (resId : Int ) {
190+ initData = false
191+ initWithAttributes(obtainStyledAttributes(context, resId, R .styleable.OmegaTextView ))
192+ initData = true
193+ updateAllText()
194+ }
195+
166196 private fun updateAllText (force : Boolean = false) {
167197 if (initData || force) {
168198 val allText = (startText + startTextStyle) +
@@ -260,4 +290,8 @@ open class OmegaTextView @JvmOverloads constructor(
260290 return this + style.createTextStyle()
261291 }
262292
293+ private fun TintTypedArray.getSize (attr : Int ) = Size .from(getDimension(attr, 0f ), Size .Unit .PX )
294+
295+ private fun TintTypedArray.getStyle (attr : Int ) = getInt(attr, STYLE_NONE )
296+
263297}
0 commit comments