如何将自定义字体和权重(样式)设置为UInavigationbar标题

时间:2016-11-09 07:30:45

标签: swift uinavigationbar custom-font

如何为UINavigationBar标题设置自定义字体名称和自定义字体粗细(字体样式)?

3 个答案:

答案 0 :(得分:1)

UIFontDescriptor用作Font NameFont Size

UIFontDescriptor.fontDescriptorWithSymbolicTraits字体样式(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()]
  

可能是性能问题。

答案 1 :(得分: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 :(得分:-1)

快速4倍:

let newFont = UIFont(名称:“ FontName”,大小:15) newFont?.fontDescriptor.withSymbolicTraits(.traitBold)