使用minimumScaleFactor时,UIButton titleLabel不会垂直居中

时间:2014-06-30 20:49:20

标签: ios uibutton

我有一个UIButton,文本在按钮中居中,直到文本字符串变长,并开始缩小文本的大小。然后文本似乎粘在textField的底部,而不是保持在按钮的中心。

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

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

[self.descriptionBtn setTitle:@"This a longer title" forState:UIControlStateNormal];
//is vertically lower than center

2 个答案:

答案 0 :(得分:9)

如上所述here :)
您需要使用UILabel的baselineAdjustment用户 label.baselineAdjustment = UIBaselineAdjustmentAlignCenters

答案 1 :(得分:5)

Swift 3

label.baselineAdjustment = .alignCenters

按钮

button.titleLabel?.baselineAdjustment = .alignCenters