如何在 Swift 中增加 plus 设备上的字体和大小? [英] How to increase fonts and sizes on plus device in Swift?

查看:11
本文介绍了如何在 Swift 中增加 plus 设备上的字体和大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观察了一些流行的应用程序.当我们比较 iPhone Plus 设备和普通设备时,字体和图像是不同的.在 iPhone Plus 设备中稍大一点.我们如何在我们的 iOS 应用程序中实现相同的目标?我已经使用了启动画面.但字体仍然相同,加号和普通设备没有区别.

I observed some popular apps. When we compare iPhone Plus devices and normal devices, fonts and images are varying. A little bit bigger in iPhone Plus device. How can we achieve same in our iOS applications? 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.

推荐答案

只需为每个控制器创建自定义类.下面,我为 UIButton 创建 csutom 类,你也可以为其他控件创建相同的类.

Just create custom class for each controller. Below, I am create csutom class for UIButton, You can also create same for other control.

HSCustomButton.h

#import <UIKit/UIKit.h>

@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

只需更改故事板中的类名,然后为所有其他设备自动缩放字体.

Just change class name in storyboard, then font scale automatically for all other device.

这篇关于如何在 Swift 中增加 plus 设备上的字体和大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆