Skip to content

Instantly share code, notes, and snippets.

@29satnam
Last active March 11, 2019 16:30
Show Gist options
  • Save 29satnam/a36a26315fb8a6477b0e138d219c3200 to your computer and use it in GitHub Desktop.
Save 29satnam/a36a26315fb8a6477b0e138d219c3200 to your computer and use it in GitHub Desktop.
//
// UILabelDeviceClass.swift
//
// Created by MyCandy on 01/01/19.
// Copyright © 2019 Silver Seahog. All rights reserved.
//
import Foundation
import UIKit
class UILabelDeviceClass : UILabel {
@IBInspectable var iPhoneFontSize:CGFloat = 0 {
didSet {
overrideFontSize(fontSize: iPhoneFontSize)
}
}
func overrideFontSize(fontSize:CGFloat){
let currentFontName = self.font.fontName
var calculatedFont: UIFont?
let bounds = UIScreen.main.bounds
let height = bounds.size.height
switch height {
case 480.0: //iphone 4,4s,SE
calculatedFont = UIFont(name: currentFontName, size: fontSize * 0.7)
self.font = calculatedFont
break
case 568.0: //iphone 5, 5s
calculatedFont = UIFont(name: currentFontName, size: fontSize * 0.8)
self.font = calculatedFont
break
case 667.0: //iphone 6, 6s, 7, 8
calculatedFont = UIFont(name: currentFontName, size: fontSize)
self.font = calculatedFont
break
case 736.0: //iphone 6s+, 6+, 7+, 8+
calculatedFont = UIFont(name: currentFontName, size: fontSize * 1.0)
self.font = calculatedFont
break
case 812.0: //iphone x/xs
calculatedFont = UIFont(name: currentFontName, size: fontSize * 1.05)
self.font = calculatedFont
break
case 896.0: //iphone xr/xs max
calculatedFont = UIFont(name: currentFontName, size: fontSize * 1.2)
self.font = calculatedFont
break
case 1024.0: //ipad 5thGen, 6thGen, air, air2, pro9.7inch
calculatedFont = UIFont(name: currentFontName, size: fontSize * 1.25)
self.font = calculatedFont
break
case 1112.0: //ipad pro10.5inch
calculatedFont = UIFont(name: currentFontName, size: fontSize * 1.28)
self.font = calculatedFont
break
case 1194.0: //ipad pro11inch
calculatedFont = UIFont(name: currentFontName, size: fontSize * 1.31)
self.font = calculatedFont
break
case 1366.0: //ipad pro12.9inch1stGen, pro12.9inch2ndGen, pro12.9inch3rdGen
calculatedFont = UIFont(name: currentFontName, size: fontSize * 1.45)
self.font = calculatedFont
break
default:
print("Not an iPhone")
break
}
print(calculatedFont)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment