Skip to content

Commit f098d11

Browse files
author
roman_tcaregorodtcev
committed
textAppearance attrs added
1 parent 90f79bd commit f098d11

File tree

2 files changed

+74
-50
lines changed

2 files changed

+74
-50
lines changed

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

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -70,37 +70,43 @@ open class OmegaTextView @JvmOverloads constructor(
7070
updateAllText()
7171
}
7272

73-
private fun initWithAttributes(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = android.R.attr.textViewStyle) {
73+
private fun initWithAttributes(
74+
context: Context,
75+
attrs: AttributeSet? = null,
76+
defStyleAttr: Int = android.R.attr.textViewStyle
77+
) {
7478
initWithAttributes(obtainStyledAttributes(context, attrs, R.styleable.OmegaTextView, defStyleAttr, 0))
7579
}
7680

7781
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+
}
78100
for (i in 0 until a.indexCount) {
79101
when (val attr = a.getIndex(i)) {
80-
R.styleable.OmegaTextView_startText -> {
81-
startText = a.getText(attr).toText()
82-
}
83-
R.styleable.OmegaTextView_endText -> {
84-
endText = a.getText(attr).toText()
85-
}
86-
R.styleable.OmegaTextView_startTextSize -> {
87-
startTextStyle.textSize = Size.from(a.getDimension(attr, 0f), Size.Unit.PX)
88-
}
89-
R.styleable.OmegaTextView_middleTextSize -> {
90-
middleTextStyle.textSize = Size.from(a.getDimension(attr, 0f), Size.Unit.PX)
91-
}
92-
R.styleable.OmegaTextView_endTextSize -> {
93-
endTextStyle.textSize = Size.from(a.getDimension(attr, 0f), Size.Unit.PX)
94-
}
95-
R.styleable.OmegaTextView_startTextStyle -> {
96-
startTextStyle.style = a.getInt(attr, STYLE_NONE)
97-
}
98-
R.styleable.OmegaTextView_middleTextStyle -> {
99-
middleTextStyle.style = a.getInt(attr, STYLE_NONE)
100-
}
101-
R.styleable.OmegaTextView_endTextStyle -> {
102-
endTextStyle.style = a.getInt(attr, STYLE_NONE)
103-
}
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)
104110
R.styleable.OmegaTextView_startTextColor -> {
105111
startTextStyle.textColor = a.getColor(attr, startTextStyle.textColor)
106112
}
@@ -110,26 +116,30 @@ open class OmegaTextView @JvmOverloads constructor(
110116
R.styleable.OmegaTextView_endTextColor -> {
111117
endTextStyle.textColor = a.getColor(attr, endTextStyle.textColor)
112118
}
113-
R.styleable.OmegaTextView_startTextFontFamily -> {
114-
obtainFont(intoStyle = startTextStyle, typedArray = a, attr = attr)
115-
}
116-
R.styleable.OmegaTextView_middleTextFontFamily -> {
117-
obtainFont(intoStyle = middleTextStyle, typedArray = a, attr = attr)
118-
}
119-
R.styleable.OmegaTextView_endTextFontFamily -> {
120-
obtainFont(intoStyle = endTextStyle, typedArray = a, attr = attr)
121-
}
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)
122122
R.styleable.OmegaTextView_startDelimiter -> {
123-
a.getString(attr)?.let {
124-
startSpaceText = Text.from(it)
125-
}
123+
a.getString(attr)?.let { startSpaceText = Text.from(it) }
126124
}
127125
R.styleable.OmegaTextView_endDelimiter -> {
128-
a.getString(attr)?.let {
129-
endSpaceText = Text.from(it)
130-
}
126+
a.getString(attr)?.let { endSpaceText = Text.from(it) }
131127
}
128+
}
129+
}
130+
a.recycle()
131+
}
132132

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)
133143
}
134144
}
135145
a.recycle()
@@ -138,19 +148,25 @@ open class OmegaTextView @JvmOverloads constructor(
138148
@SuppressLint("RestrictedApi")
139149
private fun obtainFont(intoStyle: Style, typedArray: TintTypedArray, attr: Int) {
140150
if (!context.isRestricted) {
141-
try {
142-
intoStyle.fontTypeface = typedArray.getFont(attr, intoStyle.style, null)
143-
} catch (e: UnsupportedOperationException) {
144-
// Expected if it is not a font resource.
145-
} catch (e: Resources.NotFoundException) {
146-
// Expected if it is not a font resource.
147-
}
151+
intoStyle.fontTypeface = getFontTypeface(intoStyle, typedArray, attr)
148152
}
149153
if (intoStyle.fontTypeface == null) {
150154
intoStyle.fontFamily = typedArray.getString(attr)
151155
}
152156
}
153157

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+
154170
override fun getText(): CharSequence? {
155171
super.getText()
156172
return text?.getCharSequence(context)
@@ -162,15 +178,15 @@ open class OmegaTextView @JvmOverloads constructor(
162178

163179
override fun setTextAppearance(context: Context, resId: Int) {
164180
super.setTextAppearance(context, resId)
165-
updateTextAppearance(resId)
181+
invalidateTextAppearance(resId)
166182
}
167183

168184
override fun setTextAppearance(resId: Int) {
169185
super.setTextAppearance(resId)
170-
updateTextAppearance(resId)
186+
invalidateTextAppearance(resId)
171187
}
172188

173-
private fun updateTextAppearance(resId: Int) {
189+
private fun invalidateTextAppearance(resId: Int) {
174190
initData = false
175191
initWithAttributes(obtainStyledAttributes(context, resId, R.styleable.OmegaTextView))
176192
initData = true
@@ -274,4 +290,8 @@ open class OmegaTextView @JvmOverloads constructor(
274290
return this + style.createTextStyle()
275291
}
276292

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+
277297
}

omegaviwslibs/src/main/res/values/attrs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
<attr name="startDelimiter" format="string"/>
4040
<attr name="endDelimiter" format="string"/>
4141

42+
<attr name="startTextAppearance" format="reference"/>
43+
<attr name="middleTextAppearance" format="reference"/>
44+
<attr name="endTextAppearance" format="reference"/>
45+
4246
</declare-styleable>
4347

4448
</resources>

0 commit comments

Comments
 (0)