您的位置:首页 > 移动开发 > IOS开发

[置顶] iOS适配不同屏幕下的字体大小

2016-11-21 18:50 465 查看
Demo下载地址

我们经常会遇到这样的事情,就是在iPhone6上宽度刚刚好的文字,到了iPhone6P上却变小了,到iPhone5S小屏幕上却变大了,这时有可能还导致宽度不够从而变成省略号了,这就是固定死了字体,从而导致了不同屏幕上看着字体不适应了。如何来解决这个问题呢,目前博主比较了一下,目前最大屏幕的plus宽度是iPhone6 的1.5倍,是iPhone5S的2倍左右,因此博主采取的原理就是:

根据不同屏幕尺寸设置不同的字体大小,如下方法中获得屏幕尺寸来判断设置最大屏字体也是最大,当屏幕减小时,也将字体相应的减小,但是目前博主没法判断这个宽度倍数和字体大小倍数去缩放到相应的比例,去完美的进行适配字体,望大家给出建议,以追求达到最完美的屏幕与自体之间的适配,感激不尽,谢谢!

#import "ViewController.h"

@interface ViewController ()

@end

#pragma mark - 屏幕适配
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHieight [UIScreen mainScreen].bounds.size.height
#define f_CalcRealWidthByiPhone6(widthForiPhone6) widthForiPhone6/375.0f*ScreenWidth
#define f_CalcRealHeightByiPhone6(heightForiPhone6) heightForiPhone6/667.0f*ScreenHieight
#define HIGH_RESOLUTION4             ([UIScreen mainScreen].bounds.size.height ==480)
#define HIGH_RESOLUTION5             ([UIScreen mainScreen].bounds.size.height ==568)
#define HIGH_RESOLUTION6             ([UIScreen mainScreen].bounds.size.height ==667)
#define HIGH_RESOLUTION6Plus         ([UIScreen mainScreen].bounds.size.height ==736)

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self setLabel];
}

- (void)setLabel
{
UIButton *btn_eva = [UIButton buttonWithType:UIButtonTypeCustom];
btn_eva.frame = CGRectMake(100, 200, f_CalcRealWidthByiPhone6(60), 30);
[btn_eva setTitle:@"我的评价" forState:UIControlStateNormal];
[btn_eva setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn_eva.layer.cornerRadius = 2.0;
btn_eva.layer.masksToBounds = YES;
btn_eva.titleLabel.font = [self setAutoFont];
btn_eva.backgroundColor = [UIColor greenColor];
btn_eva.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.view addSubview:btn_eva];
CGFloat fontSize = btn_eva.titleLabel.font.pointSize;
NSLog(@"字体大小--%f",fontSize);
//    lbl_name.font = [UIFont systemFontOfSize:fontSize*SizeScale];
}

- (UIFont *)setAutoFont
{
if (HIGH_RESOLUTION4)
{
UIFont *font = [UIFont systemFontOfSize:12];
return font;
}
else if (HIGH_RESOLUTION5)
{
UIFont *font = [UIFont systemFontOfSize:12];
return font;
}
else if (HIGH_RESOLUTION6)
{
UIFont *font = [UIFont systemFontOfSize:14];
return font;
}
//iPhone6P、iPhone6SP、iPhone7P
else
{
UIFont *font = [UIFont systemFontOfSize:15];
return font;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: