Skip to content

Commit

Permalink
1,添加加粗功能
Browse files Browse the repository at this point in the history
2,添加设置字体大小
  • Loading branch information
FlyJingFish committed Dec 19, 2021
1 parent d4e5e38 commit 1a90534
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.flyjingfish.formattextview
package com.flyjingfish.FormatTexttextview

import androidx.annotation.ColorRes
import androidx.annotation.StringRes
Expand All @@ -9,23 +9,46 @@ class FormatText {
var color = 0
var bold = false
var underline = false
var italic = false
var strValue: String? = null

@StringRes
var intValue = 0
var resValue = 0
var textSize = 0;

constructor(color: Int, bold: Boolean, underline: Boolean, strValue: String?) {
fun setColor(@ColorRes color: Int): FormatText {
this.color = color
return this
}

fun setBold(bold: Boolean): FormatText {
this.bold = bold
return this
}

fun setUnderline(underline: Boolean): FormatText {
this.underline = underline
return this
}

fun setItalic(italic: Boolean): FormatText {
this.italic = italic
return this
}

fun setStrValue(strValue: String?): FormatText {
this.strValue = strValue
return this
}

constructor(color: Int, bold: Boolean, underline: Boolean, intValue: Int) {
this.color = color
this.bold = bold
this.underline = underline
this.intValue = intValue
fun setResValue(@StringRes resValue: Int): FormatText {
this.resValue = resValue
return this
}

fun setTextSize(textSize: Int): FormatText {
this.textSize = textSize
return this
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import android.content.Context
import android.text.Html
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.style.AbsoluteSizeSpan
import android.text.style.ClickableSpan
import android.text.style.URLSpan
import android.text.util.Linkify
import android.util.AttributeSet
import android.view.View
import androidx.annotation.StringRes
import androidx.appcompat.widget.AppCompatTextView
import com.flyjingfish.FormatTexttextview.FormatText

class FormatTextView :AppCompatTextView {

Expand All @@ -35,20 +37,24 @@ class FormatTextView :AppCompatTextView {
val strings = arrayOfNulls<String>(args.size)
val colors = IntArray(args.size)
val bolds = BooleanArray(args.size)
val italics = BooleanArray(args.size)
val underlines = BooleanArray(args.size)
for (i in 0 until args.size) {
val textSizes = IntArray(args.size)
for (i in args.indices) {
if (args[i] != null){
if (args[i]!!.intValue != 0) {
strings[i] = resources.getString(args[i]!!.intValue)
if (args[i]!!.resValue != 0) {
strings[i] = resources.getString(args[i]!!.resValue)
} else {
strings[i] = args[i]!!.strValue
}
colors[i] = args[i]!!.color
bolds[i] = args[i]!!.bold
italics[i] = args[i]!!.italic
underlines[i] = args[i]!!.underline
textSizes[i] = args[i]!!.textSize
}
}
setFormatText(colors, bolds, underlines, formatTextValue, strings)
setFormatText(colors, bolds,italics, underlines,textSizes, formatTextValue, strings)
}


Expand All @@ -59,7 +65,7 @@ class FormatTextView :AppCompatTextView {
fun setFormatText(formatTextValue: String, vararg args: Int) {
val formatTexts: Array<FormatText?> = arrayOfNulls<FormatText>(args.size)
for (i in 0 until args.size) {
formatTexts[i] = FormatText(0, false, false, args[i])
formatTexts[i] = FormatText().setResValue(args[i])
}
setFormatTextBean(formatTextValue, *formatTexts)
}
Expand All @@ -71,7 +77,7 @@ class FormatTextView :AppCompatTextView {
fun setFormatText(formatTextValue: String, vararg args: String) {
val formatTexts: Array<FormatText?> = arrayOfNulls<FormatText>(args.size)
for (i in 0 until args.size) {
formatTexts[i] = FormatText(0, false, false, args[i])
formatTexts[i] = FormatText().setStrValue(args[i])
}
setFormatTextBean(formatTextValue, *formatTexts)
}
Expand All @@ -80,43 +86,37 @@ class FormatTextView :AppCompatTextView {
private fun setFormatText(
colors: IntArray,
bolds: BooleanArray,
italics: BooleanArray,
underlines: BooleanArray,
@StringRes formatTextRes: Int,
args: Array<String?>
) {
setFormatText(colors, bolds, underlines, resources.getString(formatTextRes), args)
}

private fun setFormatText(
colors: IntArray,
bolds: BooleanArray,
underlines: BooleanArray,
textSizes: IntArray,
formatTextValue: String,
args: Array<String?>
) {
val strings = arrayOfNulls<String>(args.size)
for (i in args.indices) { //%1$s
var start = "<a href=\"$i\">"
var end = "</a>"
val value = "%" + (i + 1) + "\$s"
var value = strings[i]
if (italics[i]) {
value = "<em>$value</em>"
}
if (bolds[i]) {
strings[i] = "<b>$value</b>"
} else {
strings[i] = value
value = "<b>$value</b>"
}
strings[i] = start + strings[i] + end

strings[i] = start + value + end
}
val formatText = String.format(formatTextValue, *strings as Array<Any?>)
val showText = String.format(formatText, *args as Array<Any?>)
text = getClickableHtml(showText, colors, underlines)
text = getClickableHtml(formatText, colors, underlines,textSizes)
highlightColor = resources.getColor(R.color.transparent)
autoLinkMask = Linkify.WEB_URLS
}

private fun getClickableHtml(
html: String,
colors: IntArray,
underlines: BooleanArray
underlines: BooleanArray,
textSizes: IntArray
): CharSequence? {
val spannedHtml = Html.fromHtml(html)
val clickableHtmlBuilder = SpannableStringBuilder(spannedHtml)
Expand All @@ -126,7 +126,7 @@ class FormatTextView :AppCompatTextView {
)
for (i in spans.indices) {
val span = spans[i]
setLinkClickable(clickableHtmlBuilder, span, colors[i], underlines[i])
setLinkClickable(clickableHtmlBuilder, span, colors[i], underlines[i] ,textSizes[i])
}
return clickableHtmlBuilder
}
Expand All @@ -135,7 +135,8 @@ class FormatTextView :AppCompatTextView {
clickableHtmlBuilder: SpannableStringBuilder,
urlSpan: URLSpan,
color: Int,
underline: Boolean
underline: Boolean,
textSize: Int
) {
val start = clickableHtmlBuilder.getSpanStart(urlSpan)
val end = clickableHtmlBuilder.getSpanEnd(urlSpan)
Expand All @@ -160,6 +161,9 @@ class FormatTextView :AppCompatTextView {
}
}
clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags)
if (textSize > 0){
clickableHtmlBuilder.setSpan(AbsoluteSizeSpan(textSize,true), start, end, flags)
}
}

override fun setOnClickListener(l: OnClickListener?) {
Expand Down
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# FormatTextViewLib
多数app登陆首页都需要显示用户协议和隐私政策并且需要能够点击,遇到需要翻译多个国家语言的,多个TextView拼接会导致语序不对,而且换行也是个问题

本库支持字体设置字体颜色,加粗,斜体,下划线,字体大小

使用示例:
可以看示例代码Demo

第一步. 根目录build.gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

第二步. 需要引用的build.gradle

dependencies {
implementation 'com.github.FlyJingFish:FormatTextViewLib:v1.0'
}
dependencies {
implementation 'com.github.FlyJingFish:FormatTextViewLib:v1.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.flyjingfish.formattextview.FormatText
import com.flyjingfish.formattextview.FormatTextView
import com.flyjingfish.FormatTexttextview.FormatText
import com.flyjingfish.formattextview.OnFormatClickListener
import kotlinx.android.synthetic.main.activity_main.*

Expand All @@ -17,16 +16,18 @@ class MainActivity : AppCompatActivity() {
text2.setFormatText(R.string.test_text, "wo","you")
text3.setFormatText("%1\$s欢迎%2\$s", R.string.we,R.string.you)
text4.setFormatText("%1\$s欢迎%2\$s", "wo","you")
text5.setFormatTextBean(R.string.test_text, FormatText(R.color.colorAccent,true,false,R.string.we),FormatText(R.color.colorPrimaryDark,true,true,R.string.you))
text6.setFormatTextBean(R.string.test_text, FormatText(R.color.colorAccent,true,false,"we"),FormatText(R.color.colorPrimaryDark,true,true,"you"))
text7.setFormatTextBean("%1\$s欢迎%2\$s", FormatText(R.color.colorAccent,true,false,R.string.we),FormatText(R.color.colorPrimaryDark,true,true,R.string.you))
text8.setFormatTextBean("%1\$s欢迎%2\$s", FormatText(R.color.colorAccent,true,false,"we"),FormatText(R.color.colorPrimaryDark,true,true,"you"))
text8.setOnFormatClickListener(object : OnFormatClickListener{
text5.setFormatTextBean(R.string.test_text,
FormatText().setColor(R.color.colorAccent).setBold(true).setUnderline(true).setItalic(true).setResValue(R.string.we).setTextSize(30),
FormatText().setColor(R.color.colorPrimaryDark).setBold(true).setUnderline(false).setItalic(false).setStrValue("you"))
text6.setFormatTextBean("%1\$s欢迎%2\$s",
FormatText().setColor(R.color.colorAccent).setBold(false).setUnderline(true).setItalic(true).setResValue(R.string.we),
FormatText().setColor(R.color.colorPrimaryDark).setBold(false).setUnderline(true).setItalic(false).setStrValue("you").setTextSize(60))
text6.setOnFormatClickListener(object : OnFormatClickListener{
override fun onLabelClick(position: Int) {
Toast.makeText(this@MainActivity,"onItemClick-item"+position,Toast.LENGTH_SHORT).show()
}
})
text8.setOnClickListener {
text6.setOnClickListener {
Toast.makeText(this@MainActivity,"onClick-view",Toast.LENGTH_SHORT).show()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import com.flyjingfish.formattextview.FormatTextView;

import java.util.Arrays;

public class SecondActivity extends AppCompatActivity {
@Override
Expand Down
12 changes: 2 additions & 10 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,13 @@
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Hello World!" />
<com.flyjingfish.formattextview.FormatTextView
android:id="@+id/text6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<com.flyjingfish.formattextview.FormatTextView
android:id="@+id/text7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<com.flyjingfish.formattextview.FormatTextView
android:id="@+id/text8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Hello World!" />


Expand Down

0 comments on commit 1a90534

Please sign in to comment.