Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationBar title gets truncated when using custom font

after using this code to custom the UINavigationBar title appearance, the label with text gets truncated, as the image below shows:

[[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, there is enough space.

Any ideas?

like image 410
Bernat Avatar asked Mar 15 '13 20:03

Bernat


1 Answers

Update for iOS 9

I've done a fair bit of testing in a clean project with a few dozen built-in fonts at various sizes, and I think I can state with some confidence that the label sizing issues found in earlier versions of iOS have been fixed in (or before) iOS 9.

The use case decribed in the original question does not seem to be reproducible, and the title label now seems to resize properly on its own. As such, I don't think it's necessary to update the layout manually anymore.

If you are still seeing truncation issues when there is clearly plenty of visual space available in the navigation bar, there are a few things you could try:

  1. Remove any extra views that you may have been using to workaround the issue. For example, if you are creating your own UILabel and setting it as the navigation bar's titleView, you can stop doing that, and just set the title normally.
  2. Remove as much code as possible that adjusts the sizing of the navigation bar and titleView. That includes the code found in the originally accepted answer below.
  3. If you are using a custom font (i.e., one not included with iOS), validate it to make sure that it is not damaged, and contains all of the metadata necessary for iOS to measure it properly. If a font is damaged, it can appear incorrectly when used.

Original Answer

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    ...
    [[[self navigationController] navigationBar] setNeedsLayout];
}
like image 81
macserv Avatar answered Sep 30 '22 15:09

macserv