android Compose Text文本

fun Text(
    text: String, //显示的文本
    modifier: Modifier = Modifier,//修饰符
    color: Color = Color.Unspecified, //文字颜色
    fontSize: TextUnit = TextUnit.Unspecified,//文字大小
    fontStyle: FontStyle? = null,//字体变体。斜体等
    fontWeight: FontWeight? = null,//文本粗细
    fontFamily: FontFamily? = null, //文本的字体
    letterSpacing: TextUnit = TextUnit.Unspecified,//文本间距
    textDecoration: TextDecoration? = null,//下划线。删除线
    textAlign: TextAlign? = null, //对齐方式
    lineHeight: TextUnit = TextUnit.Unspecified, //行间距
    overflow: TextOverflow = TextOverflow.Clip,//文本溢出时的效果
    softWrap: Boolean = true, //控制文本是否可以换行
    maxLines: Int = Int.MAX_VALUE,//文本最多几行
    onTextLayout: (TextLayoutResult) -> Unit = {},//文本变化回调
    style: TextStyle = LocalTextStyle.current//绘制文本字体样式
) 

1.style 字体样式

Text(
            text = "Hello World!",
            style = TextStyle(fontSize = 25.sp,
            fontWeight = FontWeight.Bold,
            background = Color.Cyan,
            lineHeight = 35.sp,  //行高
            letterSpacing = 4.sp, //字间距
            textDecoration = TextDecoration.LineThrough
            )
        )
        Text(
            text = "Hello World!",
            style = MaterialTheme.typography.h6.copy(fontStyle = FontStyle.Italic)
        )

Text(
            text = "Hello World!",
            style = MaterialTheme.typography.h6.copy(fontStyle = FontStyle.Italic)
        )
        Text(
            text = "你好,我正在学习compose。你好,我正在学习compose。你好,我正在学习compose。你好,我正在学习compose。",
            maxLines = 1,
            style = MaterialTheme.typography.body1,
            overflow = TextOverflow.Ellipsis //设置行数1,超过一行结尾使用...
        )

2.fontFamily 字体风格

 //字体
        Text(text = "hello")
        //使用自定义字体需要在res中创建font文件夹,字体文件放入进去
        Text(text = "hello", fontFamily = FontFamily.Cursive)

3.AnnotatedString 多样式字体。 文字局部内容格式突出显示。

 val scope = rememberCoroutineScope()
//snakerbar状态
 val snackbarHostState = remember { SnackbarHostState() }
 //文本
 var text = buildAnnotatedString {
        withStyle(
            style = SpanStyle(fontSize = 18.sp, color = Color.Red)
        ) {
            append("请点击")
        }
        //设置部分内容可以点击,开头
        pushStringAnnotation(tag = "url", annotation = "https://www.baidu.com")
        withStyle(
            style = SpanStyle(
                color = Color.Blue,
                fontStyle = FontStyle.Italic,
                textDecoration = TextDecoration.LineThrough
            )
        ) {
            append("用户协议")
        }
        //设置部分内容可以点击,结尾
        pop()
    }


 Text(text = text )
//设置点击事件,可以点击的文字
 ClickableText(text = text, style = TextStyle(fontSize = 29.sp, color = Color.Yellow),
                onClick = {
                    text.getStringAnnotations(
                        start = it,
                        end = it,
                        tag = "url"
                    ).firstOrNull()?.let {
                        scope.launch {
                            //显示snackbar代替dialog  
                            snackbarHostState.showSnackbar(it.item)
                        }
                    }

                })
   //snakerbar显示位置
  SnackbarHost(hostState = snackbarHostState)

4.SelectionContainer。 设置文本可以复制

 SelectionContainer {
            Text(text = "hello")
        }

猜你喜欢

转载自blog.csdn.net/sinat_35541927/article/details/128088383