[英]iOS Different Font Sizes within Single Size Class for Different Devices
在iOS 8中,我們可以為每個尺寸類設計不同的UI布局。 我面臨的問題是,我設計了一個緊湊寬度和常規高度的布局(縱向所有iPhone的尺寸等級),但我希望保持3.5和4英寸設備的標簽字體較小(iPhone 4和5 ),然后相對較大的4.7英寸(iPhone 6)和更大的5.5英寸(iPhone 6 Plus)設備。 我已搜索但無法找到解決方案為同一大小類中的不同設備設置不同的字體大小。
編輯 :我不再推薦這個了。 當新設備出現時,這種方法不能很好地擴展。 使用動態字體大小和特定於大小類的字體的組合。
假設有一個新的iPhone型號出現,如果您使用自動布局和大小類,則不必手動修復所有約束以使您的應用與此新設備兼容。 但是,您仍然可以使用以下代碼設置UILabel
的字體大小:
if UIScreen.mainScreen().bounds.size.height == 480 {
// iPhone 4
label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.height == 568 {
// IPhone 5
label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 375 {
// iPhone 6
label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 414 {
// iPhone 6+
label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 768 {
// iPad
label.font = label.font.fontWithSize(20)
}
我正在使用UILabel Custom類 , UILabel擴展和UIDevice擴展作為通用解決方案在Swift 3+的項目中處理它。
獲取screenType
UIDevice擴展 :
public extension UIDevice {
var iPhone: Bool {
return UIDevice().userInterfaceIdiom == .phone
}
enum ScreenType: String {
case iPhone4
case iPhone5
case iPhone6
case iPhone6Plus
case iPhoneX
case Unknown
}
var screenType: ScreenType {
guard iPhone else { return .Unknown}
switch UIScreen.main.nativeBounds.height {
case 960:
return .iPhone4
case 1136:
return .iPhone5
case 1334:
return .iPhone6
case 2208:
return .iPhone6Plus
case 2436:
return .iPhoneX
default:
return .Unknown
}
}
}
以下是使用screenType
調整字體大小的UILabel擴展 。 adjustsFontSizeToFitDevice
方法也可以添加到UILabel自定義類中 ,但我已將它放在UILabel擴展中 ,以使其可從所有類型的UILabel實例訪問。
adjustsFontSizeToFitDevice
方法中使用的常量“2”可以更改為任何所需的數字。 我的邏輯是將iPhone 6/7/8視為默認分辨率,並為該分辨率的每個標簽提供合適的字體大小(在Storyboard中 )。 然后,我為iPhone X和iPhone 6/7/8 Plus增加了2分,而為iPhone 4/5減去了2分。
extension UILabel {
func adjustsFontSizeToFitDevice() {
switch UIDevice().screenType {
case .iPhone4, .iPhone5:
font = font.withSize(font.pointSize - 2)
break
case .iPhone6Plus, .iPhoneX:
font = font.withSize(font.pointSize + 2)
break
default:
font = font.withSize(font.pointSize)
}
}
}
最后是一個UILabel自定義類 ,用於將字體調整應用於從MyCustomLabel
中細分的所有標簽。
class MyCustomLabel: UILabel {
// MARK: - Life Cycle Methods
override func awakeFromNib() {
super.awakeFromNib()
adjustsFontSizeToFitDevice()
}
}
用法:在Storyboard中 ,子類來自MyCustomLabel
的所有UILabel實例,其字體大小需要根據設備大小進行調整。
您可以達到如下所需的效果。
Usage :
不使用14作為字體大小, 您可以使用14.fontSize
,它將根據設備更改,取決於您的delta值。
無需在代碼中添加條件。 只有一次如下。
用法: UIFont.font_medium(12.fontSize)
UIFont擴展:
extension UIFont {
class func font_medium(_ size : CGFloat) -> UIFont {
return UIFont(name: "EncodeSans-Medium", size: size)!;
}
}
UIDevice擴展:
extension UIDevice {
enum DeviceTypes {
case iPhone4_4s
case iPhone5_5s
case iPhone6_6s
case iPhone6p_6ps
case after_iPhone6p_6ps
}
static var deviceType : DeviceTypes {
switch UIScreen.main.height {
case 480.0:
return .iPhone4_4s
case 568.0:
return .iPhone5_5s
case 667.0:
return .iPhone6_6s
case 736.0:
return .iPhone6p_6ps
default:
return .after_iPhone6p_6ps
}
}
}
Int擴展:
extension Int{
var fontSize : CGFloat {
var deltaSize : CGFloat = 0;
switch (UIDevice.deviceType) {
case .iPhone4_4s,
.iPhone5_5s :
deltaSize = -1;
case .iPhone6_6s :
deltaSize = 2;
case .iPhone6p_6ps :
deltaSize = 2;
default:
deltaSize = 0;
}
let selfValue = self;
return CGFloat(selfValue) + deltaSize;
}
}
兩種方式:
1)在app delegate中手動創建一個方法,共享它的對象和調用方法。
例如:
var device = UIDevice.currentDevice().model
if (device == "iPhone" || device == "iPhone Simulator" || device == "iPod touch")
{
labelboarder.frame = CGRectMake(0,self.usernameTF.frame.height-10, self.usernameTF.frame.width, 1)
labelboarder1.frame = CGRectMake(0,self.usernameTF.frame.height-10, self.usernameTF.frame.width,1)
}
else
{
labelboarder.frame = CGRectMake(0,self.usernameTF.frame.height, 500, 1)
labelboarder1.frame = CGRectMake(0,self.usernameTF.frame.height, 500,1)
}
2)在每個UI項目上,轉到屬性檢查器,聲明一個字體。
(字體大小字段左側有一個+符號。單擊它,選擇匹配的大小類並聲明字體大小。)
第二種選擇對我來說很方便。 快樂的編碼!
不要為每個標簽編寫代碼,只需使用您的自定義標簽類擴展您的標簽類,如下所示,它將根據設備分辨率比例因子自動縮放:
#define SCALE_FACTOR_H ( [UIScreen mainScreen].bounds.size.height / 568 )
CustomLabel.h
#import <UIKit/UIKit.h>
@interface CustomLabel : UILabel
@end
CustomLabel.m
#import "CustomLabel.h"
@implementation CustomLabel
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (id)initWithCoder:(NSCoder *)aDecoder {
if( (self = [super initWithCoder:aDecoder]) ){
[self layoutIfNeeded];
[self configurefont];
}
return self;
}
- (void) configurefont {
CGFloat newFontSize = (self.font.pointSize * SCALE_FACTOR_H);
self.font = [UIFont fontWithName:self.font.fontName size:newFontSize];
}
@end
像這樣創建,
#define VIEWHEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define VIEWWIDTH ([[UIScreen mainScreen] bounds].size.width)
#define FONTNAME_LIGHT @"AppleSDGothicNeo-Regular"
#define FONTNAME_BOLD @"AppleSDGothicNeo-Bold"
#define LFONT_16 [UIFont fontWithName:FONTNAME_LIGHT size:16]
在那之后你想要改變標簽字體,我們可以編寫簡單的Switch case
switch ((VIEWHEIGHT == 568)?1:((VIEWHEIGHT == 667)?2:3)) {
case 1:{
boldFont = BFONT_16;
}
break;
case 2:{
privacyFont = LFONT_18;
boldFont = BFONT_18;
}
break;
default:{
privacyFont = LFONT_20;
boldFont = BFONT_20;
}
break;
}
Swift Version
func isPhoneDevice() -> Bool {
return UIDevice.current.userInterfaceIdiom == .phone
}
func isDeviceiPad() -> Bool {
return UIDevice.current.userInterfaceIdiom == .pad
}
func isPadProDevice() -> Bool {
let SCREEN_WIDTH = Float(UIScreen.main.bounds.size.width)
let SCREEN_HEIGHT = Float(UIScreen.main.bounds.size.height)
let SCREEN_MAX_LENGTH: Float = fmax(SCREEN_WIDTH, SCREEN_HEIGHT)
return UIDevice.current.userInterfaceIdiom == .pad && SCREEN_MAX_LENGTH == 1366.0
}
func isPhoneXandXSDevice() -> Bool {
let SCREEN_WIDTH = CGFloat(UIScreen.main.bounds.size.width)
let SCREEN_HEIGHT = CGFloat(UIScreen.main.bounds.size.height)
let SCREEN_MAX_LENGTH: CGFloat = fmax(SCREEN_WIDTH, SCREEN_HEIGHT)
return UIDevice.current.userInterfaceIdiom == .phone && SCREEN_MAX_LENGTH == 812.0
}
func isPhoneXSMaxandXRDevice() -> Bool {
let SCREEN_WIDTH = CGFloat(UIScreen.main.bounds.size.width)
let SCREEN_HEIGHT = CGFloat(UIScreen.main.bounds.size.height)
let SCREEN_MAX_LENGTH: CGFloat = fmax(SCREEN_WIDTH, SCREEN_HEIGHT)
return UIDevice.current.userInterfaceIdiom == .phone && SCREEN_MAX_LENGTH == 896.0
}
這就是我做的方式。 它是用Swift 4編寫的:)
enum DeviceSize {
case big, medium, small
}
protocol Fontadjustable {
var devicetype: DeviceSize { get }
func adjustFontSizeForDevice()
}
extension UILabel: Fontadjustable {
var devicetype: DeviceSize {
switch UIScreen.main.nativeBounds.height {
case 1136:
return .small
case 1334:
return .medium
case 2208:
return .big
case 2436:
return .big
default:
return .big
}
}
func adjustFontSizeForDevice() {
switch self.devicetype {
case .small:
self.font = font.withSize(font.pointSize)
case .medium:
self.font = font.withSize(font.pointSize + 5)
case .big:
self.font = font.withSize(font.pointSize + 10)
}
}
}
用法: myawesomeLabel.adjustFontSizeForDevice()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.