Android Jetpack Compose 之 Text

官方教程

下載Android Studio Canary版本

環(huán)境

添加依賴:

    def compose_version = "0.1.0-dev03"

    kapt("androidx.compose:compose-compiler:$compose_version")
    implementation("androidx.compose:compose-runtime:$compose_version")
    implementation("androidx.ui:ui-core:$compose_version")
    implementation "androidx.ui:ui-tooling:$compose_version"
    implementation("androidx.ui:ui-layout:$compose_version")
    implementation("androidx.ui:ui-framework:$compose_version")
    implementation("androidx.ui:ui-material:$compose_version")
    implementation("androidx.ui:ui-foundation:$compose_version")
    implementation("androidx.ui:ui-text:$compose_version")

androidx.ui.core中的Composable Text組件為我們提供了一系列的屬性來(lái)控制文本的外觀。

@Composable
fun Text(
    text: String,
    modifier: Modifier = Modifier.None,
    style: TextStyle? = null,
    paragraphStyle: ParagraphStyle? = null,
    softWrap: Boolean = DefaultSoftWrap,
    overflow: TextOverflow = DefaultOverflow,
    maxLines: Int? = DefaultMaxLines
) {
    Text(
        text = AnnotatedString(text),
        modifier = modifier,
        style = style,
        paragraphStyle = paragraphStyle,
        softWrap = softWrap,
        overflow = overflow,
        maxLines = maxLines
    )
}

基本屬性

softWrap

聲明是否應(yīng)在換行符處斷開文本

overflow

組件中溢出文本

省略號(hào)


image

淡入淡出


image
@Preview
@Composable
fun initLayout() {
    // 定義一個(gè)有寬度的容器
    Container (
        width = Dp(80f)
    ){
        Text(
            text = "1234567890asdfgh",
            softWrap = false,
            maxLines = 1,
            overflow = TextOverflow.Fade
        )
    }
}

文本樣式

在定義文本組件的樣式屬性時(shí),我們需要使用TextStyle類的實(shí)例

@Immutable
data class TextStyle(
    // 文本顏色
    val color: Color? = null,
    // Sp 字體大小
    val fontSize: TextUnit = TextUnit.Inherit,
    // 文本粗細(xì)
    val fontWeight: FontWeight? = null,
    // 文本樣式 Normal或Italic
    val fontStyle: FontStyle? = null,
    // 文本字體手動(dòng)合成粗體/斜體樣式
    val fontSynthesis: FontSynthesis? = null,
    // 文本字體
    val fontFamily: FontFamily? = null,
    // 字體的高級(jí)排版設(shè)置
    val fontFeatureSettings: String? = null,
    //  字符之間的間距
    val letterSpacing: TextUnit = TextUnit.Inherit,
    // 從文本基線開始使用的垂直移位
    val baselineShift: BaselineShift? = null,
    // 文本幾何變換  
    val textGeometricTransform: TextGeometricTransform? = null,
    // 特定語(yǔ)言環(huán)境的文本
    val localeList: LocaleList? = null,
    //  文本背景色
    val background: Color? = null,
    // 在文本的某處繪制水平線 None Underline LineThrough
    val decoration: TextDecoration? = null,
    // 要在文本上繪制的陰影
    val shadow: Shadow? = null
)

段落樣式

ParagraphStyle類

data class ParagraphStyle constructor(
    // 文本對(duì)齊方式 LEFT、START、RIGHT、END、CENTER、JUSTIFY
    val textAlign: TextAlign? = null,
    // 枚舉類設(shè)置文本顯示方向
    val textDirectionAlgorithm: TextDirectionAlgorithm? = null,
    // 文本行高
    val lineHeight: Sp? = null,
    // 文本縮進(jìn)
    val textIndent: TextIndent? = null
)

多樣化文本

/**
 * The basic data structure of text with multiple styles. To construct an [AnnotatedString] you
 * can use [Builder].
 */
data class AnnotatedString(
    val text: String,
    val textStyles: List<Item<TextStyle>> = listOf(),
    val paragraphStyles: List<Item<ParagraphStyle>> = listOf()
)

例如:

@Composable
fun initLayout() {
    // 定義一個(gè)有寬度的容器
    Container (
        width = Dp(80f)
    ){
        val annotatedString = AnnotatedString.Builder("1234567890asdfgh")
            .apply {
                addStyle(TextStyle(color = Color.Red), 0, 3)
            }

        Text(
            text = annotatedString.toAnnotatedString(),
            softWrap = false,
            maxLines = 1,
            overflow = TextOverflow.Fade
        )
    }
}

添加和刪除樣式

  • pushStyle:將TextStyle添加到AnnotatedText中,該樣式將應(yīng)用于推入樣式后附加的任何文本返回樣式的索引。
  • pushStyle: 將ParagraphStyle添加到AnnotatedText中
  • popStyle: 從AnnotatedText中刪除以前添加的樣式
  • popStyle: 從AnnotatedText中刪除指定索引處的樣式
@Preview
@Composable
fun initLayout() {
    // 定義一個(gè)有寬度的容器
    Container (
        width = Dp(150f),
        alignment =  Alignment.CenterLeft

    ){
        val style = TextStyle(color = Color.Red,background = Color.White)
        val annotatedString = AnnotatedString.Builder("123456")
        annotatedString.pushStyle(style)
        annotatedString.append(", aaaaaa")
        // 去掉樣式
        annotatedString.popStyle()
        annotatedString.append("cccccc.")
        Text(text = annotatedString.toAnnotatedString(), maxLines = 1)
    }

}

image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 探索 Android 中的 Span 在 Android 中,使用 Span 定義文本的樣式. 通過(guò) Span 改...
    TonnyL閱讀 6,340評(píng)論 2 14
  • ??DOM 1 級(jí)主要定義的是 HTML 和 XML 文檔的底層結(jié)構(gòu)。 ??DOM2 和 DOM3 級(jí)則在這個(gè)結(jié)構(gòu)...
    霜天曉閱讀 1,599評(píng)論 1 3
  • Can't get dom width or height echarts解決方案 var mainContain...
    飛豹豹豹豹豹閱讀 1,726評(píng)論 0 0
  • 今天就爬華山,從早上就很幸運(yùn),農(nóng)家樂(lè)的哥哥去買菜,就免費(fèi)送我們到了山腳的地方。(*ˉ︶ˉ*) 去華山的路只有一條,...
    愛看書的西瓜閱讀 302評(píng)論 0 2

友情鏈接更多精彩內(nèi)容