xib適配(大小、字體、約束、富文本)

一句代碼適配xib,只需要傳入superview。
例如UIViewController的xib適配只需要:
[XLBTools layoutXib:self.view];
適配字體,適配約束,只需要一次調(diào)用(2022.2.7更新)
適配NSAttributedString(2019.11.25)
下面的是詳細(xì)代碼(可以直接用)。(2018.10.10更改)
Swift版:http://www.itdecent.cn/p/31ae2900853d

/** 自適應(yīng)寬高 */
#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<NSAttributedStringKey,id> * _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;
        
        /** 設(shè)置PlaceHolder */
        __block NSMutableAttributedString *placeAttributedStr = viewField.attributedPlaceholder.mutableCopy;
        [viewField.attributedPlaceholder enumerateAttributesInRange:NSMakeRange(0, viewField.placeholder.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _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<NSAttributedStringKey,id> * _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<NSAttributedStringKey,id> * _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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一句代碼適配xib,只需要傳入superview。例如UIViewController的xib適配只需要:view...
    吃貨_X閱讀 1,326評(píng)論 2 4
  • 如果時(shí)間緊,任務(wù)重,要進(jìn)行快速開(kāi)發(fā),xib是首選,而且可視化.代碼和xib兩種方式各有優(yōu)劣,在此不做討論.下面說(shuō)下...
    落寞King閱讀 1,505評(píng)論 0 0
  • iOS設(shè)計(jì)和研發(fā)過(guò)程中,UI給的圖都是基于iPhone6的標(biāo)注圖,在不同的尺寸的設(shè)備上顯示控件的位置和文字大小就要...
    藍(lán)色的風(fēng)閱讀 4,167評(píng)論 4 4
  • 前言:在咱們iOS平時(shí)的開(kāi)發(fā)過(guò)程中,難免會(huì)跟xib打交道,所以掌握xib適配就成了非純代碼開(kāi)發(fā)人員必須學(xué)會(huì)的一門(mén)技...
    圣僧留步閱讀 1,326評(píng)論 0 0
  • 很多時(shí)候我們會(huì)使用Xib開(kāi)發(fā)界面,如果是Xib創(chuàng)建的,我們?cè)偃ッ總€(gè)控件都重新都設(shè)置一遍字體,豈不是無(wú)端增加工作量,...
    趙哥窟閱讀 1,448評(píng)論 0 1

友情鏈接更多精彩內(nèi)容