Flutter中如何在Text组件使用Times New Roman字体?
在Flutter项目中使用Times New Roman字体的解决方案
因为Times New Roman是微软专有字体,并未收录在google_fonts库中,所以只能通过本地导入字体文件的方式实现,具体步骤如下:
准备字体文件
从系统字体目录中提取Times New Roman的相关文件(比如Windows系统的C:\Windows\Fonts下的times.ttf常规体、timesbd.ttf粗体、timesi.ttf斜体、timesbi.ttf粗斜体),将需要的文件复制到项目的assets/fonts/目录下(没有该目录则手动新建)。配置pubspec.yaml
在项目的pubspec.yaml文件中,找到flutter节点,添加字体配置:flutter: fonts: - family: TimesNewRoman fonts: - asset: assets/fonts/times.ttf weight: 400 - asset: assets/fonts/timesbd.ttf weight: 700 - asset: assets/fonts/timesi.ttf style: italic - asset: assets/fonts/timesbi.ttf weight: 700 style: italic注意保持缩进格式正确,配置完成后执行
flutter pub get使配置生效。在代码中使用字体
单个Text组件使用:Text( "这是Times New Roman字体的示例文本", style: TextStyle( fontFamily: "TimesNewRoman", fontSize: 18, fontWeight: FontWeight.w700, fontStyle: FontStyle.italic, ), )全局主题配置(所有文本默认使用该字体):
MaterialApp( theme: ThemeData( textTheme: const TextTheme( bodyLarge: TextStyle(fontFamily: "TimesNewRoman"), bodyMedium: TextStyle(fontFamily: "TimesNewRoman"), titleLarge: TextStyle(fontFamily: "TimesNewRoman"), // 按需配置其他文本样式 ), ), home: const YourHomePage(), )
内容的提问来源于stack exchange,提问作者Adhi
相关产品推荐
相关产品推荐

