我正在尝试使用NSMutableAttributedString将标签的特定部分设置为粗体和斜体,但是我只能获得其中一个。我使用的字体是Clear Sans。将 Storyboard上的整个标签字体设置为Bold Italic效果很好,但我只需要其中的一部分。所以我用

for family in UIFont.familyNames() {
    print("\(family)\n\t\(UIFont.fontNamesForFamilyName(family))")
}

确保它在那里,我发现
Clear Sans
    ["ClearSans", "ClearSans-Bold", "ClearSans-Italic"]

由于某种原因,它不在这里,但在 Storyboard 中。因此,我尝试使用this answer,但是由于我找不到ClearSans-BoldItalic的相同原因,它崩溃了,但我不知道为什么它不存在。

最后,我尝试将两者“堆叠”在一起,就像这样
let bold = UIFont(name: "ClearSans-Bold", size: 20)
let italic = UIFont(name: "ClearSans-Italic", size: 20)

attributedCaption.addAttribute(NSFontAttributeName, value: bold!, range: (imageContainer.caption as NSString).rangeOfString(matchString))
attributedCaption.addAttribute(NSFontAttributeName, value: italic!, range: (imageContainer.caption as NSString).rangeOfString(matchString))

但是,所有这些操作都使用了last属性,这正是我期望的。

我可以使用其他方法来使标签的同一部分上的粗体和斜体都显示吗?

编辑

我想包含项目中的字体,因此很明显我已经导入了它们。第二个图像是 Storyboard 上的字体列表。虽然被掩盖了,但这个家庭实际上是Clear Sans

ios - 将标签字体设置为粗体和斜体-LMLPHP
ios - 将标签字体设置为粗体和斜体-LMLPHP

最佳答案

确保在UIAppFonts(“由应用程序提供的字体”)键下,还将粗体斜体版本也添加到了应用程序的Info.plist中。

关于ios - 将标签字体设置为粗体和斜体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36701956/

10-14 16:33
查看更多