将图像合并为文本的一部分

Incorporate an image as part of text

是否可以用图像创建 text/font 并将其用作字符串的一部分?我想在我的应用程序的各个位置的标签等数量之前显示我们的硬币图像。因此,例如,不是 $4.00,而是我的自定义货币图像而不是 $ 符号。

我想知道是否有办法将我们的货币图像转换成某种字体文本(就像 font awesome 所做的那样)并在字符串中使用它。

如果不是,有什么好的方法可以解决这个问题,以便我们可以使用我们自己的自定义货币图像在整个应用程序中显示货币金额?某种自定义标签?

您可以使用 NSAttributeString 通过 NSTextAttachment

来完成此操作
let dollarImage = UIImage(named: "DollarSign")

let attachment = NSTextAttachment()
attachment.image = dollarImage
attachment.bounds = CGRect(origin: .zero, size: dollarImage.size)

let attachmentString = NSAttributedString(attachment: attachment)
let currencyString = NSAttributedString(string: "4.40")

let attributedString = NSMutableAttributedString(attributedString: attachmentString)
attributedString.append(currencyString)