Skip to content

Instantly share code, notes, and snippets.

@29satnam
Created November 12, 2019 07:55
Show Gist options
  • Save 29satnam/3818fce821e146a3bb83384d368a8920 to your computer and use it in GitHub Desktop.
Save 29satnam/3818fce821e146a3bb83384d368a8920 to your computer and use it in GitHub Desktop.
Dynamic Font Size UIButton Label
import Foundation
import UIKit
class UIButtonDeviceClass : UIButton {
@IBInspectable var iPhoneFontSize:CGFloat = 0 {
didSet {
overrideFontSize(fontSize: iPhoneFontSize)
}
}
func overrideFontSize(fontSize:CGFloat){
let currentFontName = self.titleLabel?.font.fontName
var calculatedFont: UIFont?
let bounds = UIScreen.main.bounds
let height = bounds.size.height
print("height:", height)
switch height {
case 480.0: //iphone 4,4s,SE
calculatedFont = UIFont(name: currentFontName!, size: fontSize * 0.85)
self.titleLabel?.font = calculatedFont
break
case 568.0: //iphone 5, 5s
calculatedFont = UIFont(name: currentFontName!, size: fontSize * 0.9)
self.titleLabel?.font = calculatedFont
break
case 667.0: //iphone 6, 6s, 7, 8
calculatedFont = UIFont(name: currentFontName!, size: fontSize)
self.titleLabel?.font = calculatedFont
break
case 736.0: //iphone 6s+, 6+, 7+, 8+
calculatedFont = UIFont(name: currentFontName!, size: fontSize * 1.0)
self.titleLabel?.font = calculatedFont
break
case 812.0: //iphone x/xs/11pro
calculatedFont = UIFont(name: currentFontName!, size: fontSize * 1.05)
self.titleLabel?.font = calculatedFont
break
case 896.0: //iphone xr/xs max/11/11promax
calculatedFont = UIFont(name: currentFontName!, size: fontSize * 1.2)
self.titleLabel?.font = calculatedFont
break
case 1024.0: //ipad 5thGen, 6thGen, air, air2, pro9.7inch
calculatedFont = UIFont(name: currentFontName!, size: fontSize * 1.25)
self.titleLabel?.font = calculatedFont
break
case 1112.0: //ipad pro10.5inch
calculatedFont = UIFont(name: currentFontName!, size: fontSize * 1.28)
self.titleLabel?.font = calculatedFont
break
case 1194.0: //ipad pro11inch
calculatedFont = UIFont(name: currentFontName!, size: fontSize * 1.31)
self.titleLabel?.font = calculatedFont
break
case 1366.0: //ipad pro12.9inch1stGen, pro12.9inch2ndGen, pro12.9inch3rdGen
calculatedFont = UIFont(name: currentFontName!, size: fontSize * 1.45)
self.titleLabel?.font = calculatedFont
break
default:
print("Not an iPhone")
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment