【问题标题】:How to include user fonts如何包含用户字体
【发布时间】:2020-11-22 17:26:35
【问题描述】:

所以我有一个 Flutter 应用程序,我在其中使用了来自 Google 字体的自定义字体。这些字体包含在fonts 文件夹中,我已经像这样在pubspec.yaml 中导入了,

fonts:
  - family: Source Sans Pro
  fonts:
    - asset: fonts/SourceSansPro-Regular.ttf
    weight: 400
    - asset: fonts/SourceSansPro-SemiBold.ttf
    weight: 600
    - asset: fonts/SourceSansPro-Bold.ttf
    weight: 700
    - asset: fonts/SourceSansPro-Black.ttf
    weight: 900 

但我希望用户能够添加自己的字体(TTf 文件)。我可以让他们选择 TTf 文件并将其保存在appsDocDirectory 中,之后如何在应用程序中使用该字体?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    试试这个:来自https://github.com/flutter/flutter/issues/55458

    Future<ByteData> loadFont(String path) async{
          File file = File(path);
          Uint8List bytes = await file.readAsBytes();
          return ByteData.view(bytes.buffer);
        }
        
        var custom = FontLoader('Pacifico');
        custom.addFont(loadFont("/storage/emulated/0/Download/Pacifico-Regular.ttf"));
        await custom.load();
        setState(() {});
    

    编辑:

    现在您的字体将在await custom.load(); 完成后以'Pacifico' 的形式提供

    【讨论】:

      猜你喜欢
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 1970-01-01
      • 2023-03-15
      • 2011-01-30
      • 2017-06-20
      • 1970-01-01
      相关资源
      最近更新 更多