Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.yzbzz:StylePhrase:1.0.3'
}
dependencies {
implementation("com.github.yzbzz:StylePhrase:1.0.3")
}
<dependency>
<groupId>com.github.yzbzz</groupId>
<artifactId>StylePhrase</artifactId>
<version>1.0.3</version>
</dependency>
libraryDependencies += "com.github.yzbzz" % "StylePhrase" % "1.0.3"
:dependencies [[com.github.yzbzz/StylePhrase "1.0.3"]]
StylePhrase is an Android string resource templating library
在项目中,我们经常会碰到一行文本,部分文字要显示不同的颜色和大小,或者其他样式。使用StylePhrase可以很方便的解决这个问题。只需要把需要显示其它样式的文本使用分割符包裹一下,就可以对文本进行各种操作。
本库主要基于以下项目做了修改,更方便使用和扩展:
https://github.com/square/phrase
https://github.com/THEONE10211024/ColorPhrase


注意:对于比较特殊的字符,使用StylePhrase会导致一些问题,尽量使用{}、[]和()作为分割符
大部分时候,一行文本我们需要显示2种或3种样式,所以StylePhrase默认提供两种分割符{}和[],当然你也可以修改默认的分割符。为了方便使用,StylePhrase默认提供了setInnerFirstColor、setInnerFirstSize、setInnerSecondColor、setInnerSecondSize
一种操作符
// 永生不过是场{幻梦},唯吾{所爱}不朽
val oneSeparatorString = getString(R.string.text_phrase_one)
val colorAndSize = StylePhrase(oneSeparatorString)
.setInnerFirstColor(Color.BLUE)
.setInnerFirstSize(20)
tv_content.text = colorAndSize.format()
二种操作符
// 何以{缘起}?何以[缘灭]?当以{剑歌}问之
val twoSeparatorString = getString(R.string.text_phrase_two)
val colorAndSize = StylePhrase(twoSeparatorString)
.setInnerFirstColor(Color.BLUE)
.setInnerFirstSize(20)
.setInnerSecondColor(Color.RED)
.setInnerSecondSize(25)
tv_content.text = colorAndSize.format()
多种操作符
// 凤兮{凤兮}归[故乡],遨(游)四海(求其凰)
val colorAndSize = StylePhrase(multiSeparatorString)
.setInnerFirstColor(Color.BLUE)
.setInnerFirstSize(20)
.setInnerSecondColor(Color.RED)
.setInnerSecondSize(25)
val builder = StylePhrase.Builder()
builder.separator = "()"
builder.setColor(Color.GREEN)
builder.setSize(18)
colorAndSize.addBuilder(builder)
tv_content.text = colorAndSize.format()
更多操作可以参考DEMO
使用Gradle进行引用
1.在你根目录的build.gradle文件添加代码: maven { url 'https://www.jitpack.io' }
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
2.在你的工程目录添加
StylePhrase依赖appcompat-v7,所以需要同时引用appcompat-v7包
dependencies {
implementation 'com.github.yzbzz:StylePhrase:1.0.3'
implementation 'com.android.support:appcompat-v7:$version'
}
你也可以直接下载StylePhrase类放到你的工程中