如何定制UINavigationBar标题的AttributedText
Swift 4.2版本:
let titleLbl = UILabel()
let titleLblColor = UIColor.blue
let attributes: [NSAttributedStringKey: Any] = [NSAttributedStringKey.font: UIFont(name: "Noteworthy-Bold", size: 30)!, NSAttributedStringKey.foregroundColor: titleLblColor]
titleLbl.attributedText = NSAttributedString(string: "My Title", attributes: attributes)
titleLbl.sizeToFit()
self.navigationItem.titleView = titleLbl
将会得到一个下面这样的导航栏标题:
请记住在属性字典中的NSAttributedStringKey.font
属性遵循以下格式:
NSAttributedStringKey.font: UIFont(name: "FontNameILike-FontStyleILike", size: 30)!
FontName可以是iOS SDK中任何可用的字体(我使用了Noteworthy),一些标准的字体样式(我使用了Bold)如下:
Bold, BoldItalic, CondensedBlack, CondensedBold, Italic, Light, LightItalic, Regular, Thin, ThinItalic, UltraLight, UltraLightItalic
我希望以下博客文章也会有所帮助(截至2018年9月19日仍然有效)https://medium.com/@dushyant_db/swift-4-recipe-using-attributed-string-in-navigation-bar-title-39f08f5cdb81