简体   繁体   English

如何在Swift的plus设备上增加字体和大小?

[英]How to increase fonts and sizes on plus device in Swift?

I observed some popular apps. 我观察了一些热门应用。 When we compare iPhone Plus devices and normal devices, fonts and images are varying. 当我们比较iPhone Plus设备和普通设备时,字体和图像会有所不同。 A little bit bigger in iPhone Plus device. 在iPhone Plus设备中稍大一点。 How can we achieve same in our iOS applications? 我们如何在iOS应用程序中实现相同目标? I already used splash screens. 我已经使用了闪屏。 But still fonts are same, no difference in plus and normal devices. 但是字体仍然相同,加号和普通设备没有区别。

Note : By resolution differentiate in coding is working fine. 注意 :通过分辨率区分编码工作正常。 But I'm looking for other alternative ways like either in adaptive layouts nor launch screens. 但我正在寻找其他替代方式,如自适应布局和启动屏幕。

Just create custom class for each controller. 只需为每个控制器创建自定义类。 Below, I am create csutom class for UIButton , You can also create same for other control. 下面,我为UIButton创建csutom类,您也可以为其他控件创建相同的类。

HSCustomButton.h HSCustomButton.h

#import <UIKit/UIKit.h>

@interface HSCustomButton : UIButton

@end

HSCustomButton.m 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

Just change class name in storyboard, then font scale automatically for all other device. 只需在故事板中更改类名,然后自动为所有其他设备进行字体缩放。

You can do it withing your interface builder(xib or storyboard)! 您可以使用界面构建器(xib或故事板)来完成它!

Select your label or text field etc and go to attribute inspector . 选择标签或文本字段等,然后转到attribute inspector

Near font you will find + button, click it, select your variation (size class) and set different font for that particular size class. 您会找到近似font +按钮,单击它,选择您的变体(大小类)并为该特定大小类设置不同的字体。

Refer below screenshot for better understanding, 请参阅下面的屏幕截图以获

在此输入图像描述

Reference : Size classes in Interface Builder in Xcode 8 参考: Xcode 8中Interface Builder中的大小类

For example Compact width, Regular height this variation or size class is for all iphones in portrait mode! 例如, Compact width, Regular height此变化或尺寸等级适用于纵向模式下的所有iphone!

you can refer Apple documentation for more details about size classes! 有关尺寸等级的更多详细信息,请参阅Apple文档

Second thing you can do that is set autoshrink to minimum font scale and set that font scale between 0 to 1 . 你可以做的第二件事是将自动autoshrink设置为minimum font scale ,并将字体比例设置在0 to 1之间。

and then set larger font(maximum font size that you want to show in plus or pro devices for your app) in your interface builder. 然后在界面构建器中设置较大的字体(您希望在应用的加号或专业设备中显示的最大字体大小)。 Now, when your app will get open in small size device, your font size will be try to reduce with that minimum scale factor. 现在,当您的应用程序在小型设备中打开时,您的字体大小将尝试使用该最小比例因子进行缩减。 for example if you have set scale factor 0.5 and your font size is 100 in storyboard then in small device it will try to reduce font size till 50 to fit in label or textfield. 例如,如果您在故事板中设置了比例因子0.5并且字体大小为100,则在小型设备中它将尝试将字体大小减小到50以适合标签或文本字段。

在此输入图像描述

Create category class of UILabel or any other control (UIButton , UITextField , etc). 创建UILabel的类别类或任何其他控件(UIButton,UITextField等)。

UILabel+DynamicFontSize.h 的UILabel + DynamicFontSize.h

 #import <UIKit/UIKit.h>

 @interface UILabel (DynamicFontSize)
 @property (nonatomic) IBInspectable BOOL adjustFontSize;
 @end

UILabel+DynamicFontSize.m 的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

Usage 用法

Select any label and find property name Adjust Font Size change value to ON 选择任何标签并查找属性名称调整字体大小更改值为ON

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM