首页 > 解决方案 > 如何在单个文本中包含多种字体

问题描述

我在 pdf 文件中有一个阿拉伯文本。当我将文本复制到 Text Widget 时,它会变成奇怪的字符。当我检查 pdf 文件属性时,我发现它使用 HQPB1、HQPB2、HQPB3、HQPB4 字体,所以我将所有这些字体导入到我的 pubsec.yaml 文件中。问题是我一次只能使用这 4 种字体中的一种,但 pdf 文件同时使用所有这 4 种字体。

这是pdf的原文

当我只添加 HQPB1.ttf

当我只添加 HQPB2.ttf

因此,我想在 Text 中包含所有这 4 种字体,以便在需要时使用每种单独的字体,就像 pdf 一样。

标签: flutterdart

解决方案


RichText(
  text: TextSpan(
    children: <TextSpan>[
      TextSpan(text: 'Hello ', style: TextStyle(fontFamily: "Serif", fontSize: 30, color: Colors.black)),
      TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold, color: Colors.blue, fontSize: 30)),
      TextSpan(text: ' world!', style: TextStyle(fontFamily: "Roboto", fontSize: 30, color: Colors.red)),
    ],
  ),
)

输出:

在此处输入图像描述


推荐阅读