iosresponsive-designstoryboardsize

How To Add Dynamic Font Size about Multi Device in Xcode Storyboard


I added autoLayout in storyboard (w: Any, h: Any)

But because of fixed font size, The font size is same in all of devices (4, 4.7, 5.5 inch)

It looks nice in 4 inch. But in 5.5 inch, That's too small

I want to dynamically inclease and decrease UIlabel font size in any devices.

Any Ideas?

enter image description here


Solution

  • I Found Solution

    I Made one Class

    class UILabelDeviceClass : UILabel {
    
    @IBInspectable var iPhoneFontSize:CGFloat = 0 {
        didSet {
            overrideFontSize(iPhoneFontSize)
        }
    }
    
    func overrideFontSize(fontSize:CGFloat){
        let currentFontName = self.font.fontName
        var calculatedFont: UIFont?
        let bounds = UIScreen.mainScreen().bounds
        let height = bounds.size.height
        switch height {
        case 480.0: //Iphone 3,4,SE => 3.5 inch
            calculatedFont = UIFont(name: currentFontName, size: fontSize * 0.7)
            self.font = calculatedFont
            break
        case 568.0: //iphone 5, 5s => 4 inch
            calculatedFont = UIFont(name: currentFontName, size: fontSize * 0.8)
            self.font = calculatedFont
            break
        case 667.0: //iphone 6, 6s => 4.7 inch
            calculatedFont = UIFont(name: currentFontName, size: fontSize * 0.9)
            self.font = calculatedFont
            break
        case 736.0: //iphone 6s+ 6+ => 5.5 inch
            calculatedFont = UIFont(name: currentFontName, size: fontSize)
            self.font = calculatedFont
            break
        default:
            print("not an iPhone")
            break
        }
    
    }
    

    }

    Then, Set Class

    Img1

    Img2

    And Then, Set Value

    Happy Coding!