文字处理
文字处理
如何为 Text Widget 设置自定义字体?
在 Android SDK 中(从 Android O 开始),你可以创建一个字体资源文件并将其传给 TextView 的 FontFamily 参数。
在 Flutter 中,将字体文件放入一个文件夹,并在 pubspec.yaml 文件中引用它,就和导入图片一样。
fonts:
- family: MyCustomFont
fonts:
- asset: fonts/MyCustomFont.ttf
- style: italic
然后将字体赋值给你的 Text Widget:
@overrideWidget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Sample App"), ), body: Center( child: Text( 'This is a custom font text', style: TextStyle(fontFamily: 'MyCustomFont'), ), ), );}
如何更改 Text Widget 的样式?
除了字体,你还可以自定义 Text Widget 的其它样式元素。Text Widget 的样式参数接收一个 TextStyle 对象,你可以在这个对象里自定义很多参数,例如:
- color
- decoration
- decorationColor
- decorationStyle
- fontFamily
- fontSize
- fontStyle
- fontWeight
- hashCode
- height
- inherit
- letterSpacing
- textBaseline
- wordSpacing
