Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing NavigationItem’s font

Tags:

iphone

I have used this code to change a NavigationItem’s title:

self.navigationItem.title = @"My Name is ABC";

How can I change the font size and font as well?

like image 290
Ankit Vyas Avatar asked May 05 '10 08:05

Ankit Vyas


People also ask

How do I change the navigation font in Item Swift?

Swift 3. Step 2 - Then again choose Custom and it will show all the fonts. 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.


3 Answers

Sample Code :

CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:8.0];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = @"Sample custom Title With small Fonts ";
self.navigationItem.titleView = label;
like image 160
Mihir Mehta Avatar answered Oct 18 '22 14:10

Mihir Mehta


As of iOS5+, you can use the UIAppearance protocol to change the font of the nav title, here's code:

NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:[UIFont fontWithName:@"Helvetica-Bold" size:18] forKey:UITextAttributeFont];
[[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];
like image 36
Sneakyness Avatar answered Oct 18 '22 14:10

Sneakyness


Since iOS5 you can use the new titleTextAttributes property like so:

NSMutableDictionary *newAttributes = [[NSMutableDictionary alloc] init]; 
[newAttributes setObject:[UIFont boldSystemFontOfSize:18] forKey:UITextAttributeFont];
[self.navigationController.navigationBar setTitleTextAttributes:newAttributes];

Possible keys are:

UITextAttributeFont
UITextAttributeTextColor
UITextAttributeTextShadowColor
UITextAttributeTextShadowOffset
like image 20
Brabbeldas Avatar answered Oct 18 '22 14:10

Brabbeldas