如何在Swift中增加plus设备的字体和大小?
我观察了一些热门应用。 当我们比较iPhone Plus设备和普通设备时,字体和图像会有所不同。 在iPhone Plus设备中稍大一点。 我们如何在iOS应用程序中实现相同目标? 我已经使用过闪屏。 但是字体仍然相同,加号和普通设备没有区别。
注意 :通过分辨率区分编码工作正常。 但我正在寻找其他替代方式,如自适应布局和启动屏幕。
您可以使用界面构建器(xib或storyboard)来完成它!
选择标签或文本字段等,然后转到attribute inspector
。
您会找到近似font
+
按钮,单击它,选择您的变体(大小类)并为该特定大小类设置不同的字体。
请参阅下面的屏幕截图以获
参考: Xcode 8中Interface Builder中的大小类
例如, Compact width, Regular height
此变体或尺寸等级适用于纵向模式下的所有iphone!
有关尺寸等级的更多详细信息,请参阅Apple文档 !
你可以做的第二件事是将自动autoshrink
设置为minimum font scale
,并将字体比例设置在0 to 1
之间。
然后在界面构建器中设置较大的字体(您希望在应用的加号或专业设备中显示的最大字体大小)。 现在,当您的应用程序在小型设备中打开时,您的字体大小将尝试使用该最小比例因子进行缩减。 例如,如果您在故事板中设置了比例因子0.5
并且字体大小为100,那么在小型设备中,它将尝试将字体大小减小到50以适应标签或文本字段。
只需为每个控制器创建自定义类。 下面,我为UIButton
创建csutom类,您也可以为其他控件创建相同的类。
HSCustomButton.h
#import @interface HSCustomButton : UIButton @end
HSCustomButton.m
#import "HSCustomButton.h" #define SCALE_FACTOR_H ([UIScreen mainScreen].bounds.size.height / 667) @implementation HSCustomButton - (id)initWithCoder:(NSCoder *)aDecoder { if( (self = [super initWithCoder:aDecoder]) ){ [self layoutIfNeeded]; [self configurefont]; } return self; } - (void) configurefont { CGFloat newFontSize = (self.titleLabel.font.pointSize * SCALE_FACTOR_H); self.titleLabel.font = [UIFont fontWithName:self.titleLabel.font.fontName size:newFontSize]; } @end
只需在故事板中更改类名,然后自动为所有其他设备进行字体缩放。
创建UILabel的类别类或任何其他控件(UIButton,UITextField等)。
的UILabel + DynamicFontSize.h
#import @interface UILabel (DynamicFontSize) @property (nonatomic) IBInspectable BOOL adjustFontSize; @end
的UILabel + DynamicFontSize.m
#import "UILabel+DynamicFontSize.h" @implementation UILabel (DynamicFontSize) @dynamic adjustFontSize; -(void)setAdjustFontSize:(BOOL)adjustFontSize{ if (adjustFontSize) { CGRect screenBounds = [[UIScreen mainScreen] bounds]; self.font = [self.font fontWithSize:self.font.pointSize*(screenBounds.size.width/320)]; // 320 for iPhone 5(320x568) storyboard design // if you design with iphone 6(375x667) in storyboard, use 375 instead of 320 and iphone 6 plus(414x736), use 414 } } @end
用法
选择任何标签并查找属性名称调整字体大小更改值为ON