How to set custom font and weight (style) to UInav

2019-07-27 05:19发布

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

3条回答
女痞
2楼-- · 2019-07-27 05:52

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!

查看更多
何必那么认真
3楼-- · 2019-07-27 05:53

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.

查看更多
狗以群分
4楼-- · 2019-07-27 05:53

Swift 4x:

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

查看更多
登录 后发表回答