2017-11-17 16 views

答えて

-1

下のスクリーンショットで見られるように

class Label: UILabel { 

    @IBInspectable var iPhoneFontSize:CGFloat = 0 { 
     didSet { 
      overrideFontSize(fontSize: iPhoneFontSize) 
     } 
    } 

    @IBInspectable 
    public var cornerRadius: CGFloat = 2.0 { 
     didSet { 
      self.layer.cornerRadius = self.cornerRadius 
     } 
    } 

    @IBInspectable 
    public var borderWidth: CGFloat = 0 { 
     didSet { 
      self.layer.borderWidth = self.borderWidth 
     } 
    } 

    @IBInspectable 
    public var borderColor: UIColor = .clear { 
     didSet { 
      self.layer.borderColor = self.borderColor.cgColor 
     } 
    } 

    private 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 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, 7, 7s => 4.7 inch 
      calculatedFont = UIFont(name: currentFontName, size: fontSize * 0.9) 
      self.font = calculatedFont 
      break 
     case 736.0: //iphone 6+, 6s+, 7+, 7s+ => 5.5 inch 
      calculatedFont = UIFont(name: currentFontName, size: fontSize) 
      self.font = calculatedFont 
      break 
     default: // New devices 
      calculatedFont = UIFont(name: currentFontName, size: fontSize) 
      self.font = calculatedFont 
      break 
     } 

    } 
} 

は、次にストーリーボードで最高の解像度のためのiPhoneFontSizeプロパティ値を設定します。

Storyboard Setup

関連する問題