Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton vertical alignment doesn't work

I can't figure why in the following code, the title alignment isn't remain Top.

UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.titleLabel.font = [UIFont systemFontOfSize:53];
btn2.frame = CGRectMake(20, 20, 270, 44);
[btn2 setTitle:@"test1 test2 test3 test4 test5 test6 test7" forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn2.titleLabel.minimumFontSize = 1.0;
btn2.titleLabel.adjustsFontSizeToFitWidth = YES;
btn2.titleLabel.numberOfLines = 1;
btn2.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;

enter image description here

like image 605
Rizon Avatar asked Aug 18 '13 22:08

Rizon


People also ask

What determines the height and vertical alignment of buttons and inputs?

The height and vertical alignment of buttons and inputs is determined by the combination of borders, padding, font-size, and line-height. With that in mind, let's define the basic style of buttons and inputs:

Why this damn vertical-align is not working?

Why This Damn Vertical-Align Is Not Working (CSS) September 24, 2018. Yeah! Why the hell does the CSS property vertical-align never work? Goddammit. The Answer has three parts: 1) First, vertical-align aligns elements relative to the dimensions of the line the element appears in. Nope, not relative to the container element.

What does vertical-align middle mean in HTML?

Because vertical-align: middle places elements relative to the baseline according to the formula baseline + x-height/2. middle means alignment with the middle of the line’s text. And this text sits on the baseline. To move the baseline up again, we need to align the bar itself with vertical-align: middle.

What does vertical-align do?

1) First, vertical-align aligns elements relative to the dimensions of the line the element appears in. Nope, not relative to the container element. See? Fire up the dev tools to poke around in the markup of the example! But here’s also a simplified version of it:


1 Answers

UIButton has a very nifty property named "titleEdgeInsets" which you can use (via UIEdgeInsetsMake to reposition the top and bottom margins of the title and get the thing centered, vertically.

like image 88
Michael Dautermann Avatar answered Nov 12 '22 19:11

Michael Dautermann