屏幕适配


  • 目的:为了能够使应用的显示以黄金比例运行在各个类型的苹果手机上。

1、将以下代码放入 .pch 预编译文件中。


#ifndef ScreenAdaptation_pch
#define ScreenAdaptation_pch

/************ 机子屏幕高度与宽度 *****************************/
#define CHScreenH [UIScreen mainScreen].bounds.size.height
#define CHScreenW [UIScreen mainScreen].bounds.size.width

/****** 屏幕适配,以iphone 6 Plus 为基准 **************/
#define HEIGHT_BASE 736
#define WIDTH_BASE  414

/******** 点适配 ********/
#define ScaleH(HEIGHT) CHScreenH / HEIGHT_BASE * HEIGHT
#define ScaleW(WIDTH)  CHScreenW / WIDTH_BASE * WIDTH
/******** 字体适配 ********/
#define ScaleFront(front)  CHScreenH / HEIGHT_BASE * front

#endif /* ScreenAdaptation_pch */

2、开始使用

  • 2.1 UIView的使用

    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(ScaleW(10), ScaleH(10), CHScreenW - ScaleW(20), 0.5 * CHScreenH - ScaleH(20));
    view.backgroundColor = [UIColor redColor];
    NSLog(@"%f-----%f", ScaleW(10), ScaleH(10));
    [self.view addSubview:view];
  • 2.2 字体大小的使用

UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(ScaleW(10), 0.5 * CHScreenH + ScaleH(10), CHScreenW - ScaleW(20), 0.5 * CHScreenH - ScaleH(20));
    label.text = @"屏幕字体适配呵呵呵";
    label.textAlignment = NSTextAlignmentCenter;
    [label setFont:[UIFont systemFontOfSize:ScaleFront(40)]];
    [self.view addSubview:label];

3、码云地址

4、效果图

  • 大屏幕iphone 6 Plus

  • 小屏幕iphone 5s

原文地址:https://www.cnblogs.com/CH520/p/9260053.html