iOS Different Font Sizes within Single Size Class for Different Devices

前端 未结 7 1576
死守一世寂寞
死守一世寂寞 2020-12-01 05:32

In iOS 8, we can design a different UI layout for each size class. The issue I\'m facing is, I\'ve designed a layout for Compact Width and Regular Height (size class for all

7条回答
  •  甜味超标
    2020-12-01 06:27

    Thats the way how I did it. It's written in Swift 4 :)

    enum DeviceSize {
        case big, medium, small
    }
    
    protocol Fontadjustable {
    
        var devicetype: DeviceSize { get }
    
        func adjustFontSizeForDevice()
    }
    
    extension UILabel: Fontadjustable {
    
        var devicetype: DeviceSize {
            switch UIScreen.main.nativeBounds.height {
            case 1136:
                return .small
            case 1334:
                return .medium
            case 2208:
                return .big
            case 2436:
                return .big
            default:
                return .big
            }
        }
    
        func adjustFontSizeForDevice() {
            switch self.devicetype {
            case .small:
                self.font = font.withSize(font.pointSize)
            case .medium:
                self.font = font.withSize(font.pointSize + 5)
            case .big:
                self.font = font.withSize(font.pointSize + 10)
            }
        }
    }
    

    USAGE : myawesomeLabel.adjustFontSizeForDevice()

提交回复
热议问题