text = value, overflow = TextOverflow.Ellipsis, maxLines = 1 )
Clip将溢出的部分裁剪Ellipsis使用省略号表示溢出部分Visible指定范围内没有足够的空间。也要显示所有文本
关于最后一个Visible 在官网中可以找到示例去演示器效果。笔者这边简化了一下。示例如下。
Box(modifier = Modifier .size(300.dp, 150.dp) .background(Color.Red)) { Text( text = “Hello World”.repeat(2), modifier = Modifier .size(200.dp, 70.dp) .background(Color.Yellow), fontSize = 35.sp, overflow = TextOverflow.Visible, ) }
未设置Visible |
设置了Visible |
换行处理
Text( text = value2, softWrap = false )
false被定位为有无限的水平空间true默认会有边界
onTextLayout
计算新的文本布局时执行的回调.预览是不打印的。只有运行才会打印
@Preview(showBackground = true) @Composable fun SimpleText7() { val value = “hello world” Column(Modifier.width(200.dp)) { Text( text = value, onTextLayout = { Log.i(“TAGText”,it.toString()) } ) } } 运行以后的结果,可以看到所有的属性都被打印出来了 2021-04-14 11:40:50.016 16830-16830/com.starot.pencase_compose I/TAGText: TextLayoutResult(layoutInput=TextLayoutInput( text=hello world, style=TextStyle(color=Color(0.0, 0.0, 0.0, 1.0, sRGB IEC61966-2.1), fontSize=Unspecified, fontWeight=null, fontStyle=null, fontSynthesis=null, fontFamily=null, fontFeatureSettings=null, letterSpacing=Unspecified, baselineShift=null, textGeometricTransform=null, localeList=null, background=Color(0.0, 0.0, 0.0, 0.0, None), textDecoration=null, shadow=null, textAlign=null, textDirection=null, lineHeight=Unspecified, textIndent=null), placeholders=[], maxLines=2147483647, softWrap=true, overflow=Clip, density=DensityImpl(density=2.75, fontScale=1.0), layoutDirection=Ltr, resourceLoader=androidx.compose.ui.platform.AndroidFontResourceLoader@7058a4b, constraints=Constraints(minWidth = 0, maxWidth = 550, minHeight = 0, maxHeight = 1977)), multiParagraph=androidx.compose.ui.text.MultiParagraph@e80ec28, size=184 x 52, firstBaseline=41.0, lastBaseline=41.0, placeholderRects=[])
文字样式 🔥
style属性的TextStyle类中。有很多就可以在Text的构造方法中去执行。但是依然有一些特殊的属性
背景颜色
style = TextStyle( background = Color.Red )
基线偏移
Text( text = value, style = TextStyle( baselineShift = BaselineShift.Subscript ) )
说明这个属性之前,要明白什么是基线。用hencoder中描述
每只小鸟的最高点和最低点都不一样,但画面很平衡,而这个用来让所有文字互相对齐的基准线,就是基线(
baseline)
Android Text 的基线baseline
实际运行效果呢?
笔者这边选择了 3个参数进行演示。
BaselineShift给我们提供了3个默认的选项
val Superscript = BaselineShift(0.5f)val Subscript = BaselineShift(-0.5f)val None = BaselineShift(0.0f)
合成字体
fontSynthesis = FontSynthesis.All
合成字体用于指定当使用的FontFamily不包含粗体或斜体时,系统是否应该伪造粗体或倾斜字形。
None关闭字体合成。 如果FontFamily中不存在粗体和斜体,则不会合成它们Weight如果FontFamily中不提供,则仅合成粗体。 倾斜的字体将不会被合成。Style如果FontFamily中不可用,则仅合成倾斜的字体。 粗体字体将不被合成。All如果FontFamily中不提供粗体和倾斜字体,则系统会合成粗体和倾斜字体
文字缩进
Text( text = “hello world”.repeat(2), style = TextStyle( textIndent = TextIndent(10.sp,10.sp) ) ) class TextIndent( //第一行的缩进 val firstLine: TextUnit = 0.sp, //除了第一行其他行的缩进 val restLine: TextUnit = 0.sp )
文字方向
style = TextStyle( textDirection = TextDirection.Ltr )
一般情况下。我们用到的都是从左往右。也有一些国家的语言是从右往左,例如阿拉伯语
Ltr从左往右Rtl从右往左
下面的几个类型都是根据Unicode双向算法,文本方向取决于文本中的第一个强方向字符。适用于文本中有两种不同的文字。
关于Unicode双向算法可以查看 Unicode控制字符
ContentContentOrLtrContentOrRtl
字体阴影
style = TextStyle( shadow = Shadow( color = Color.Red, offset = Offset.Zero, blurRadius = 20.0f ) )
color背景颜色
blurRadius模糊半径
offset偏移
几何变换
scaleX水平缩放 默认是1f 不缩放
style = TextStyle( textGeometricTransform = TextGeometricTransform( scaleX = 1f, skewX = 0f ) )
skewX倾斜 默认是0f 不倾斜