UINavigationBar header truncated when using custom font

after using this code to customize the appearance of the UINavigationBar header, the text label is truncated, as shown below:

[[UINavigationBar appearance] setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor whiteColor], UITextAttributeFont : [UIFont fontWithName:@"Intro" size:20.0f], UITextAttributeTextShadowColor : [UIColor clearColor] }]; 

enter image description here

And, as you can see, enough space.

Any ideas?

+7
source share
4 answers

Update for iOS 9

I did some honest testing in a clean project with several dozen built-in fonts of different sizes, and I think I can confidently say that tag size issues found in earlier versions of iOS were fixed in (or before) iOS 9.

The use case described in the original question doesn't seem reproducible, and now the title bar seems to resize correctly. Thus, I do not consider it necessary to update the layout manually.

If you still see truncation problems when there is a lot of visual space available in the navigation bar, you can try a few things:

  • Remove any additional views that you may have used to work around the problem. For example, if you create your own UILabel and set it as the titleView navigation titleView , you can stop doing this and just set the title as usual.
  • Remove as much code as possible that adjusts the size of the navigation bar and titleView . This includes the code found in the originally accepted answer below.
  • If you are using a custom font (i.e. not included in iOS), check it to make sure that it is not damaged and contains all the metadata necessary for iOS to measure them correctly. If the font is damaged, it may not display correctly when used.

Original answer

There are some known issues with the UINavigationBar layout. Try updating the layout when the view controller appears, and / or while rotating.

 - (void)viewDidLoad { [super viewDidLoad]; ... [[[self navigationController] navigationBar] setNeedsLayout]; } 
+7
source

You can try making UILabel with the background color and the desired text settings. You can then set this label as the titleView attribute of your UINavigationBar

+1
source

I had the same problem if I first initialized the header with an empty string and then tried to update it. The setNeedsLayout call failed. Initializing an empty header with up to 20 space characters solved the problem for me.

0
source

After you set the label, call

 label.sizeToFit() 
0
source

All Articles