设置标签字体为粗体和斜体
Set label font with bold and italic
我正在尝试使用 NSMutableAttributedString
将标签的特定部分设置为粗体和斜体,但我只能得到其中之一。我使用的字体是 Clear Sans。将故事板上整个标签的字体设置为 Bold Italic
效果很好,但我只需要其中的一部分。所以我用了
for family in UIFont.familyNames() {
print("\(family)\n\t\(UIFont.fontNamesForFamilyName(family))")
}
为了确保它在那里,我找到了
Clear Sans
["ClearSans", "ClearSans-Bold", "ClearSans-Italic"]
出于某种原因,它不在此处,但在故事板中。所以我尝试使用 this answer,但它崩溃了,我假设出于与我没有找到 ClearSans-BoldItalic
相同的原因,但我不知道为什么它不在那里。
最后,我尝试只"stack"这两个,像这样
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
。
确保粗体斜体版本也添加到 UIAppFonts
("Fonts provided by application") 键下应用的 Info.plist。
UIFont.italicSystemFont(ofSize: 15.0)
UIFont.boldSystemFont(ofSize: 15)
如果你想像fond系统一样自定义标签字体。你可以试试我的解决方案。
它适用于 swift 3
我正在尝试使用 NSMutableAttributedString
将标签的特定部分设置为粗体和斜体,但我只能得到其中之一。我使用的字体是 Clear Sans。将故事板上整个标签的字体设置为 Bold Italic
效果很好,但我只需要其中的一部分。所以我用了
for family in UIFont.familyNames() {
print("\(family)\n\t\(UIFont.fontNamesForFamilyName(family))")
}
为了确保它在那里,我找到了
Clear Sans
["ClearSans", "ClearSans-Bold", "ClearSans-Italic"]
出于某种原因,它不在此处,但在故事板中。所以我尝试使用 this answer,但它崩溃了,我假设出于与我没有找到 ClearSans-BoldItalic
相同的原因,但我不知道为什么它不在那里。
最后,我尝试只"stack"这两个,像这样
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
。
确保粗体斜体版本也添加到 UIAppFonts
("Fonts provided by application") 键下应用的 Info.plist。
UIFont.italicSystemFont(ofSize: 15.0)
UIFont.boldSystemFont(ofSize: 15)
如果你想像fond系统一样自定义标签字体。你可以试试我的解决方案。 它适用于 swift 3