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
@@ -40,7 +47,13 @@ open class OmegaTextView @JvmOverloads constructor(
4047
4148 }
4249
43- var spaceText: Text ? = null
50+ var startSpaceText: Text ? = null
51+ set(value) {
52+ field = value
53+ updateAllText()
54+ }
55+
56+ var endSpaceText: Text ? = null
4457 set(value) {
4558 field = value
4659 updateAllText()
@@ -54,52 +67,78 @@ open class OmegaTextView @JvmOverloads constructor(
5467 init {
5568 initData = false
5669 if (attrs != null ) {
57- val a = context.obtainStyledAttributes(attrs, R .styleable.OmegaTextView , defStyleAttr, 0 )
70+ initWithAttributes(context, attrs, defStyleAttr)
71+ }
72+ initData = true
73+ updateAllText()
74+ }
5875
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)
76+ @SuppressLint(" RestrictedApi" )
77+ 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 )
79+
80+ for (i in 0 until a.indexCount) {
81+ when (val attr = a.getIndex(i)) {
82+ R .styleable.OmegaTextView_startText -> {
83+ startText = a.getText(attr).toText()
84+ }
85+ R .styleable.OmegaTextView_endText -> {
86+ endText = a.getText(attr).toText()
87+ }
88+ R .styleable.OmegaTextView_startTextSize -> {
89+ startTextStyle.textSize = Size .from(a.getDimension(attr, 0f ), Size .Unit .PX )
90+ }
91+ R .styleable.OmegaTextView_endTextSize -> {
92+ endTextStyle.textSize = Size .from(a.getDimension(attr, 0f ), Size .Unit .PX )
93+ }
94+ R .styleable.OmegaTextView_startTextStyle -> {
95+ startTextStyle.style = a.getInt(attr, STYLE_NONE )
96+ }
97+ R .styleable.OmegaTextView_endTextStyle -> {
98+ endTextStyle.style = a.getInt(attr, STYLE_NONE )
99+ }
100+ R .styleable.OmegaTextView_startTextColor -> {
101+ startTextStyle.textColor = a.getColor(attr, startTextStyle.textColor)
102+ }
103+ R .styleable.OmegaTextView_endTextColor -> {
104+ endTextStyle.textColor = a.getColor(attr, endTextStyle.textColor)
105+ }
106+ R .styleable.OmegaTextView_startTextFontFamily -> {
107+ obtainFont(intoStyle = startTextStyle, typedArray = a, attr = attr)
108+ }
109+ R .styleable.OmegaTextView_endTextFontFamily -> {
110+ obtainFont(intoStyle = endTextStyle, typedArray = a, attr = attr)
111+ }
112+ R .styleable.OmegaTextView_startDelimiter -> {
113+ a.getString(attr)?.let {
114+ startSpaceText = Text .from(it)
90115 }
91- R .styleable. OmegaTextView_includeTextSpace -> {
92- if (a.getBoolean(attr, false )) {
93- spaceText = Text .from( " " )
94- }
116+ }
117+ R .styleable. OmegaTextView_endDelimiter -> {
118+ a.getString(attr)?. let {
119+ endSpaceText = Text .from(it)
95120 }
96-
97121 }
122+
98123 }
99- a.recycle()
100124 }
101- initData = true
102- updateAllText()
125+ a.recycle()
126+ }
127+
128+ @SuppressLint(" RestrictedApi" )
129+ private fun obtainFont (intoStyle : Style , typedArray : TintTypedArray , attr : Int ) {
130+ if (! context.isRestricted) {
131+ try {
132+ intoStyle.fontTypeface = typedArray.getFont(attr, intoStyle.style, null )
133+ } catch (e: UnsupportedOperationException ) {
134+ // Expected if it is not a font resource.
135+ } catch (e: Resources .NotFoundException ) {
136+ // Expected if it is not a font resource.
137+ }
138+ }
139+ if (intoStyle.fontTypeface == null ) {
140+ intoStyle.fontFamily = typedArray.getString(attr)
141+ }
103142 }
104143
105144 override fun getText (): CharSequence? {
@@ -113,7 +152,7 @@ open class OmegaTextView @JvmOverloads constructor(
113152
114153 private fun updateAllText (force : Boolean = false) {
115154 if (initData || force) {
116- val allText = (startText + startTextStyle) + spaceText + text + spaceText + (endText + endTextStyle)
155+ val allText = (startText + startTextStyle) + startSpaceText + text + endSpaceText + (endText + endTextStyle)
117156 super .setText(allText?.getCharSequence(context), BufferType .NORMAL )
118157 }
119158 }
@@ -144,6 +183,12 @@ open class OmegaTextView @JvmOverloads constructor(
144183 updateAllText()
145184 }
146185
186+ var fontTypeface: Typeface ? = null
187+ set(value) {
188+ field = value
189+ updateAllText()
190+ }
191+
147192 fun createTextStyle (): TextStyle ? {
148193 var result: TextStyle ? = null
149194 textSize?.let {
@@ -169,6 +214,10 @@ open class OmegaTextView @JvmOverloads constructor(
169214 result + = TextStyle .color(textColor)
170215 }
171216
217+ fontTypeface?.let {
218+ result + = TextStyle .font(it)
219+ }
220+
172221 fontFamily?.let {
173222 result + = TextStyle .font(it)
174223 }
0 commit comments