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;
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 (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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With