Text - TextStyle
文本的样式配置,如颜色、字体、行高等和 Text
组件一样。
一般属性查看:Jectpack-Compose 基础组件 : Text
下面介绍不一样的属性。
fontSynthesis: FontSynthesis?
在提供的自定义字体系列中找不到请求的字体粗细或样式时,是否合成字体粗细和/或样式。
看下 FontSynthesis
enum class FontSynthesis {
/**
* 关闭字体合成。如果[FontFamily]中不存在粗体或倾斜的面,则不会合成它们
*/
None,
/**
* 如果[FontFamily]中没有粗体字体,则只合成粗体字体。不会合成倾斜字体。
*/
Weight,
/**
* 如果[FontFamily]中没有粗体字体,则只合成粗体字体。不会合成倾斜字体。
*/
Style,
/**
* 如果[FontFamily]中没有粗体和倾斜字体,则系统会综合这两种字体
*/
All;
internal val isWeightOn: Boolean
get() = this == All || this == Weight
internal val isStyleOn: Boolean
get() = this == All || this == Style
}
shadow: Shadow?
应用于文本的阴影效果。
看下 Shadow
class Shadow(
// 阴影颜色
@Stable
val color: Color = Color(0xFF000000),
// 偏移量
@Stable
val offset: Offset = Offset.Zero,
// 阴影半径
@Stable
val blurRadius: Float = 0.0f
)
示例代码
val textStyle = TextStyle(
shadow = Shadow(color = Color.Red, offset = Offset(0f, 2f), blurRadius = 5f),
)
Text(
text = "你好 $name!",
style = textStyle,
)
实际效果
Offset
函数
Offset(x: Float, y: Float)
同安卓坐标系一样,左上角为0点
x:为x轴偏移量。正数往右偏移,负数往左偏移
y:为y轴偏移量。正数往下偏移,正数往上偏移
Offset(-2f, -2f)
实际效果:
Offset(2f, 2f)
实际效果:
textIndent: TextIndent?
指定段落的缩进。
参数:
firstLine—应用于第一行的缩进量。
restLine—应用于除第一行以外的每行的缩进量。
class TextIndent(
val firstLine: TextUnit = 0.sp,
val restLine: TextUnit = 0.sp
)
效果展示:
val textStyle = TextStyle(
textIndent = TextIndent(40.sp, 20.sp)
)
Text(
text = "你好 $name!你好 $name!你好 $name!你好 $name!你好 $name!你好 $name!你好 $name!你好 $name!你好 $name!你好 $name!",
style = textStyle,
)
textGeometricTransform: TextGeometricTransform?
几何变换。
/**
* 定义文本的几何变换。
* 参数:
* scaleX-文本在水平方向上的比例。默认值为1.0f,即无缩放。
* skewX-文本在水平方向上的剪切。(x,y)处的像素(y是基线上方的距离)将转换为(x+y*skewX,y)。默认值为0.0f,即无倾斜。
*/
@Immutable
class TextGeometricTransform(
val scaleX: Float = 1.0f,
val skewX: Float = 0f
)
先试试 scaleX
的效果:
val textStyle = TextStyle(
textGeometricTransform = TextGeometricTransform(2f, 0f)
)
Text(
text = "你好 $name",
style = textStyle,
)
文字被拉长变扁了,变胖了
val textStyle = TextStyle(
textGeometricTransform = TextGeometricTransform(0.5f, 0f)
)
Text(
text = "你好 $name",
style = textStyle,
)
文字被挤扁了~~,变瘦了
scaleX
的默认值为 1.0f
综上两个测试可以得出: scaleX > 1.0f
时文字会拉长, scaleX < 1.0f
时会挤扁~~
再试试 skewX
:
val textStyle = TextStyle(
textGeometricTransform = TextGeometricTransform(1f, 1f)
)
Text(
text = "你好 $name",
style = textStyle,
)
val textStyle = TextStyle(
textGeometricTransform = TextGeometricTransform(1f, 1f)
)
Text(
text = "你好 $name",
style = textStyle,
)
skewX
的默认值为 0f
综上两个测试可以得出: skewX > 0f
时文字会向左倾斜,并且若倾斜后文字超出 Text
容器的边缘时会被裁切, skewX < 1.0f
时会向右倾斜,并且若倾斜后文字超出 Text
容器的边缘时会被裁切