Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton titleLabel not vertically centered when using minimumScaleFactor

Tags:

ios

uibutton

I have a UIButton and the text is centered within the button normally until the text string gets longer and it starts to scale down the size of the text. Then the text seems to stick to the bottom of the textField instead of staying centered in the button.

self.titleBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
self.titleBtn.titleLabel.minimumScaleFactor = .2;

[self.descriptionBtn setTitle:@"T" forState:UIControlStateNormal]; 
//works normally

but

[self.descriptionBtn setTitle:@"This a longer title" forState:UIControlStateNormal];
//is vertically lower than center
like image 855
Chase Roberts Avatar asked Jun 30 '14 20:06

Chase Roberts


1 Answers

As noted here :)
You need to user baselineAdjustment propery of the UILabel
label.baselineAdjustment = UIBaselineAdjustmentAlignCenters

like image 60
tt.Kilew Avatar answered Oct 01 '22 03:10

tt.Kilew