3

I'm creating a custom UIBarButtonItem, like so:

    UIImage *originalImage = [UIImage imageNamed:@"button"];
    UIImage *buttonImage = [originalImage stretchableImageWithLeftCapWidth:10 topCapHeight:5];

    UIButton *toolbarB = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 65, 29)];
    [toolbarB setTitle:title forState:UIControlStateNormal];
    [toolbarB.titleLabel setFont:[UIFont fontWithName:kLatoBold size:17.0f]];
    [toolbarB setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [toolbarB addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarB];

Depending on which of my custom fonts I use, after inserting my UIBarButtonItem into my controller's navigationItem, I get one of the results below.

Why does the button text in the second result "float" higher than it is supposed to? The first button looks great, but the second one positions the text in an unnatural fashion... Could it be a problem with my font?

Button Text Displaying Properly

enter image description here

Annoying, Floating Button Text that is Too High!

enter image description here

MikeS
  • 3,891
  • 6
  • 35
  • 51

1 Answers1

1

It happens for some fonts, I don't know why, but I can suggest you to use UIEdgeInsets to push your text down a bit. I guess you cannot fix your font, so;

Here is a link for a similar solution; Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

Community
  • 1
  • 1
Bartu
  • 2,189
  • 2
  • 26
  • 50
  • Did you ever figure out why this happens? I'd like to be able to programmatically determine the inset. – digidigo Mar 12 '14 at 18:19