Jetpack Compose基础组件之 Text

326 阅读3分钟

概述

本文基于androidx.compose.material3依赖进行分析。依赖版本

androidx.compose.material3:material3-android:1.3.1

Text 组件是 Jetpack Compose 中用于显示文本的基本组件。它提供了丰富的功能来自定义文本的外观和行为。

基本用法

要使用 Text 组件,只需导入 androidx.compose.material3.Text 并在 Composable 函数中调用它。

1.text: String

  • 描述: 要显示的文本内容。

  • 示例:

    Text(text = "Hello, Jetpack Compose!")
    

2. modifier: Modifier = Modifier

  • 描述: 用于修改 Text 的布局、外观或行为,例如添加填充、边距、点击事件等。

  • 示例:

    Text(
        text = "Hello, World!",
        modifier = Modifier.padding(16.dp).background(Color.LightGray)
    )
    

3. color: Color = Color.Unspecified

  • 描述: 设置文本的颜色。如果未指定,则使用父级样式的默认颜色。

  • 示例:

    Text(
        text = "Hello, World!",
        color = Color.Red
    )
    

4. fontSize: TextUnit = TextUnit.Unspecified

  • 描述: 设置文本的字体大小。单位可以是 sp(可缩放像素)或 px(像素)。

  • 示例:

    Text(
        text = "Hello, World!",
        fontSize = 20.sp
    )
    

5. fontStyle: FontStyle? = null

  • 描述: 设置文本的字体样式(如正常或斜体)。可选值为 FontStyle.Normal 或 FontStyle.Italic

  • 示例:

    Text(
        text = "Hello, World!",
        fontStyle = FontStyle.Italic
    )
    

6. fontWeight: FontWeight? = null

  • 描述: 设置文本的字体粗细。可选值包括 FontWeight.ThinFontWeight.Bold 等。

  • 示例:

    Text(
        text = "Hello, World!",
        fontWeight = FontWeight.Bold
    )
    

7. fontFamily: FontFamily? = null

  • 描述: 设置文本的字体家族。您可以使用系统提供的字体或自定义字体。

  • 示例:

    val customFont = FontFamily(Font(R.font.my_custom_font))
    Text(
        text = "Hello, World!",
        fontFamily = customFont
    )
    

8. letterSpacing: TextUnit = TextUnit.Unspecified

  • 描述: 设置字符之间的间距。单位是 em,表示相对于当前字体大小的比例。

  • 示例:

    Text(
        text = "Hello, World!",
        letterSpacing = 0.1.em
    )
    

9. textDecoration: TextDecoration? = null

  • 描述: 添加文本装饰效果,例如下划线或删除线。可选值包括 TextDecoration.Underline 和 TextDecoration.LineThrough

  • 示例:

    Text(
        text = "Hello, World!",
        textDecoration = TextDecoration.Underline
    )
    

10. textAlign: TextAlign? = null

  • 描述: 设置文本在容器中的对齐方式。可选值包括 TextAlign.LeftTextAlign.CenterTextAlign.Right 等。

  • 示例:

    Text(
        text = "Hello, World!",
        textAlign = TextAlign.Center,
        modifier = Modifier.width(200.dp)
    )
    

11. lineHeight: TextUnit = TextUnit.Unspecified

  • 描述: 设置行高(即行与行之间的距离)。单位可以是 sp 或 em

  • 示例:

    Text(
        text = "Hello,\nWorld!",
        lineHeight = 1.5.em
    )
    

12. overflow: TextOverflow = TextOverflow.Clip

  • 描述: 定义当文本溢出其容器时的行为。可选值包括 TextOverflow.Clip(裁剪)、TextOverflow.Ellipsis(省略号)等。

  • 示例:

    Text(
        text = "This is a very long text that will not fit in the container.",
        maxLines = 1,
        overflow = TextOverflow.Ellipsis
    )
    

13. softWrap: Boolean = true

  • 描述: 是否允许文本自动换行。如果设置为 false,文本将在一行内显示。

  • 示例:

    Text(
        text = "This is a long text that will not wrap.",
        softWrap = false
    )
    

14. maxLines: Int = Int.MAX_VALUE

  • 描述: 设置文本的最大行数。如果超过最大行数,将根据 overflow 属性处理溢出部分。

  • 示例:

    Text(
        text = "This is a long text with multiple lines.",
        maxLines = 2
    )
    

15. minLines: Int = 1

  • 描述: 设置文本的最小行数。即使文本内容较少,也会占用指定的行数。

  • 示例:

    Text(
        text = "Short text",
        minLines = 3
    )
    

16. onTextLayout: ((TextLayoutResult) -> Unit)? = null

  • 描述: 一个回调函数,在文本布局完成后调用。可以使用它来获取布局信息,例如文本宽度、高度、行数等。

  • 示例:

    Text(
        text = "Hello, World!",
        onTextLayout = { result ->
            println("Text width: ${result.size.width}, height: ${result.size.height}")
        }
    )
    

17. style: TextStyle = LocalTextStyle.current

  • 描述: 提供一个整体的 TextStyle 对象,用于一次性设置多个文本样式属性。可单独封装一个字体样式类,进行全局设置

  • 示例:

    val textStyle = TextStyle(
        color = Color.Blue,
        fontSize = 18.sp,
        fontWeight = FontWeight.Bold
    )
    Text(
        text = "Hello, World!",
        style = textStyle
    )
    

注意事项

Text 组件默认情况下会自动换行。如果需要禁用自动换行,可以设置 softWrap 参数为 false。 Text 组件支持 AnnotatedString,可以用于在文本中嵌入不同的样式或点击事件。