我正在尝试使用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中。所以我尝试使用这个答案,但它会崩溃,我猜是因为我没有找到
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))
但这样做只使用了最后一个属性,这正是我所期望的。
有没有其他方法可以使用,使得加粗和斜体都显示在标签的同一部分? 编辑 我想要包含项目中的字体,以便清楚地表明我已经导入它们。第二张图片是故事板上字体列表。虽然被遮盖了,但实际上是
Clear Sans
字体族。
![Installed fonts](https://istack.dev59.com/E5u6O.webp)
![Storyboard options](https://istack.dev59.com/mWUvw.webp)