OGeek|极客世界-中国程序员成长平台

标题: ios - 动态缩放 UIButton 中的字体 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:51
标题: ios - 动态缩放 UIButton 中的字体

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

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

我使用可检查变量为按钮创建了类

@IBInspectable var adjustFontSize : Bool {
set { titleLabel?.adjustsFontForContentSizeCategory = newValue }
get { return titleLabel!.adjustsFontForContentSizeCategory  }
   }

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



Best Answer-推荐答案


使用这个:

sender.titleLabel?.lineBreakMode = .byClipping

关于ios - 动态缩放 UIButton 中的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46495128/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4