XIB 根据屏幕比例适配

2020-05-09  本文已影响0人  阿栋先森

比例约束

@interface NSLayoutConstraint (IBDesignable)
@property (nonatomic)IBInspectable BOOL widthScreen;
@end
#define  C_WIDTH(WIDTH) WIDTH * [UIScreen mainScreen].bounds.size.width/375.0
#import "NSLayoutConstraint+IBDesignable.h"

@implementation NSLayoutConstraint (IBDesignable)

-(void)setWidthScreen:(BOOL)widthScreen{
    if (widthScreen) {
        self.constant = C_WIDTH(self.constant);
    }else{
        self.constant = self.constant;
    }
}

-(BOOL)widthScreen{
    return self.widthScreen;
}

@end

字体比例约束

@interface UILabel (FixScreenFont)
@property (nonatomic)IBInspectable float fixWidthScreenFont;

@end
#define  C_WIDTH(WIDTH) WIDTH * [UIScreen mainScreen].bounds.size.width/375.0

@implementation UILabel (FixScreenFont)
- (void)setFixWidthScreenFont:(float)fixWidthScreenFont{
    
    if (fixWidthScreenFont > 0 ) {
        self.font = [UIFont systemFontOfSize:C_WIDTH(fixWidthScreenFont)];
    }else{
        self.font = self.font;
    }
}

- (float )fixWidthScreenFont{
    return self.fixWidthScreenFont;
}
@end
上一篇下一篇

猜你喜欢

热点阅读