首页 文章

设置标签字体用粗体和斜体

提问于
浏览
3

我正在尝试将我的标签的特定部分设置为粗体和斜体 NSMutableAttributedString ,但我只能得到一个或另一个 . 故事板上的'm using is Clear Sans. Setting the entire label'字体到 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 ,但我不在那里't know why it' .

最后,我试图将两者“叠加”,就像这样

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))

但所有这一切都是使用最后一个属性,这是我所期望的 .

我可以使用其他方法来做到这一点,以便我在标签的同一部分显示粗体和斜体吗?

Edit

我想要包含项目中的字体,所以_1588557已经导入了它们 . 第二个图像是故事板上的字体列表 . 虽然它被掩盖了,但实际上这个家庭是 Clear Sans .

Installed fonts

Storyboard options

2 回答

  • 2

    确保在 UIAppFonts ("Fonts provided by application")键下的应用程序的Info.plist中还添加了粗体斜体版本 .

  • 0
    UIFont.italicSystemFont(ofSize: 15.0)
    UIFont.boldSystemFont(ofSize: 15)
    

    如果你想自定义喜欢系统的标签字体 . 你可以尝试我的解决方案 . 它适用于swift 3

相关问题