Ios 根据iphone屏幕大小增加字体大小

Ios 根据iphone屏幕大小增加字体大小,ios,objective-c,iphone,ipad,Ios,Objective C,Iphone,Ipad,如何根据UIStoryboard中的设备(iphone/ipad)自动更改所有按钮、标签标题的字体大小?如果我以编程方式更改屏幕大小,那么如何更改所有组件的屏幕大小。有没有合适的方法可以做到这一点?如果您使用的是interface builder,您可以为元素设置单独的大小类,并进行相应的调整您可以像这样使用AutoShrink: yourButton.titleLabel.minimumScaleFactor = 0.5; yourButton.titleLabel.adjustsFontSi

如何根据UIStoryboard中的设备(iphone/ipad)自动更改所有按钮、标签标题的字体大小?如果我以编程方式更改屏幕大小,那么如何更改所有组件的屏幕大小。有没有合适的方法可以做到这一点?

如果您使用的是interface builder,您可以为元素设置单独的大小类,并进行相应的调整

您可以像这样使用AutoShrink:

yourButton.titleLabel.minimumScaleFactor = 0.5;
yourButton.titleLabel.adjustsFontSizeToFitWidth = YES;

这不会调整大于我们设置的字体大小。但它会根据手机大小增加到我们设置的最大大小。

为Swift尝试此操作

创建函数

func setCustomFont() -> CGFloat {

        //Current runable device/simulator width find
        let bounds = UIScreen.main.bounds
        let width = bounds.size.width

        // basewidth you have set like your base storybord is IPhoneSE this storybord width 320px.
        let baseWidth: CGFloat = 320

        // "14" font size is defult font size
        let fontSize = 14 * (width / baseWidth)

        return fontSize
    }
yourbutton.titleLabel?.font = UIFont.init(name: "Helvetica", size: setCustomFont())
使用此功能

func setCustomFont() -> CGFloat {

        //Current runable device/simulator width find
        let bounds = UIScreen.main.bounds
        let width = bounds.size.width

        // basewidth you have set like your base storybord is IPhoneSE this storybord width 320px.
        let baseWidth: CGFloat = 320

        // "14" font size is defult font size
        let fontSize = 14 * (width / baseWidth)

        return fontSize
    }
yourbutton.titleLabel?.font = UIFont.init(name: "Helvetica", size: setCustomFont())
此类型还可以设置UILabelUITextfield字体,但不用于UIButton

1.转到情节提要,选择UILabel/UITextField

2.进入属性检查器


3.选中动态类型选项自动调整字体

我不明白,你能告诉我怎么做吗?但这只是基于字体大小的长度