swift - 如何在 Swift 中以编程方式调整 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?.adjustsFontSizeToFitWidth = true

这确实会将字体大小调整为宽度,但不会调整按钮的高度。我找不到等效的方法来确保文本也适合 UIButton 的高度。我也尝试了以下方法,但它对我不起作用:

button.titleLabel?.drawTextInRect(button.frame)

最佳答案

看看 baselineAdjustment UILabel 的属性:

它可以与 UIButton 一起使用,如下所示:

button.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters

关于swift - 如何在 Swift 中以编程方式调整 UIButton 中文本的字体大小以适应高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28871173/

相关文章:

ios - 为什么这个对象在 ARC 下被过早释放?

ios - 在 RxSwift 的 UICollectionViewCell 中订阅 UIButton 点击​​?

ios - 如何删除使用 swift 代码添加的 3D touch 快速操作

Swift 类继承

ios - 我如何在 CloudKit 中创建一个记录作为 child 的记录列表?

ios - 在 Widget/Today View Extension 中使用核心数据模型?

objective-c - 以编程方式添加 UIButton 不会显示

swift - 快速更改默认的全局色调颜色

ios - 没有收到 Firebase 远程通知?

iOS 操作 :@selector issue when UIButton is added to another UIButton. 操作仅在按钮相交的地方有效