![](/img/trans.png)
[英]How to set different Font sizes for different iPhone screen size using storyboard?
[英]How to set different font sizes for different devices using Xcode Storyboard?
使用Size-Class並從Label屬性的Attribute Inspector
添加字體的大小變化。
以下是不同的可能變體,您可以使用Size類進行設置:
嘗試這個:
這是iPhone和iPad中字體大小變化的(結果)預覽
使用IB(故事板)可能無法實現您期望的結果,但您可以使用以下程序化解決方案進行嘗試:
extension UIDevice {
enum DeviceType: String {
case iPhone4_4S = "iPhone 4 or iPhone 4S"
case iPhones_5_5s_5c_SE = "iPhone 5, iPhone 5s, iPhone 5c or iPhone SE"
case iPhones_6_6s_7_8 = "iPhone 6, iPhone 6S, iPhone 7 or iPhone 8"
case iPhones_6Plus_6sPlus_7Plus_8Plus = "iPhone 6 Plus, iPhone 6S Plus, iPhone 7 Plus or iPhone 8 Plus"
case iPhoneX = "iPhone X"
case unknown = "iPadOrUnknown"
}
var deviceType: DeviceType {
switch UIScreen.main.nativeBounds.height {
case 960:
return .iPhone4_4S
case 1136:
return .iPhones_5_5s_5c_SE
case 1334:
return .iPhones_6_6s_7_8
case 1920, 2208:
return .iPhones_6Plus_6sPlus_7Plus_8Plus
case 2436:
return .iPhoneX
default:
return .unknown
}
}
}
// Get device type (with help of above extension) and assign font size accordingly.
let label = UILabel()
let deviceType = UIDevice.current.deviceType
switch deviceType {
case .iPhone4_4S:
label.font = UIFont.systemFont(ofSize: 10)
case .iPhones_5_5s_5c_SE:
label.font = UIFont.systemFont(ofSize: 12)
case .iPhones_6_6s_7_8:
label.font = UIFont.systemFont(ofSize: 14)
case .iPhones_6Plus_6sPlus_7Plus_8Plus:
label.font = UIFont.systemFont(ofSize: 16)
case .iPhoneX:
label.font = UIFont.systemFont(ofSize: 18)
default:
print("iPad or Unkown device")
label.font = UIFont.systemFont(ofSize: 20)
}
您無法在IB中完成此操作,您可以在運行時檢測當前設備的大小並相應地設置字體,如果所有元素都具有相同的字體,則可以使用。出現設置全局字體
iPhone 6,7,X沒有字體變體,它必須是代碼而不是IB
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.