Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UINavigationBar font properties?

I have an UINavigationBar added to my UIViewController view. I want to change the fonts properties. Note that I want to change a UINavigationBar not controller. In my app where I use UINavigationController I use self.navigationItem.titleView = label; to display the custom label.

How can I accomplish to have a custom title in my UINavigationBar?

P.S I use this to set the title text self.navBar.topItem.title = @"Information"; of my UINavigationBar.

like image 680
Jacob Avatar asked Jan 08 '12 01:01

Jacob


People also ask

How do I change the font style of the navigation bar title in Swift?

let navigation = UINavigationBar. appearance() let navigationFont = UIFont(name: "Custom_Font_Name", size: 20) let navigationLargeFont = UIFont(name: "Custom_Font_Name", size: 34) //34 is Large Title size by default navigation.

How do I change the font in SwiftUI?

Hold the command key and click the text to bring up a pop-over menu. Choose Show SwiftUI Inspector and then you can edit the text/font properties.

How do I change the navigation bar title color in SwiftUI?

To change a navigation bar color in SwiftUI, you apply toolbarBackground modifier to the content view of NavigationStack . NavigationView is deprecated in iOS 16. toolbarBackground accepts two parameters. ShapeStyle : The style to display as the background of the bar.

How do I use the navigation bar in Xcode?

Start with Navigation ControllerCreate a single view application in Xcode. Add two view controller into your storyboard. Create two different swift files for those view controllers and set identifiers for them. Take a button in each view controller, set constrain for them and customize as you want.


2 Answers

From iOS 5 onwards we have to set title text color and font of navigation bar using titleTextAttribute Dictionary(predefined dictionary in UInavigation controller class reference).

[[UINavigationBar appearance] setTitleTextAttributes:      [NSDictionary dictionaryWithObjectsAndKeys:          [UIColor blackColor], NSForegroundColorAttributeName,             [UIFont fontWithName:@"ArialMT" size:16.0], NSFontAttributeName,nil]]; 

The below tutorial is the best tutorial for customization of UIElements like UInavigation bar, UIsegmented control, UITabBar. This may be helpful to you

http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

like image 190
Sandeep Avatar answered Oct 05 '22 22:10

Sandeep


(This is not possible using the new iOS 5.0 Appearance API).

Edit:

iOS >= 5.0 :

Set the Title Text Attributes for the Navigationbar:

// Customize the title text for *all* UINavigationBars NSDictionary *settings = @{     UITextAttributeFont                 :  [UIFont fontWithName:@"YOURFONTNAME" size:20.0],     UITextAttributeTextColor            :  [UIColor whiteColor],     UITextAttributeTextShadowColor      :  [UIColor clearColor],     UITextAttributeTextShadowOffset     :  [NSValue valueWithUIOffset:UIOffsetZero]};  [[UINavigationBar appearance] setTitleTextAttributes:settings]; 

iOS < 5.0

UINavigationItem doesn't have a property called label or something, only a titleView. You are only able to set the font by setting a custom label as this title view You could use the following code: (as suggested here)

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)]; label.font = [UIFont fontWithName:@"YOURFONTNAME" size:20.0]; label.shadowColor = [UIColor clearColor]; label.textColor =[UIColor whiteColor]; label.text = self.title;   self.navigationItem.titleView = label;       [label release]; 
like image 31
Tieme Avatar answered Oct 05 '22 21:10

Tieme