Skip to content

Commit 2959452

Browse files
author
roman_tcaregorodtcev
committed
setTextAppearance method overridden.
1 parent 7849f68 commit 2959452

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

omegaviwslibs/src/main/java/com/omega_r/libs/views/OmegaTextView.kt

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ package com.omega_r.libs.views
33
import android.annotation.SuppressLint
44
import android.content.Context
55
import android.content.res.Resources
6-
import android.content.res.TypedArray
76
import android.graphics.Typeface
8-
import android.os.Build
97
import android.util.AttributeSet
108
import androidx.appcompat.widget.AppCompatTextView
119
import androidx.appcompat.widget.TintTypedArray
12-
import androidx.core.content.res.ResourcesCompat
10+
import androidx.appcompat.widget.TintTypedArray.obtainStyledAttributes
1311
import com.omega_r.libs.omegatypes.*
1412

15-
1613
/**
1714
* Created by Anton Knyazev on 18.05.2019.
1815
*/
16+
@SuppressLint("RestrictedApi")
1917
open class OmegaTextView @JvmOverloads constructor(
2018
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = android.R.attr.textViewStyle
2119
) : AppCompatTextView(context, attrs, defStyleAttr) {
@@ -66,17 +64,16 @@ open class OmegaTextView @JvmOverloads constructor(
6664

6765
init {
6866
initData = false
69-
if (attrs != null) {
70-
initWithAttributes(context, attrs, defStyleAttr)
71-
}
67+
if (attrs != null) initWithAttributes(context, attrs, defStyleAttr)
7268
initData = true
7369
updateAllText()
7470
}
7571

76-
@SuppressLint("RestrictedApi")
7772
private fun initWithAttributes(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = android.R.attr.textViewStyle) {
78-
val a = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.OmegaTextView, defStyleAttr, 0)
73+
initWithAttributes(obtainStyledAttributes(context, attrs, R.styleable.OmegaTextView, defStyleAttr, 0))
74+
}
7975

76+
private fun initWithAttributes(a: TintTypedArray) {
8077
for (i in 0 until a.indexCount) {
8178
when (val attr = a.getIndex(i)) {
8279
R.styleable.OmegaTextView_startText -> {
@@ -150,6 +147,23 @@ open class OmegaTextView @JvmOverloads constructor(
150147
this.text = text?.toText()
151148
}
152149

150+
override fun setTextAppearance(context: Context, resId: Int) {
151+
super.setTextAppearance(context, resId)
152+
updateTextAppearance(resId)
153+
}
154+
155+
override fun setTextAppearance(resId: Int) {
156+
super.setTextAppearance(resId)
157+
updateTextAppearance(resId)
158+
}
159+
160+
private fun updateTextAppearance(resId: Int) {
161+
initData = false
162+
initWithAttributes(obtainStyledAttributes(context, resId, R.styleable.OmegaTextView))
163+
initData = true
164+
updateAllText()
165+
}
166+
153167
private fun updateAllText(force: Boolean = false) {
154168
if (initData || force) {
155169
val allText = (startText + startTextStyle) + startSpaceText + text + endSpaceText + (endText + endTextStyle)

0 commit comments

Comments
 (0)