ios - 如何使用 Xcode Storyboard 为不同的设备设置不同的字体大小?

标签 ios fonts font-size xcode-storyboard

我想使用 Xcode Storyboard 为 iPhone 5、iPhone 6 Plus、iPhone 7 Plus 和 iPhone X 设置不同的字体大小。

有人可以提供任何建议吗?

Example is shown in the image where i was stuck.

最佳答案

使用Size-Class并从 Label 属性的 Attribute Inspector 添加字体大小变化。

这里有不同的可能变化,你可以用 Size class 设置:

enter image description here

enter image description here

试试这个:

enter image description here

enter image description here

这是 iPhone 和 iPad 中字体大小变化的(结果)预览

enter image description here

更新

您期望的结果可能无法使用 IB( Storyboard)实现,但您可以尝试使用以下编程解决方案:

extension UIDevice {
    
    
    enum DeviceType: String {
        case iPhone4_4S = "iPhone 4 or iPhone 4S"
        case iPhones_5_5s_5c_SE = "iPhone 5, iPhone 5s, iPhone 5c or iPhone SE"
        case iPhones_6_6s_7_8 = "iPhone 6, iPhone 6S, iPhone 7 or iPhone 8"
        case iPhones_6Plus_6sPlus_7Plus_8Plus = "iPhone 6 Plus, iPhone 6S Plus, iPhone 7 Plus or iPhone 8 Plus"
        case iPhoneX = "iPhone X"
        case unknown = "iPadOrUnknown"
    }
    
    var deviceType: DeviceType {
        switch UIScreen.main.nativeBounds.height {
        case 960:
            return .iPhone4_4S
        case 1136:
            return .iPhones_5_5s_5c_SE
        case 1334:
            return .iPhones_6_6s_7_8
        case 1920, 2208:
            return .iPhones_6Plus_6sPlus_7Plus_8Plus
        case 2436:
            return .iPhoneX
        default:
            return .unknown
        }
    }
}


// Get device type (with help of above extension) and assign font size accordingly.
let label = UILabel()

let deviceType = UIDevice.current.deviceType

switch deviceType {
    
case .iPhone4_4S:
    label.font = UIFont.systemFont(ofSize: 10)
    
case .iPhones_5_5s_5c_SE:
    label.font = UIFont.systemFont(ofSize: 12)
    
case .iPhones_6_6s_7_8:
    label.font = UIFont.systemFont(ofSize: 14)
    
case .iPhones_6Plus_6sPlus_7Plus_8Plus:
    label.font = UIFont.systemFont(ofSize: 16)
    
case .iPhoneX:
    label.font = UIFont.systemFont(ofSize: 18)
    
default:
    print("iPad or Unkown device")
    label.font = UIFont.systemFont(ofSize: 20)

}

关于ios - 如何使用 Xcode Storyboard 为不同的设备设置不同的字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49296318/

相关文章:

css - 不显示 CSS 中的字体变体

android - Android 中的默认字体特征是什么?

html - CSS 覆盖 'font-weight' 属性

Swift 3.0 动态改变标签的字体

html - 子元素上的相对字体大小很棘手?

Android 相当于 CoreText

ios - 旋转时 UILabel 文本会被截断。怎么修?

iOS:GPUImage 库和 VBO

ios - 自定义 UITableView 单元格动画

java - 如何将 swing jButton 上的文本字体大小设置为适合 jButton 的最大大小