文本处理

文本处理

如何在文本(Text) widget 上设置自定义字体?

在 Xamarin.Forms 中,您必须在每个原生项目中添加自定义字体。然后在你的 元素 中,你会使用 filename#fontname 给 FontFamily 属性分配这个字体名,而在iOS中只使用 fontname 。

在 Flutter 中,将字体文件放在一个文件夹中,并在 pubspec.yaml 中引用它,这跟导入图像的方式类似。

fonts:
   - family: MyCustomFont
     fonts:
       - asset: fonts/MyCustomFont.ttf
       - style: italic

Then assign the font to your Text widget:

@overrideWidget build(BuildContext context) {  return new Scaffold(    appBar: new AppBar(      title: new Text("Sample App"),    ),    body: new Center(      child: new Text(        'This is a custom font text',        style: new TextStyle(fontFamily: 'MyCustomFont'),      ),    ),  );}

如何设置文本 widget 的样式?

除了字体,您还可以在文本 widget 上定制其他样式元素。文本 widget 的样式参数接受一个 TextStyle 对象,您可以在其中定制许多参数,比如:

  • color
  • decoration
  • decorationColor
  • decorationStyle
  • fontFamily
  • fontSize
  • fontStyle
  • fontWeight
  • hashCode
  • height
  • inherit
  • letterSpacing
  • textBaseline
  • wordSpacing