How to set custom font and weight (style) to UInavigationbar title

爱⌒轻易说出口 提交于 2019-12-12 23:35:42

问题


How to set custom font name and custom font weight (font style) for UINavigationBar title?


回答1:


Try this code. Swift 3x

This could be a better approach to set nanvigationBar title font and Style.

    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.ANY COLOUR, NSFontAttributeName:UIFont(name:"FontName-FontStyle", size: ANY SIZE)!]

Some Font Styles:

    Regular,Bold,BoldItalic,CondensedBlack,CondensedBold,Italic,Light,LightItalic,Thin,ThinItalic,UltraLight,UltraLightItalic

Note: Font style varies from font to font!




回答2:


Use UIFontDescriptor as Font Name and Font Size

UIFontDescriptor.fontDescriptorWithSymbolicTraits as font style (Bold, Italic, ...)

Swift 2.x:

let fontDec = UIFontDescriptor(name: "FontName", size: 20)
fontDec.fontDescriptorWithSymbolicTraits(.TraitBold)
let font = UIFont(descriptor: fontDec, size: 20)

self.navigationBar.titleTextAttributes = [
    NSFontAttributeName: font,
    NSForegroundColorAttributeName: UIColor.redColor()]

Maybe performance issue.




回答3:


Swift 4x:

let newFont = UIFont(name: "FontName", size: 15) newFont?.fontDescriptor.withSymbolicTraits(.traitBold)



来源:https://stackoverflow.com/questions/40501923/how-to-set-custom-font-and-weight-style-to-uinavigationbar-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!