【问题标题】:Set different font size for iPhone 5 and 6为 iPhone 5 和 6 设置不同的字体大小
【发布时间】:2015-07-21 19:32:42
【问题描述】:

我已经搜索过,但还没有找到答案。

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

【问题讨论】:

  • 你在使用自动布局吗?然后你可以很容易地完成这个任务。您将在属性检查器中的字体选项之前获得 + 选项。在哪里可以设置乘数值
  • @amelie ... 请参阅下面发布的答案。我发现一个答案是正确的。

标签: ios objective-c iphone resize font-size


【解决方案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

【讨论】:

  • 谢谢! @AshokLondhe
  • 你的答案是正确的。昨天我也同样被连根拔起。欢迎。
  • 您不应该使用硬编码的大小来获取设备型号
【解决方案2】:

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

【讨论】:

  • 谢谢,我去看看。
【解决方案3】:

我的辅助扩展:

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 特定尺寸的解决方案。

【讨论】:

    猜你喜欢
    • 2016-04-15
    • 2015-08-04
    • 2016-01-14
    • 1970-01-01
    • 2016-10-21
    • 2015-08-16
    • 2021-06-05
    • 2017-09-15
    • 2014-02-02
    相关资源
    最近更新 更多