首先来看一些基础的参数:
Text(text = "hello world", fontSize = 30.sp)//设置字体大小
Text(
text = stringResource(id = R.string.hello_world),//设置为资源中的文字
color = colorResource(id = R.color.purple_500)//设置字体颜色
)
Text(text = "HelloWorld", fontStyle = FontStyle.Italic)//设置文字斜体
Text(text = "Hello World", fontWeight = FontWeight.Bold)//文字粗体
Text(
text = "Hello World",
textAlign = TextAlign.Center,//文字居中对齐
modifier = Modifier
.width(150.dp)//控件总宽度
.background(color = colorResource(id = R.color.teal_200))//控件背景色
)
Text(text = "HelloWorld", fontFamily = FontFamily.Serif)//设置字体样式为Serif
设置Text行数及文字溢出
Text("Hello".repeat(50), maxLines = 1)//设置行数上线,超出直接截断
Text(
"Hello".repeat(50),
maxLines = 1,//设置最大行数
color = colorResource(id = R.color.purple_200),
overflow = TextOverflow.Ellipsis//文字溢出,文末显示...
)
多样式配置Text:
1,Text中个别文字字体及颜色设置
Text(buildAnnotatedString {//设置多样式字体
withStyle(style = SpanStyle(color = Color.Blue)) {
append("H")
}
append("ello ")