0

I have over 20 UINavigationBars need to change the font of their titles, and based on all the tutorials I saw, there is one way to do this which is change the bar UILabel, so I used below code:

+ (void) applyFontForUINavigationItem:(UINavigationItem *)navItem withTitle:(NSString *) title{
    UILabel *navLabel = [Util generateNabBarLabel withText:title];
    [navLabel setFontName:FONT_NAME_BOLD];
    navItem.titleView = navLabel;
}

It works fine, but I don't want to add this odd line in all my ViewControllerss' viewDidLoad, is there a legal way to do this even with subclassing ?

EDIT: I am using custom font from a ttf file same as here.

and after applying apperance I got this:

enter image description here

Note that setFont doesn't work for Arabic Custome font according to this so am using below for UILabels:

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:myLabel.text attributes:@{ NSFontAttributeName : myLabel.font, NSLigatureAttributeName: @1}];
myLabel.attributedText =   attributedString;

And it works fine (for UILabel).

Community
  • 1
  • 1

3 Answers3

1

try to use with UIAppearance feature to set the navigation bar tile font size.

Krishna
  • 31
  • 2
0

You can use the appearance delegate to change the font via the title text attributes:

[[UINavigationBar appearance] setTitleTextAttributes:@{ UITextAttributeFont : [UIFont fontWithName:FONT_NAME_BOLD size:17] }];
Wain
  • 118,658
  • 15
  • 128
  • 151
  • Doesn't' work, dude sorry for the late edit, please check it in my question. –  Aug 03 '13 at 19:49
  • It should work, you just need to get the font name right. What is the `setFontName:` method you're using in your code? – Wain Aug 03 '13 at 19:52
  • I am using this for UILabel and it works fine: NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:myLabel.text attributes:@{ NSFontAttributeName : myLabel.font, NSLigatureAttributeName: @1}]; myLabel.attributedText = attributedString;self.font,NSLigatureAttributeName: @1}]; label.attributedText = attributedString; –  Aug 03 '13 at 19:55
  • You should be able to add the font you create for the label currently into the text attributes dictionary. – Wain Aug 03 '13 at 19:58
  • It seems like a bug in iOS, so I am setting attributedText rather than font. –  Aug 03 '13 at 20:01
0

1.If edit your project in code I have a idea by inheriting a super navigation controller which has changed the navigation bar tile font size.

2.If you edit your project in storyboard, you can search .storyboard in your project, in storyboard you can easy edit the navigationbar to change the font.

I takon a photo for you below: three steps to edit your navigationbar in storyboard

lme
  • 59
  • 3
  • 17