为iPhone 5和6设置不同的字体大小

问题描述 投票:2回答:3

我已经搜索过但尚未找到答案。

我想为iPhone 5和6设置不同的标签字体大小。我知道我可以为Compact Width设置特定的布局,但5和6都属于该组。有没有办法做到这一点?

ios objective-c iphone resize font-size
3个回答
1
投票

您可以获取iPhone的型号并将其与iPhone5,5s,5c,6进行比较,并相应地设置字体。您不应该使用硬编码的大小来获取设备型号,但您可以使用Apple的API获取它。请参考thisthis


1
投票
if UIScreen.mainScreen().bounds.size.height == 480 {
// iPhone 4
label.font = label.font.fontWithSize(20)     

} else if UIScreen.mainScreen().bounds.size.height == 568 {
// IPhone 5
label.font = label.font.fontWithSize(20)

} else if UIScreen.mainScreen().bounds.size.width == 375 {
// iPhone 6
label.font = label.font.fontWithSize(20)

} else if UIScreen.mainScreen().bounds.size.width == 414 {
// iPhone 6+
label.font = label.font.fontWithSize(20)

} else if UIScreen.mainScreen().bounds.size.width == 768 {
// iPad
label.font = label.font.fontWithSize(20)

}

或者您可以使用Apple的API获取设备并执行其余逻辑来设置字体大小。请参考this


1
投票

我的助手扩展:

extension UIFont {
    static var sizeMultiplier: CGFloat {
        return UIDevice.current.isPhone5 ? 0.85 : 1
    }

    static func regular(_ size: CGFloat) -> UIFont {
        return UIFont(name: "SFUIText-Regular", size: size * sizeMultiplier)!
    }

    static func bold(_ size: CGFloat) -> UIFont {
        return UIFont(name: "SFUIText-Bold", size: size * sizeMultiplier)!
    }
}

用法:

titleLabel.font = .bold(16)

我也在寻找一种解决方案,在运行时属性中附加特定于iPhone5的大小。

© www.soinside.com 2019 - 2024. All rights reserved.