How to Change Navigationbar Font in Swift

change navigation bar title font - swift

Try this:

Objective-C

[[UINavigationBar appearance] setTitleTextAttributes:attrsDictionary];

Swift 3

self.navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "CaviarDreams", size: 20)!]

Swift 4

self.navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "CaviarDreams", size: 20)!]

How do I change navigationBar font in Swift?

Try this:

self.navigationController.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "CaviarDreams", size: 20)!]

Edit: Now, UIFont must be unwrapped to be able to be used here.

Swift 5 (+ safe handling of optional UIFont)

self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont(name: "Caviar-Dreams", size: 20) ?? UIFont.systemFont(ofSize: 20)]

Navigation Bar title font problem on ios 13

iOS 13.+ has UINavigationBarAppearance approach to customize NavigationBar-Title & NavigationBar-BarButtonItems

Check this code, might help you

    let titleFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.white ]
let barButtonFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 14)! ]

UINavigationBar.appearance().tintColor = UIColor.white // bar icons

if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .red // If you want different nav background color other than white

appearance.titleTextAttributes = titleFontAttrs
appearance.largeTitleTextAttributes = titleFontAttrs // If your app supports largeNavBarTitle

UINavigationBar.appearance().isTranslucent = false

appearance.buttonAppearance.normal.titleTextAttributes = barButtonFontAttrs
appearance.buttonAppearance.highlighted.titleTextAttributes = barButtonFontAttrs

UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
UINavigationBar.appearance().barTintColor = .red // bar background

UINavigationBar.appearance().titleTextAttributes = titleFontAttrs

UINavigationBar.appearance().isTranslucent = false

UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .highlighted)
}

How to customize navigation bar title font and size in UINavigationBar?

You should use a UILabel with an attributed title as a custom title view for your navigation bar.

let titleLabel = UILabel()

//attributes for the first part of the string
let firstAttr: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 16),
.foregroundColor: UIColor.blue]

//attributes for the second part of the string
let secondAttr: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 16),
.foregroundColor: UIColor.red]

//initializing and merging the two parts
let attrString = NSMutableAttributedString(string: "Navigation", attributes: firstAttr)
let secondAttrString = NSAttributedString(string: " Bar Title", attributes: secondAttr)
attrString.append(secondAttrString)

//setting the attributed string as an attributed text
titleLabel.attributedText = attrString

//set the size of the label to fit its contents
titleLabel.sizeToFit()

//setting the label as the title view of the navigation bar
navigationItem.titleView = titleLabel

Result:

navigation bar with custom title view

iOS change navigation bar title font and color

The correct way to change the title font (and color) is:

[self.navigationController.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont fontWithName:@"mplus-1c-regular" size:21]}];

Edit: Swift 4.2

self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedString.Key.foregroundColor: UIColor.red,
NSAttributedString.Key.font: UIFont(name: "mplus-1c-regular", size: 21)!]

Edit: Swift 4

self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedStringKey.foregroundColor: UIColor.red,
NSAttributedStringKey.font: UIFont(name: "mplus-1c-regular", size: 21)!]

Swift 3:

self.navigationController?.navigationBar.titleTextAttributes = 
[NSForegroundColorAttributeName: UIColor.redColor(),
NSFontAttributeName: UIFont(name: "mplus-1c-regular", size: 21)!]

Swift 5:

navigation.navigationBar.titleTextAttributes = [
.foregroundColor: UIColor.red,
.font: UIFont(name: "mplus-1c-regular", size: 21)!
]

Changing the font of a Large Titled Navigation Bar in Swift

Please check the images
how to add font in <code>info.plist</code>

Fist make sure that your font is .ttf or .otf format

1) Import your font into the project
2) Add new key "Fonts provided by application" on application's info.plist file and add your font names Now you can use custom fonts with interface builder or programatically

navigationController?.navigationBar.largeTitleTextAttributes =
[NSAttributedStringKey.foregroundColor: UIColor.blue,
NSAttributedStringKey.font: UIFont(name: "your font name", size: 30) ??
UIFont.systemFont(ofSize: 30)]

How can I change the font and color of the Navigation Bar back button

It's a UIBarButtonItem so set this property.

init() {
UIBarButtonItem.appearance().tintColor = .red
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10)], for: .normal)
}

Or you can use this also,

init() {
let standard = UINavigationBarAppearance()

let button = UIBarButtonItemAppearance(style: .plain)
button.normal.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.red]
standard.buttonAppearance = button
}

Changing navigation title programmatically

You change the title by changing the title of the view controller being displayed:

viewController.title = "some title"

Normally this is done in view did load on the view controller:

override func viewDidLoad() {
super.viewDidLoad()
self.title = "some title"
}

However, this only works if you have your view controller embedded in a UINavigationController. I highly recommend doing this instead of creating a navigation bar yourself. If you insist on creating a navigation bar yourself, you can change the title by doing:

navigationBar.topItem.title = "some title"

How to set the font size of navigation bar title

Add these lines to your viewDidLoad() method:

self.navigationController?.navigationBar.topItem?.title = "Your title" 
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Your Font", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()]


Related Topics



Leave a reply



Submit