Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text doesn't fit in UIButton titleLabel height for a specific font

I have had some troubles with a specific font i am using. It's made for Schools so A and Å is not the same height. I think it causes troubles for my UIButtons. I've tried to work around it but can't seem to get it right. This is what I've tried last:

[self setValue:[UIFont fontWithName:@"fontname" size:220] forKeyPath:@"button1.font"];
[self setValue:[UIFont fontWithName:@"fontname" size:220] forKeyPath:@"button2.font"];
[self setValue:[UIFont fontWithName:@"fontname" size:220] forKeyPath:@"button3.font"];
[button1 setTitle:@"A" forState:UIControlStateNormal];
[button2 setTitle:@"Å" forState:UIControlStateNormal];
[button3 setTitle:@"Å" forState:UIControlStateNormal];
[button2 setBackgroundColor:[UIColor blackColor]];
[button2 setFrame:CGRectMake(button2.frame.origin.x, button2.frame.origin.y, button2.frame.size.width, button2.frame.size.height+100)];
[button2.titleLabel setFrame:CGRectMake(button2.frame.origin.x, button2.frame.origin.y, button2.frame.size.width, button2.frame.size.height)];
button2.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;

This is the result:

The Å is not displayed properly.

The Å is not displayed properly as you can see. How can I fix this?

Thanks!

Best regards, Joakim

like image 372
Joakim Serholt Avatar asked Jan 07 '15 18:01

Joakim Serholt


1 Answers

I used the following way to solve the problem.

Suppose that you have a UIButton in your xib or storyboard, then

  1. Select the UIButton
  2. Go to Attributes Inspector
  3. Find Control Category
  4. Find Alignment
  5. Choose the right most side button of the Vertical property!

enter image description here

like image 190
YuAn Shaolin Maculelê Lai Avatar answered Oct 12 '22 16:10

YuAn Shaolin Maculelê Lai