1.基本属性
text: String, //文本内容
//text: AnnotatedString, //多样式文字
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, //控制文本是否能够换行,如果为false,则会截断
maxLines: Int = Int.MAX_VALUE, //最大行数
onTextLayout: (TextLayoutResult) -> Unit = {}, //在文本发生变化之后,回调此方法
style: TextStyle = LocalTextStyle.current //文本的风格配置,如颜色、字体、行高等
package com.example.composetest
import android.graphics.Typeface
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.PlaceholderVerticalAlign
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.composetest.ui.theme.ComposeTestTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NewsStory()
}
}
}
@Preview(showBackground = true)
@Composable
fun NewsStory() {
Column {
val annotatedString = buildAnnotatedString {
withStyle(style = SpanStyle(fontSize = 14.sp, fontStyle = FontStyle.Italic, color = Color.Red, fontWeight = FontWeight.Bold)) {
append("Compose Text ")
}
append("通过 AnnotatedString 设置富文本效果!")
withStyle(style = SpanStyle(fontStyle = FontStyle.Italic, color = Color.Blue)) {
append("设置富文本效果!")
}
}
val annotatedString2 = buildAnnotatedString {
appendInlineContent(id="txt")
withStyle(style = SpanStyle(fontStyle = FontStyle.Italic, color = Color.Red)) {
append("Compose Text ")
}
append("通过 AnnotatedString 设置富文本效果!")
withStyle(style = SpanStyle(fontStyle = FontStyle.Italic, color = Color.Blue)) {
append("设置富文本效果!")
}
}
/**
* 普通效果
*/
Text(text = "Hello Android")
/**
* 富文本效果
*/
Text( text = annotatedString)
Text(
text = annotatedString2, inlineContent = mapOf("txt" to InlineTextContent(
Placeholder(20.sp, 20.sp, PlaceholderVerticalAlign.Bottom)
) {
Image(
painter = painterResource(id = R.mipmap.icon_red),
modifier = Modifier.width(20.dp).height(20.dp),
contentDescription = ""
)
}
)
)
Text(text = "TextDecoration.None", textDecoration = TextDecoration.None)
Text(text = "TextDecoration.LineThrough", textDecoration = TextDecoration.LineThrough)
Text(text = "TextDecoration.Underline", textDecoration = TextDecoration.Underline)
Text(
text = "Modifier.background(brush, shape, alpha)",
modifier = Modifier.background(
brush = Brush.linearGradient(
colors = listOf(
colorResource(id = R.color.teal_200),
colorResource(id = R.color.teal_700),
colorResource(id = R.color.purple_200),
colorResource(id = R.color.purple_500)
)
),
shape = RectangleShape,
alpha = 0.8f
)
)
Text(
text = "最大两行结尾省略号...示例这里示例这里示例这里示例这里示例这里示例这里示例这里示例这里示例这里示例这里示例这里示例这里示例",
maxLines = 2,//最大行数
overflow = TextOverflow.Ellipsis
)
Text(
text = "Modifier.background(width,color, shape) 设置边框",
modifier = Modifier.border(
width = 1.dp,
color = colorResource(id = R.color.teal_700),
shape = RectangleShape
)
)
}
}
}
最后加个图参照
文字、文字加粗、颜色、字号、左侧加图标、下划线、中划线、背景、限制行数、边框
image.png