xib适配(大小、字体、约束、富文本)

一句代码适配xib,只需要传入superview。
例如UIViewController的xib适配只需要:
[XLBTools layoutXib:self.view];
适配字体,适配约束,只需要一次调用(2022.2.7更新)
适配NSAttributedString(2019.11.25)
下面的是详细代码(可以直接用)。(2018.10.10更改)
Swift版:https://www.jianshu.com/p/31ae2900853d

/** 自适应宽高 */
#define ADAPTATION_WIDTH(Width) [UIScreen mainScreen].bounds.size.width * (Width) / 375.0

/// XIB适配
+ (void)layoutXib:(UIView *)viewLayout{
    for (NSLayoutConstraint *constant in viewLayout.constraints) {
        if (constant.constant > 0.5) {
            constant.constant = ADAPTATION_WIDTH(constant.constant);
        }
    }
    [self layoutChildView:viewLayout];
}


+ (void)layoutView:(UIView *)viewLayout{
    CGRect returnRect;
    returnRect.size.width = ADAPTATION_WIDTH(viewLayout.frame.size.width);
    returnRect.size.height = ADAPTATION_WIDTH(viewLayout.frame.size.height);
    returnRect.origin.x = ADAPTATION_WIDTH(viewLayout.frame.origin.x);
    returnRect.origin.y = ADAPTATION_WIDTH(viewLayout.frame.origin.y);
    viewLayout.frame = returnRect;
    if ([viewLayout isKindOfClass:[UITextField class]]) {
        UITextField *viewField = (UITextField *)viewLayout;
        UIFont *newFont = [self getFontWith:viewField.font];
        viewField.font = newFont;
        MJWeakSelf
        __block NSMutableAttributedString *attributedStr = viewField.attributedText.mutableCopy;
        [viewField.attributedText enumerateAttributesInRange:NSMakeRange(0, viewField.text.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
            [attributedStr removeAttribute:NSFontAttributeName range:range];
            UIFont *font = attrs[@"NSFont"];
            UIFont *setFont = [weakSelf fontIsEqual:newFont font1:font] ? newFont : [self getFontWith:font];
            [attributedStr addAttribute:NSFontAttributeName value:setFont range:range];
        }];
        viewField.attributedText = attributedStr;
        
        /** 设置PlaceHolder */
        __block NSMutableAttributedString *placeAttributedStr = viewField.attributedPlaceholder.mutableCopy;
        [viewField.attributedPlaceholder enumerateAttributesInRange:NSMakeRange(0, viewField.placeholder.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
            [placeAttributedStr removeAttribute:NSFontAttributeName range:range];
            UIFont *font = attrs[@"NSFont"];
            UIFont *setFont = [weakSelf fontIsEqual:newFont font1:font] ? newFont : [self getFontWith:font];
            [placeAttributedStr addAttribute:NSFontAttributeName value:setFont range:range];
        }];
        viewField.attributedPlaceholder = placeAttributedStr;
        
    }else if([viewLayout isKindOfClass:[UITextView class]]){
        UITextView *textView = (UITextView *)viewLayout;
        UIFont *newFont = [self getFontWith:textView.font];
        textView.font = newFont;
        MJWeakSelf
        __block NSMutableAttributedString *attributedStr = textView.attributedText.mutableCopy;
        [textView.attributedText enumerateAttributesInRange:NSMakeRange(0, textView.text.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
            [attributedStr removeAttribute:NSFontAttributeName range:range];
            UIFont *font = attrs[@"NSFont"];
            UIFont *setFont = [weakSelf fontIsEqual:newFont font1:font] ? newFont : [self getFontWith:font];
            [attributedStr addAttribute:NSFontAttributeName value:setFont range:range];
        }];
        textView.attributedText = attributedStr;
        
    }else if ([viewLayout isKindOfClass:[UILabel class]]){
        UILabel *viewLabel = (UILabel *)viewLayout;
        UIFont *newFont = [self getFontWith:viewLabel.font];
        viewLabel.font = newFont;
        MJWeakSelf
        __block NSMutableAttributedString *attributedStr = viewLabel.attributedText.mutableCopy;
        [viewLabel.attributedText enumerateAttributesInRange:NSMakeRange(0, viewLabel.text.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
            [attributedStr removeAttribute:NSFontAttributeName range:range];
            UIFont *font = attrs[@"NSFont"];
            UIFont *setFont = [weakSelf fontIsEqual:newFont font1:font] ? newFont : [self getFontWith:font];
            [attributedStr addAttribute:NSFontAttributeName value:setFont range:range];
        }];
        viewLabel.attributedText = attributedStr;
    }else if ([viewLayout isKindOfClass:[UIButton class]]){
        UIButton *viewButton = (UIButton *)viewLayout;
        viewButton.titleLabel.font = [self getFontWith:viewButton.titleLabel.font];
    }
}

+ (BOOL)fontIsEqual:(UIFont *)font font1:(UIFont *)font1{
    if ([font.fontName isEqual:font1.fontName] && font.pointSize == font1.pointSize) {
        return YES;
    } else {
        return NO;
    }
}

// 递归获取子视图
+ (void)layoutChildView:(UIView *)view{
    NSArray *subviews = [view subviews];
    if ([subviews count] == 0) return;
    for (UIView *subview in subviews) {
        [self layoutXib:subview];
        [self layoutView:subview];
    }
}

//Font适配
+ (UIFont*)getFontWith:(UIFont*)font {
    CGFloat fontSize = font.pointSize;
    NSString *fontName = font.fontName;
    NSString *fontType = font.fontName;
    if ([fontName hasSuffix:@"Medium"]) {
        fontType = @"PingFangSC-Medium";
    } else if ([fontName hasSuffix:@"Regular"]) {
        fontType = @"PingFangSC-Regular";
    }else if ([fontName hasSuffix:@"Semibold"]) {
        fontType = @"PingFangSC-Semibold";
    }else if ([fontName hasSuffix:@"Bold"]) {
        fontType = @"Helvetica-Bold";
    } else if ([fontName hasSuffix:@"Light"]) {
        fontType = @"PingFangSC-Light";
    } else if ([fontName hasSuffix:@"Ultralight"]) {
        fontType = @"PingFangSC-Ultralight";
    } else if ([fontName hasSuffix:@"Thin"]) {
        fontType = @"PingFangSC-Thin";
    }
    return [UIFont fontWithName:fontType size:ADAPTATION_WIDTH(fontSize)];
}

xib原图


屏幕快照 2018-07-05 21.43.30.png

8效果图


屏幕快照 2018-07-05 21.45.54.png

8p效果图


屏幕快照 2018-07-05 21.44.07.png

iPhone X效果图


屏幕快照 2018-07-05 21.44.28.png

iPhone XR 图


Simulator Screen Shot - iPhone XR - 2018-10-17 at 10.40.26.png

iPhone XS Max 图


Simulator Screen Shot - iPhone XS Max - 2018-10-17 at 10.30.29.png

你可能感兴趣的:(xib适配(大小、字体、约束、富文本))