flutter中⽂字间距的实现
在flutter没有直接设置text ⽂字间距的⽅法,可以通过text的StrutStyle属性的leading,加上Transform 偏移来实现。
1.设置StrutStyle的leading
final double leading = 0.9;
strutStyle:StrutStyle(forceStrutHeight: true, height: 1, leading: leading),
2.利⽤Transform做计算反⽅向位置偏移
child: anslate(
offset: Offset(0,-fontSize * leading / 2),
child: Text(
//⽂本
textContent,
//通过设置StrutStyle的leading,然后利⽤Transform做反⽅向位置偏移
strutStyle:
StrutStyle(forceStrutHeight: true, height: 1, leading: leading),
),
),
具体代码如下:
class TextLineHeightDemoPage extends StatelessWidget{
final double leading = 0.9;
final double fontSize = 16;
@override
Widget build(BuildContext context) {
/
/ TODO: implement buildtextstyle
return Scaffold(
appBar: AppBar(
title: Text("TextLineHeightDemoPage"),
),
body: Container(
color: Colors.blueGrey,
margin: EdgeInsets.all(20),
//利⽤ Transform 偏移将对应权重部分位置
child: anslate(
offset: Offset(0,-fontSize * leading / 2),
child: Text(
textContent,
//通过设置StrutStyle的leading,然后利⽤Transform做反⽅向位置偏移
strutStyle:
StrutStyle(forceStrutHeight: true, height: 1, leading: leading),
style: TextStyle(
fontSize: fontSize,
color: Colors.black,
//backgroundColor: Accent),
),
),
)
,
),
);
}
}
效果如图
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论