Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uppercase string for all UINavigation bar titles

Currently, I am changing the font of the navigation bar using the following in the AppDelegate:

[[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIFont fontWithName:@"..." size:...], NSFontAttributeName,
  nil]];

Is there a way to do the same to make sure that the string is capitalized globally?

like image 267
p0lAris Avatar asked Dec 30 '14 22:12

p0lAris


1 Answers

It's not possible to achieve this using NSAttributedString, but instead you can change the title in your base view controller class. Try this:

/// Base class for all view controllers
class BaseViewController: UIViewController {

    private var firstWillAppearOccured = false

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if !firstWillAppearOccured {
            viewWillFirstAppear(animated)
            firstWillAppearOccured = true
        }
    }

    /// Method is called when `viewWillAppear(_:)` is called for the first time
    func viewWillFirstAppear(_ animated: Bool) {
        title = title?.uppercased() // Note that title is not available in viewDidLoad()
    }
}
like image 125
boa_in_samoa Avatar answered Oct 23 '22 05:10

boa_in_samoa