Skip to content

Commit 92b160c

Browse files
committed
1,完善功能
1 parent 660876d commit 92b160c

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

FormatTextView/src/main/java/com/flyjingfish/formattextview/FormatTextView.kt

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class FormatTextView : BaseTextView {
3333
private var isClickSpanItem = false
3434
var isSetOnClick = false
3535
private var isDrawGradient = false
36+
private var isClearTexts = true
3637
private val underLineTexts: ArrayList<LineText> = ArrayList()
3738
private val deleteLineTexts: ArrayList<LineText> = ArrayList()
3839
private val gradientTexts: ArrayList<LineText> = ArrayList()
@@ -52,10 +53,10 @@ class FormatTextView : BaseTextView {
5253
setFormatTextBean(resources.getString(formatTextRes), *args)
5354
}
5455

55-
fun setFormatTextBean(formatTextValue: String, vararg args: BaseFormat?) {
56-
var textValue = formatTextValue.replace("\\r\\n".toRegex(), "<br>")
57-
textValue = textValue.replace("\\n".toRegex(), "<br>")
58-
textValue = textValue.replace("\\r".toRegex(), "<br>")
56+
fun setFormatTextBean(formatTextValue: String?, vararg args: BaseFormat?) {
57+
var textValue = formatTextValue?.replace("\\r\\n".toRegex(), "<br>")
58+
textValue = textValue?.replace("\\n".toRegex(), "<br>")
59+
textValue = textValue?.replace("\\r".toRegex(), "<br>")
5960
val strings = arrayOfNulls<String>(args.size)
6061
var isContainGradient = false
6162
for (i in args.indices) { //%1$s
@@ -84,28 +85,48 @@ class FormatTextView : BaseTextView {
8485

8586

8687
}
87-
val richText = String.format(textValue, *strings)
88-
formatArgs = args
89-
this.richText = richText
88+
if (textValue == null){
89+
isClearTexts = true
90+
text = ""
91+
return
92+
}
9093
isDrawGradient = !isContainGradient
9194
underLineTexts.clear()
9295
deleteLineTexts.clear()
9396
gradientTexts.clear()
9497
gradientDrawTexts.clear()
98+
val richText = String.format(textValue, *strings)
99+
formatArgs = args
100+
this.richText = richText
101+
isClearTexts = false
95102
text = getCustomStyleHtml(richText, *args)
96103
highlightColor = Color.TRANSPARENT
97104
autoLinkMask = Linkify.WEB_URLS
98105
}
99106

107+
override fun setText(text: CharSequence?, type: BufferType?) {
108+
if (isClearTexts){
109+
isDrawGradient = true
110+
underLineTexts.clear()
111+
deleteLineTexts.clear()
112+
gradientTexts.clear()
113+
gradientDrawTexts.clear()
114+
formatArgs = null
115+
this.richText = null
116+
}
117+
super.setText(text, type)
118+
}
119+
100120
private fun resetText(){
121+
isClearTexts = false
101122
text = richText?.let { formatArgs?.let { it1 -> getCustomStyleHtml(it, *it1) } }
102123
}
103124

104125
fun setFormatText(@StringRes formatTextRes: Int, vararg args: Int) {
105126
setFormatText(resources.getString(formatTextRes), *args)
106127
}
107128

108-
fun setFormatText(formatTextValue: String, vararg args: Int) {
129+
fun setFormatText(formatTextValue: String?, vararg args: Int) {
109130
val formatTexts: Array<FormatText?> = arrayOfNulls(args.size)
110131
for (i in args.indices) {
111132
formatTexts[i] = FormatText().setResValue(args[i])
@@ -117,7 +138,7 @@ class FormatTextView : BaseTextView {
117138
setFormatText(resources.getString(formatTextRes), *args)
118139
}
119140

120-
fun setFormatText(formatTextValue: String, vararg args: String) {
141+
fun setFormatText(formatTextValue: String?, vararg args: String) {
121142
val formatTexts: Array<FormatText?> = arrayOfNulls(args.size)
122143
for (i in args.indices) {
123144
formatTexts[i] = FormatText().setStrValue(args[i])
@@ -492,6 +513,7 @@ class FormatTextView : BaseTextView {
492513
drawUnderline(canvas)
493514
drawDeleteLine(canvas)
494515
getGradient()
516+
isClearTexts = true
495517
}
496518
private fun getGradient() {
497519
if (gradientTexts.size == 0 || isDrawGradient) {

README-zh.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ ALIGN_CENTER 为当前库新增对齐方式旨解决在小图标和文本中心
236236
| ignorePaintShader | boolean | 文字是否忽略TextView的Shader |
237237
| gradient | Gradient | 文字渐变色配置 |
238238

239+
## Gradient 参数一览
240+
241+
| 属性 | 参数类型 | 描述 |
242+
|-------------------|:---------------:|:-------------------------------------------------------------------------------------------------------------:|
243+
| gradientColors | @ColorInt int[] | 渐变色(颜色值为ColorInt 类型)数组 |
244+
| gradientPositions | float[] | 渐变色分布配置(此数组需与gradientColors长度一致) |
245+
| orientation | Orientation | LEFT_TO_RIGHT (从左到右)/TOP_TO_BOTTOM (从上到下)/LEFT_TOP_TO_RIGHT_BOTTOM (从左上到右下)/LEFT_BOTTOM_TO_RIGHT_TOP (从左下到右上) |
246+
239247
## FormatImage 参数一览
240248
| 属性 | 参数类型 | 描述 |
241249
|-------------------|:----------------:|:------------------------------------------------:|

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ ALIGN_CENTER adds an alignment method to the current library to solve the alignm
238238
| ignorePaintShader | boolean | Whether the text ignores the Shader of the TextView |
239239
| gradient | Gradient | Text Gradient Color Configuration |
240240

241+
## Gradient parameter list
242+
243+
| Attribute | Parameter Type | Description |
244+
|--------------------|:---------------:|:---------- -------------------------------------------------- --------------------------------------------------: |
245+
| gradientColors | @ColorInt int[] | array of gradient colors (color values are of type ColorInt) |
246+
| gradientPositions | float[] | Gradient color distribution configuration (this array needs to be consistent with the length of gradientColors) |
247+
| orientation | Orientation | LEFT_TO_RIGHT (left to right)/TOP_TO_BOTTOM (top to bottom)/LEFT_TOP_TO_RIGHT_BOTTOM (top left to bottom right)/LEFT_BOTTOM_TO_RIGHT_TOP (bottom left to top right) |
248+
241249
## FormatImage parameter list
242250
| property | parameter type | description |
243251
|-------------------|:----------------:|:----------------------------------------------------------:|

0 commit comments

Comments
 (0)