Skip to content

Commit 8132b77

Browse files
Merge pull request #3 from Omega-R/develop
Add font from resources for OmegaTextView
2 parents 1d0d7fe + 726d517 commit 8132b77

File tree

6 files changed

+102
-50
lines changed

6 files changed

+102
-50
lines changed
306 KB
Binary file not shown.

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
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"
21-
app:includeTextSpace="true"
20+
app:endTextStyle="underline"
21+
app:endTextFontFamily="@font/noto_sans_medium"
22+
app:startDelimiter=" "
23+
app:endDelimiter="\n"
2224
app:layout_constraintBottom_toBottomOf="parent"
2325
app:layout_constraintLeft_toLeftOf="parent"
2426
app:layout_constraintRight_toRightOf="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:1.0.0'
3434

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

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

Lines changed: 91 additions & 42 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

@@ -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
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
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

30-
<attr name="includeTextSpace" format="boolean"/>
30+
<attr name="startDelimiter" format="string" />
31+
<attr name="endDelimiter" format="string" />
3132

3233
</declare-styleable>
3334

0 commit comments

Comments
 (0)