7

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?

macserv
  • 3,546
  • 1
  • 26
  • 36
Bernat
  • 1,537
  • 3
  • 18
  • 40
  • Not an answer, but an observation from my own experience: I've tried using a custom font in a NavigationBar too and it appears that iOS tries really hard to make the text fit into a very specific space. So I either get very small (height) text, or ellipses like in your example. My solution has been to use a pre-rendered background graphic. Not an answer, I know, but it works. – Blake Schwendiman Mar 15 '13 at 21:37
  • check this [link](http://stackoverflow.com/questions/5928851/uinavigationbar-title). This should be the alternative to fix your issue. – kaar3k Mar 16 '13 at 01:28
  • The problem is not solved. And @BlakeSchwendiman, i can't use an image, since my app in lots of languages – Bernat Mar 18 '13 at 09:33
  • Would refreshing the navigation bar's layout help? Try calling: `[[[self navigationController] navigationBar] setNeedsLayout];` in your `-viewDidLoad` method. – macserv Mar 18 '13 at 12:38
  • Hei @macserv, you solution worked! What I don't understand yet is why that problem happend. – Bernat Mar 18 '13 at 14:46
  • Excellent. I've added it as an answer below. – macserv Mar 18 '13 at 15:17

4 Answers4

7

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];
}
macserv
  • 3,546
  • 1
  • 26
  • 36
1

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

Undo
  • 25,519
  • 37
  • 106
  • 129
0

I had the same problem if I first initialized the title to an empty string, then later tried to update it. Calling setNeedsLayout did not fix. Initializing the empty title to 20 space characters solved the problem for me.

user1055568
  • 1,349
  • 13
  • 21
0

After you set the label call

label.sizeToFit()
Jeremy Sh
  • 609
  • 7
  • 24