2

I'm developing an app which uses a custom font but I'm experiencing some unusual clipping. I think the problem is with the font itself, but I've no way to fix that, so i need to come up with a solution to work around it within the app.

I'm using the font for the navigationBar title, here's what's happening:

NavBar Broken

You can see that font is sitting too high from the baseline which is why the clipping is occurring.

I've managed to find an almost-suitable solution replacing the standard navigationBar title with a UILabel, inserting it in to the navigationBar's titleView.

    UILabel *navTitle = [[UILabel alloc] init];
    navTitle.frame = CGRectMake(0,0,190,40);
    navTitle.text = @"My Title Text";
    navTitle.font = [UIFont fontWithName:AGENDA_TYPE_FONT size:17];
    navTitle.backgroundColor = [UIColor clearColor];
    navTitle.textColor = [UIColor colorWithHue:0.356 saturation:0.457 brightness:0.288 alpha:1.00];
    navTitle.shadowColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.50];
    navTitle.shadowOffset = CGSizeMake(0, 1);
    navTitle.textAlignment = UITextAlignmentCenter;

    // Set label as titleView
    self.navigationItem.titleView = navTitle;

    // Shift the title down a bit...
    [self.navigationController.navigationBar setTitleVerticalPositionAdjustment:9 forBarMetrics:UIBarMetricsDefault];

This works because I've made the label twice the height it should be, which brings the text fully into view.

Partial Solution

The problem with this though is that it now sits far too low (outside of the navigationBar. As below...

Out of bounds

I can't think of a way to fix this, or even another way around this. I've tried overriding the frame origin height, but this does nothing.

Can anyone offer any help on this?

Liam
  • 1,028
  • 13
  • 24

2 Answers2

2

Create a container UIView which is the same width as your label, but is no higher than the UINavigationBar (46 pixels?) - lets say its 36 points high. Set 'clipsToBounds' on this container view to YES. Now add your label to this container view, and make the container view the titleView.

David H
  • 40,852
  • 12
  • 92
  • 138
0

Are you storyboarding? You might be able to adjust the baseline alignment of the navigation title.

Other than that, did you try installing the font and selecting it through storyboard?

If that doesn't work I think you might have issues with your font - you could try setting up a quick html page with a div to see if its an issue with the font.

Also, you might be able to use something here: How to programmatically resize an UIButton to the text size and keep a nice padding?

Community
  • 1
  • 1
ddoor
  • 5,819
  • 9
  • 34
  • 41
  • It is definitely an issue with the font - it's almost as if the font is superscript by default. I just need to find a workaround as I can't change the font. I am storyboarding but changing the baseline and other spacing attributes doesn't seem to have any effect. – Liam Aug 21 '12 at 11:12
  • Looking hear, you are on the right track by hacking up the frame. http://stackoverflow.com/questions/6566216/vertically-aligning-uinavigationitems – ddoor Aug 21 '12 at 11:14
  • my worry is that having an overlapping frame will cause problems with the title intercepting user's interaction with elements within the main view? – Liam Aug 21 '12 at 11:15
  • That shouldn't be a problem, because titlebars don't usually have interaction set up by default. I have done hacks like this before though and there is always a way around it! – ddoor Aug 21 '12 at 11:17