欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Span富文本简单使用 - Spannable

程序员文章站 2022-10-07 20:01:07
###为文字实现很丰富的特殊效果,下面来实现几种很常用的操作so... 直接上效果:​简单介绍下Spanable中的常用常量:Spanned.SPAN_EXCLUSIVE_EXCLUSIVE -- 不包含start和end所在的端点 (a,b)Spanned.SPAN_EXCLUSIVE_INCLUSIVE -- 不包含端start,但包含end所在的端点 (a,b]Spanned.SPAN_INCLUSIVE_EXCLUSIVE -- ......

###为文字实现很丰富的特殊效果,下面来实现几种很常用的操作

so... 直接上效果:

Android Span富文本简单使用 - Spannable

​先简单介绍下Spanable中的常用常量:

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE -- 不包含start和end所在的端点              (a,b)

Spanned.SPAN_EXCLUSIVE_INCLUSIVE -- 不包含端start,但包含end所在的端点       (a,b]

Spanned.SPAN_INCLUSIVE_EXCLUSIVE -- 包含start,但不包含end所在的端点         [a,b)

Spanned.SPAN_INCLUSIVE_INCLUSIVE -- 包含start和end所在的端点                [a,b]
 

#1 设置部分文字的大小/颜色以及改变字体:

 ##改变大小:AbsoluteSizeSpan/RelativeSizeSpan

//设置部分文字的大小 AbsoluteSizeSpan/RelativeSizeSpan
sb.setSpan(
            AbsoluteSizeSpan(size),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )

 ##改变大小:ForegroundColorSpan

//改变部分文字的颜色
sb.setSpan(
            ForegroundColorSpan(color),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )

 ##除了上面的简单的两种,我们还可以用:TextAppearanceSpan

//更改Text的一些属性
sb.setSpan(
            TextAppearanceSpan(this, R.style.DateTextOther),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )

 cc:使用TextAppearanceSpan,可以直接同时更改Text的各种属性,再贴上简单的style: R.style.DateTextOther

<style name="DateTextOther">
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">@color/colorAccent</item>
        <item name="android:textAllCaps">true</item>
        <item name="android:typeface">serif</item>
        <item name="android:fontFamily">@font/graphik_bold</item>
    </style>

 

#2 设置部分文字的背景色:BackgroundColorSpan

//设置部分文字背景色
sb.setSpan(
            BackgroundColorSpan(color),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )

 

#3 设置部分文字的下划线/删除线:UnderlineSpan/StrikethroughSpan

//设置部分文字添加下划线
sb.setSpan(
            UnderlineSpan(),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )

 

#4 设置部分文字的上下标:SuperscriptSpan/SubscriptSpan

//设置为上标
sb.setSpan(
            SuperscriptSpan(),
            indexTH,
            indexTH + TEXT_TH.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )

//设置为下标
sb.setSpan(
            SubscriptSpan(),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )

 

##最后贴上Activity示例:

class TextSpanActivity : AppCompatActivity() {

    companion object {
        private const val TEXT_CONTENT = "Today is Jul 25th"
        private const val TEXT_SUB = "Jul"
        private const val TEXT_TH = "th"
    }

    private val index by lazy { TEXT_CONTENT.indexOf(TEXT_SUB) }
    private val indexTH by lazy { TEXT_CONTENT.indexOf(TEXT_TH) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_text_span)
        initView()
    }

    private fun initView() {
        text_type_size.text = getAbsoluteSizeSpan(12.toDp())
        text_type_color.text = getTextForegroundColorSpan(resources.getColor(R.color.colorAccent))
        text_type_text.text = getTextAppearanceSpan(R.style.DateTextOther)
        text_type_background.text = getBackgroundColorSpan(resources.getColor(R.color.colorAccent))
        text_type_underline.text = getUnderlineSpan()
        text_type_strikethrough.text = getStrikethroughSpan()
        text_type_superscript.text = getSuperscript_SubscriptSpan()
    }

    //设置部分文字的大小 AbsoluteSizeSpan/RelativeSizeSpan
    private fun getAbsoluteSizeSpan(size: Int): Spannable {
        val sb = SpannableStringBuilder(TEXT_CONTENT)
        sb.setSpan(
            AbsoluteSizeSpan(size),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        return sb
    }

    //设置部分文字的颜色
    private fun getTextForegroundColorSpan(color: Int): Spannable {
        val sb = SpannableStringBuilder(TEXT_CONTENT)
        sb.setSpan(
            ForegroundColorSpan(color),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        return sb
    }

    //设置部分文字的字体/大小/颜色
    private fun getTextAppearanceSpan(appearance: Int): Spannable {
        val sb = SpannableStringBuilder(TEXT_CONTENT)
        sb.setSpan(
            TextAppearanceSpan(this, appearance),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        return sb
    }

    //设置部分文字背景色
    private fun getBackgroundColorSpan(color: Int): Spannable {
        val sb = SpannableStringBuilder(TEXT_CONTENT)
        sb.setSpan(
            BackgroundColorSpan(color),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        return sb
    }

    //设置部分文字添加下划线
    private fun getUnderlineSpan(): Spannable {
        val sb = SpannableStringBuilder(TEXT_CONTENT)
        sb.setSpan(
            UnderlineSpan(),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        return sb
    }

    //设置部分文字添加删除线
    private fun getStrikethroughSpan(): Spannable {
        val sb = SpannableStringBuilder(TEXT_CONTENT)
        sb.setSpan(
            StrikethroughSpan(),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        return sb
    }

    //设置部分文字 上下标 Superscript/SubscriptSpan
    private fun getSuperscript_SubscriptSpan(): Spannable {
        val sb = SpannableStringBuilder(TEXT_CONTENT)
        //设置上标文字的字体/大小/颜色
        sb.setSpan(
            TextAppearanceSpan(this, R.style.DateTextOther),
            indexTH,
            indexTH + TEXT_TH.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        //设置为上标
        sb.setSpan(
            SuperscriptSpan(),
            indexTH,
            indexTH + TEXT_TH.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )

        //设置下标文字的字体/大小/颜色
        sb.setSpan(
            TextAppearanceSpan(this, R.style.DateTextOther),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        //设置为下标
        sb.setSpan(
            SubscriptSpan(),
            index,
            index + TEXT_SUB.length,
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        return sb
    }
}

Spanable未完待续...

>>URLSpan/ClickableSpan 

>>ReplacementSpan

>>ImageSpan

本文地址:https://blog.csdn.net/qq_20613731/article/details/107500213