是否可以根据整个应用程序的不同iPhone设备大小设置不同的UILabel
s'字体大小?
答案 0 :(得分:-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
}
}
}
然后在Storyboard中设置iPhoneFontSize属性值以获得最高分辨率,如下面的屏幕截图所示。