iOS 控件宽高字体大小适配方法

1.在AppDelegate.h里面设置两个方法:

  • (CGFloat)autoScaleW:(CGFloat)w;

  • (CGFloat)autoScaleH:(CGFloat)h;

2.在AppDelegate.m里面设置两个属性:
//当前屏幕与设计尺寸(iPhone6)宽度比例
@property (nonatomic, assign)CGFloat autoSizeScaleW;

//当前屏幕与设计尺寸(iPhone6)高度比例
@property (nonatomic, assign)CGFloat autoSizeScaleH;

实现自定义方法 - (void)initAutoScaleSize;
在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;中调用此方法

pragma mark - ScaleSize

  • (void)initAutoScaleSize{

    if (kScreenHeight == 480) {
    //********* 4s
    _autoSizeScaleW = kScreenWith / 375;
    _autoSizeScaleH = kScreenHeight / 667;
    }else if (kScreenHeight == 568) {
    //**********5
    _autoSizeScaleW = kScreenWith / 375;
    _autoSizeScaleH = kScreenHeight / 667;
    }else if (kScreenHeight ==667){
    //******** 6
    _autoSizeScaleW = kScreenWith / 375;
    _autoSizeScaleH = kScreenHeight / 667;
    }else if(kScreenHeight == 736){
    //********** 6p
    _autoSizeScaleW = kScreenWith / 375;
    _autoSizeScaleH = kScreenHeight / 667;
    }else{
    _autoSizeScaleW = 1;
    _autoSizeScaleH = 1;
    }

}

  • (CGFloat)autoScaleW:(CGFloat)w{

    return w * self.autoSizeScaleW;

}

  • (CGFloat)autoScaleH:(CGFloat)h{

    return h * self.autoSizeScaleH;

}

3.在pch文件中设置宏定义

define autoScaleW(width) [(AppDelegate *)[UIApplication sharedApplication].delegate autoScaleW:width]

define autoScaleH(height) [(AppDelegate *)[UIApplication sharedApplication].delegate autoScaleH:height]

4.写控件时设置字体大小:
[UIFont systemFontOfSize:autoScaleW(14)]

设置frame:
CGRectMake(CGRectGetMaxX(self.productNameLabel.frame) + 10 , self.productNameLabel.top + 3, autoScaleW(32),autoScaleW(17));

你可能感兴趣的:(iOS 控件宽高字体大小适配方法)