32

我对如何在 iOS 项目的文本中使用 SF 符号感到困惑。我有一个使用符号的 UIButton,使用UIImage systemImageNamed:. 我想在另一个视图中显示有关该按钮的消息和一些文本。我认为我应该能够使用该符号的 Unicode 引用,但它不会呈现。我的理解是这些符号在私人使用区域,但我不能让它们使用代码来呈现@"Press the \U0010057C button."

我尝试将 SFSymbolsFallback.ttf 字体文件导入我的项目。

我希望看到呈现的符号,但我得到一个?反而。

4

2 回答 2

63

这对我有用:

let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(systemName: "checkmark.circle")

// If you want to enable Color in the SF Symbols.
imageAttachment.image = UIImage(systemName: "checkmark.circle")?.withTintColor(.blue)

let fullString = NSMutableAttributedString(string: "Press the ")
fullString.append(NSAttributedString(attachment: imageAttachment))
fullString.append(NSAttributedString(string: " button"))
label.attributedText = fullString

结果:

在此处输入图像描述

于 2019-10-11T12:25:21.780 回答
9

我一直在努力解决如何使用 CoreText 渲染 API 使用 SF Symbol 字形的同样问题。在 MacOS 上,“SF Pro Text”字体中有 7500 多个字形,包括 SF Symbol 字形。在 iOS 上,系统字体只有约 2800 个字形,并且似乎不包括新的 SF Symbol 字形。除了在项目本身中包含字体(我相信这违反了 Apple 的许可)之外,我还没有找到一种方法来使用它们的 Unicode 字符值来呈现这些字形。

于 2019-12-04T17:11:42.033 回答