我有一个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
答案 0 :(得分:9)
如上所述here :)
您需要使用UILabel的baselineAdjustment
用户
label.baselineAdjustment = UIBaselineAdjustmentAlignCenters
答案 1 :(得分:5)
Swift 3
label.baselineAdjustment = .alignCenters
按钮
button.titleLabel?.baselineAdjustment = .alignCenters