Skip to content

Commit e699b6f

Browse files
committed
added possibility to set fontFamily to start and end texts
1 parent 1d0d7fe commit e699b6f

File tree

6 files changed

+86
-47
lines changed

6 files changed

+86
-47
lines changed
306 KB
Binary file not shown.

app/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
app:startTextSize="27sp"
1616
app:startTextStyle="strikethrough"
1717
app:startTextColor="@color/colorAccent"
18-
app:startTextFont="sans-serif-thin"
18+
app:startTextFontFamily="sans-serif-thin"
1919
app:endText="@string/app_name"
20-
app:endTextStyle="bold|underline"
20+
app:endTextStyle="underline"
21+
app:endTextFontFamily="@font/noto_sans_medium"
2122
app:includeTextSpace="true"
2223
app:layout_constraintBottom_toBottomOf="parent"
2324
app:layout_constraintLeft_toLeftOf="parent"

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88

99
}
1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.4.0'
11+
classpath 'com.android.tools.build:gradle:3.4.1'
1212
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1313
// NOTE: Do not place your application dependencies here; they belong
1414
// in the individual module build.gradle files

omegaviwslibs/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ android {
3030
dependencies {
3131
implementation fileTree(dir: 'libs', include: ['*.jar'])
3232
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
33-
implementation 'com.github.Omega-R.OmegaTypes:omegatypes:89dee9ed'
33+
implementation 'com.github.Omega-R.OmegaTypes:omegatypes:d48ca08'
3434

3535
implementation 'androidx.appcompat:appcompat:1.0.2'
3636

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

Lines changed: 79 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package com.omega_r.libs.views
22

3+
import android.annotation.SuppressLint
34
import 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
49
import android.util.AttributeSet
510
import androidx.appcompat.widget.AppCompatTextView
11+
import androidx.appcompat.widget.TintTypedArray
12+
import androidx.core.content.res.ResourcesCompat
613
import 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
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
<attr name="startTextColor" format="color"/>
2626
<attr name="endTextColor" format="color"/>
27-
<attr name="startTextFont" format="string"/>
28-
<attr name="endTextFont" format="string"/>
27+
<attr name="startTextFontFamily" />
28+
<attr name="endTextFontFamily" />
2929

3030
<attr name="includeTextSpace" format="boolean"/>
3131

0 commit comments

Comments
 (0)