如何通过编程调整UIButton中文本的字体大小以适应Swift中的高度?
我有一个按钮,并设置其框架和标题:如何通过编程调整UIButton中文本的字体大小以适应Swift中的高度?,swift,ios8,uibutton,Swift,Ios8,Uibutton,我有一个按钮,并设置其框架和标题: let button = UIButton.buttonWithType(UIButtonType.System) as UIButton button.frame = CGRectMake(x, y, width, height) button.setTitle("Shuffle", forState: UIControlState.Normal) 我一直在使用下面的更改UIButton中文本的宽度: button.titleLabel?.adjustsF
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(x, y, width, height)
button.setTitle("Shuffle", forState: UIControlState.Normal)
我一直在使用下面的更改UIButton中文本的宽度:
button.titleLabel?.adjustsFontSizeToFitWidth = true
这会将字体大小调整为按钮的宽度,但不会调整按钮的高度。我找不到同等的方法来确保文本也符合UIButton的高度。我也尝试了以下方法,但对我无效:
button.titleLabel?.drawTextInRect(button.frame)
查看UILabel
的属性:
它可以与UIButton一起使用,如下所示:
button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters
查看
baslineAdjustment
属性:我发现以下内容在我的案例中有效:button.titleLabel?.baselineAdjustment=UIBaselineAdjustment.AlignCentersGreat。添加了一个aswer。这完全救了我的命。我爱你。