Android UI开发之多样式富文本的简洁实现

文章介绍了一种使用appendableStyleScope在Android中简便创建和管理多样式富文本的方法,包括链接、图片、特殊样式如首行缩进、子弹点、引用、删除线、上标和下标等,并提供了相应的代码示例。
摘要由CSDN通过智能技术生成

多样式富文本的简洁实现

appendableStyleScope 允许你快速构建多种样式文字。

富文本

特性

  • 支持对于同一个字符串设置多种样式。
  • 支持文字和图片。
  • 提供默认样式。
  • 采用 DSL 确保更清晰的样式作用范围

快速开始

下面的示例为你展示了如何创建一条链接文本。

appendableStyleScope(getBinding().tv) {
    withStyle(AppendableStyle(url = "https://www.baidu.com/")) {
        append("这是一个链接")
    }
}

下面的示例为你展示了如何创建一条图片文本。

appendableStyleScope(getBinding().tv) {
    withImage(ImageSpan(this, R.mipmap.ic_launcher))
}

拼接字符串

下面的示例为你展示如何创建一条复杂的富文本字符串。

appendableStyleScope(getBinding().tv) {
    withStyle(AppendableStyle(QuoteSpan(ColorUtils.colorHex2Int("#27ae60"), 10, 30))) {
        withStyle(AppendableStyle(fontSize = 20F.SP.toInt())) {
            appendLine("什么是 Android?")
        }
        append("一个颠覆移动设备功能的平台,你可以访问")
        withStyle(AppendableStyle("https://www.android.com/intl/zh-CN_cn/what-is-android/")) {
            append("链接")
        }
        appendLine("来了解更多。")
        append(
            "从只能让设备运行,到让生活更轻松,都是Android在背后提供强力支持。" +
                    "有了Android, 才能让GPS避开拥堵,用手表发短信,让Google助理回答问题。" +
                    "目前有 25 亿部活跃设备搭载了 Android 操作系统。Android 能够为各种设备" +
                    "提供强力支持,从 5G 手机到炫酷的平板电脑,不胜枚举。"
        )
        withStyle(AppendableStyle(ScriptMode.SUPERSCRIPT)) { append("[1]") }
        append("\n")
        withImage(ImageSpan(this@DateActivity, R.drawable.android_logo))
    }
}

富文本

通用样式

通过指定 AppendableStyle 内的 backColor 属性,可以为字符串设置背景色。目前 AppendableStyle 允许你指定以下属性:

  • foreColor : 文字颜色。
  • backColor : 文字背景色。
  • fontStyle : 文字风格,目前支持粗体、斜体或者正常。
  • fontFamily : 文字字体。
  • fontSize : 文字大小。
  • fontAlign : 文字对齐方式。
  • proportion : 文字放大比例,例如如果放大 50% ,则该属性设置为 1.5f 。
  • xProportion : 值 > 1.0 会将文本拉伸得更宽。值 < 1.0 会将文本拉伸得更窄。
appendableStyleScope(getBinding().tv) {
    withStyle(AppendableStyle(url = "https://www.baidu.com/", backColor = R.color.lightslategray)
    ) {
        append("这是一个链接")
    }
}

特殊样式

除了通用样式,你也可以为文字指定以下唯一的特殊样式,目前支持:

链接文字

appendableStyleScope(getBinding().tv) {
    withStyle(AppendableStyle(url = "https://www.baidu.com/")) {
        append("这是一个链接")
    }
}

链接富文本

首行缩进

appendableStyleScope(getBinding().tv) {
    withStyle(AppendableStyle(linesIndent = LeadingMarginSpan.Standard(100, 0))) {
        append("这是一段带首行缩进的文本哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈。")
    }
}

首行缩进

携带子弹点

appendableStyleScope(getBinding().tv) {
    withStyle(
        AppendableStyle(bulletSpan = BulletSpan(10, ColorUtils.colorHex2Int("#d63031"), 10))
    ) {
        append("这是一段带子弹点的文字。")
    }
}

携带子弹点

引用文字

appendableStyleScope(getBinding().tv) {
    withStyle(
        AppendableStyle(quoteSpan = QuoteSpan(ColorUtils.colorHex2Int("#f0932b"), 10, 10))
    ) {
        append("这是一段引用文字。")
    }
}

引用文字

删除线或者下划线

appendableStyleScope(getBinding().tv) {
    withStyle(
        AppendableStyle(strikeMode = StrikeMode.STRIKETHROUGH)
    ) {
        append("这是一段带删除线的文字。")
    }
    withStyle(
        AppendableStyle(strikeMode = StrikeMode.UNDERLINE)
    ) {
        append("这是一段带下划线的文字。")
    }
}

删除线或者下划线

上标或者下标

appendableStyleScope(getBinding().tv) {
    withStyle { append("这是一段带上标的文字。") }
    withStyle(AppendableStyle(scriptMode = ScriptMode.SUPERSCRIPT)) {
        append("[1]")
    }
}

上标或者下标

范围模糊

appendableStyleScope(getBinding().tv) {
    withStyle(
        AppendableStyle(blurRadius = 5f, blur = BlurMaskFilter.Blur.NORMAL)
    ) {
        append("这是一段带模糊的文字。")
    }
}

范围模糊

可点击文字

该项不提供示例。

源代码

你可以点击 AppendableStyleString 来查看源码,这个仓库也包含了我封装的其他工具,如果你喜欢的话,还希望能够star,fork和提出issue。😀😀😀

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上夏雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值