LoginSignup
13
14

More than 5 years have passed since last update.

【iOS】Storyboard上で5.5インチ端末のフォントサイズを変更する

Last updated at Posted at 2016-07-24

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)
            }
        }
    }
}

使い方

Storyboard上にUILabelを配置します。
スクリーンショット 2016-07-24 21.42.20.png

fontSize5_5 の値を変更します。
スクリーンショット 2016-07-24 21.34.42.png

おわりに

他に良い方法があればご教授お願い致します!

13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14