Skip to content

Commit ab5c43e

Browse files
committed
added span from custom font
1 parent 5ece050 commit ab5c43e

File tree

9 files changed

+36
-41
lines changed

9 files changed

+36
-41
lines changed
-4 Bytes
Binary file not shown.

.idea/codeStyles/Project.xml

Lines changed: 0 additions & 29 deletions
This file was deleted.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/omega_r/com/omegatypesexample/MainActivity.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.graphics.BitmapFactory
55
import android.os.Bundle
66
import android.widget.ImageView
77
import android.widget.TextView
8+
import androidx.core.content.res.ResourcesCompat
89
import com.omega_r.libs.omegatypes.*
910
import kotlin.concurrent.thread
1011

@@ -16,7 +17,16 @@ class MainActivity : BaseActivity() {
1617
override fun onCreate(savedInstanceState: Bundle?) {
1718
super.onCreate(savedInstanceState)
1819
setContentView(R.layout.activity_main)
19-
val text = Text.from("test ") + Text.from(R.string.hello_world, Text.from(R.string.app_name), textStyle = TextStyle.bold())
20+
val text = Text.from("test ") +
21+
Text.from(
22+
R.string.hello_world,
23+
Text.from(R.string.app_name),
24+
textStyle = TextStyle.font(ResourcesCompat.getFont(this, R.font.noto_sans_regular)!!)
25+
) +
26+
Text.from(
27+
" SEMI BOLD",
28+
textStyle = TextStyle.font(ResourcesCompat.getFont(this, R.font.noto_sans_semi_bold)!!)
29+
)
2030
text.applyTo(exampleTextView) // or exampleTextView.setText(text)
2131
val image = Image.from("https://avatars1.githubusercontent.com/u/28600571")
2232

306 KB
Binary file not shown.
306 KB
Binary file not shown.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ buildscript {
1111
jcenter()
1212
}
1313
dependencies {
14-
classpath 'com.android.tools.build:gradle:3.2.0'
14+
classpath 'com.android.tools.build:gradle:3.4.1'
1515
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1616
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
1717
// NOTE: Do not place your application dependencies here; they belong
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Oct 03 15:12:40 MSK 2018
1+
#Wed May 29 11:12:26 MSK 2019
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip

omegatypes/src/main/java/com/omega_r/libs/omegatypes/TextStyle.kt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,20 @@ abstract class TextStyle : Serializable {
4242
fun strikethrough(): TextStyle = strikethroughTextStyle
4343

4444
@JvmStatic
45-
fun font(fontName: Text): TextStyle = FontTextStyle(fontName)
45+
fun font(fontName: Text): TextStyle = FontTextStyle(fontNameText = fontName)
4646

4747
@JvmStatic
48-
fun font(fontName: String): TextStyle = FontTextStyle(fontName.toText())
48+
fun font(fontName: String): TextStyle = FontTextStyle(fontNameText = fontName.toText())
49+
50+
@JvmStatic
51+
fun font(typeface: Typeface): TextStyle = FontTextStyle(fontTypeface = typeface)
4952

5053
@JvmStatic
5154
fun size(size: Size): TextStyle = SizeTextStyle(size)
5255

5356
}
5457

55-
operator fun plus(textStyle: TextStyle?):TextStyle {
58+
operator fun plus(textStyle: TextStyle?): TextStyle {
5659
val list = mutableListOf<TextStyle>().apply {
5760
addTextStyle(this@TextStyle)
5861
addTextStyle(textStyle)
@@ -115,12 +118,23 @@ abstract class TextStyle : Serializable {
115118
}
116119
}
117120

118-
private class FontTextStyle(private val fontNameText: Text) : TextStyle() {
121+
private class FontTextStyle(
122+
private val fontNameText: Text? = null,
123+
private val fontTypeface: Typeface? = null
124+
) : TextStyle() {
119125

120126
override fun SpannableString.applyStyle(context: Context) {
121-
fontNameText.getString(context)?.let {
122-
setSpan(TypefaceSpan(it),0, length, 0)
123-
}
127+
setSpan(createSpan(context), 0, length, 0)
128+
}
129+
130+
private fun createSpan(context: Context): Any {
131+
if (fontTypeface != null) {
132+
return StyleSpan(fontTypeface.style)
133+
}
134+
fontNameText?.getString(context)?.let {
135+
return TypefaceSpan(it)
136+
}
137+
throw IllegalStateException()
124138
}
125139

126140
}

0 commit comments

Comments
 (0)