美文网首页
XIB 根据屏幕比例适配

XIB 根据屏幕比例适配

作者: 阿栋先森 | 来源:发表于2020-05-09 15:04 被阅读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
    

    相关文章

      网友评论

          本文标题:XIB 根据屏幕比例适配

          本文链接:https://www.haomeiwen.com/subject/peyknhtx.html