动态缩放UIButton中的字体

时间:2017-09-29 18:40:37

标签: ios swift uibutton

刚刚翻新我之前的问题 How to scale the fonts in a Button 仍然没有答案 问题描述 - 我从main.storyboard界面创建了一些UIButtons。按钮有一些噱头。当我使用约束时,随着我更改设备屏幕尺寸,按钮大小会增加。同时,tittles字体保持不变,所以我有一个很小的按钮。当我使用小屏幕的设备作为iPhone4时,标题会从按钮边缘移出。 我试过了

@IBAction func touchDigit(_ sender: UIButton) {
        sender.titleLabel?.minimumScaleFactor = 0.5;
        sender.titleLabel?.numberOfLines = 0
        sender.titleLabel?.adjustsFontSizeToFitWidth = true

我用可检查的var

创建了按钮类
@IBInspectable var adjustFontSize : Bool {
set { titleLabel?.adjustsFontForContentSizeCategory = newValue }
get { return titleLabel!.adjustsFontForContentSizeCategory  }
   }

没有效果。 UILabel具有自动调整字体复选框。 UIButton没有。 我确信每个人都有这样的问题,解决方案似乎很简单。请帮忙找到它!

1 个答案:

答案 0 :(得分:0)

使用此:

sender.titleLabel?.lineBreakMode = .byClipping