如何自定义 UINavigationBar Title 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
会产生类似导航栏标题的波纹:
请记住 Attributes 字典中的 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