富文本中添加字体选项功能
A little extra help for styling your text
样式设置方面的一些额外帮助
I recently learned about a little known corner of the Flutter world: Font Features. I’ll share that with you today. Keep it in the back of your mind and when you need it, just do a search for Flutter Font Features. You’ll probably end up back here because there isn’t much out there about Font Features except for the documentation, and even that is a little sparse.
我最近了解了Flutter世界的一个鲜为人知的角落:字体功能。 今天我将与您分享。 记住它,当您需要时,只需搜索Flutter Font Features即可 。 您可能最终会回到这里,因为除文档外,关于Font Features的内容并不多,甚至还很少。
更改字体 (Changing the font)
This article isn’t primarily about changing the font family, but since it is related to fonts, I’ll start by briefly reviewing how to use different fonts in your app.
本文主要不是改变字体,但由于它关系到字体,我将简要回顾如何在你的应用程序中使用不同的字体启动。
系统字体 (System fonts)
The default font on Android is Roboto
and on iOS it is .SF UI Display
or .SF UI Text
(SF meaning San Francisco). If you want to use a different font, then you will need to add it to your app.
Android上的默认字体是Roboto
,而iOS上的默认字体是.SF UI Display
或.SF UI Text
(SF表示旧金山)。 如果要使用其他字体,则需要将其添加到您的应用程序中。
![Image for post](https://miro.medium.com/max/9999/1*DFu_n7dV6kHUD7mxuUm1HQ.png)
![Image for post](https://miro.medium.com/max/9999/1*8SKnrg5fkUuTh-9itODdTg.png)
They look the same to me.
他们对我来说看起来一样。
您自己的自定义字体 (Your own custom fonts)
I’ve written before about how to add your own custom font to your project, so I won’t go into detail, but the basic process is the following:
之前我已经写过关于如何向您的项目中添加自己的自定义字体的文章,因此我将不做详细介绍,但是其基本过程如下:
- Add a font to your assets folder. 将字体添加到资产文件夹。
2. Register the font in pubspec.yaml.
2.在pubspec.yaml中注册字体。
flutter:
fonts:
- family: MyFont
fonts:
- asset: assets/my_font.ttf
3. Use the font in your code by specifying the fontFamily
.
3.通过指定fontFamily
在代码中使用字体。
Text(
'Hello world',
style: TextStyle(
fontFamily: 'MyFont',
),
)
![Image for post](https://miro.medium.com/max/9999/1*KsOqnmo9eC8whIsOK8QHtA.png)