iPhone 6s PlusやiPhone 6 Plusといった5.5インチの端末はサイズが大きいため、それに合わせてフォントサイズを大きくしたいと考えることがあるかと思います。
landscape(横向き)のフォントサイズであればSize Classを使うことで対応することができますが、portrait(縦向き)では対応することができません。
そのため、Storyboard上で5.5インチのフォントサイズを変更する方法について考えてみました。
実装
まずは、3.5 / 4.0 / 4.7 / 5.5インチのデバイスサイズをenumで定義します。
enum DeviceHeight: CGFloat {
case iPhone3_5 = 480.0 // 3.5インチ
case iPhone4_0 = 568.0 // 4.0インチ
case iPhone4_7 = 667.0 // 4.7インチ
case iPhone5_5 = 736.0 // 5.5インチ
}
次に、UIScreenクラスに5.5インチ端末かどうか調べる機能を追加します。
protocol DeviceType {
static var is5_5: Bool { get }
}
extension DeviceType where Self: UIScreen {
static var is5_5: Bool {
switch UIScreen.mainScreen().bounds.size.height {
case DeviceHeight.iPhone5_5.rawValue: return true
default: return false
}
}
}
extension UIScreen: DeviceType {}
IBDesignableとIBInspectableで5.5インチ端末のフォントサイズをStoryboard上から変更できるようにします。
@IBDesignable
extension UILabel {
@IBInspectable
var fontSize5_5: CGFloat {
get {
if UIScreen.is5_5 {
return font.pointSize
} else {
return 0
}
}
set {
if UIScreen.is5_5 {
font = UIFont.systemFontOfSize(newValue)
}
}
}
}
使い方
おわりに
他に良い方法があればご教授お願い致します!