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

一句代码适配xib,只需要传入superview。
例如UIViewController的xib适配只需要:
view.layoutXib()
OC版:https://www.jianshu.com/p/a8eb3bbdc00f

//MARK: - UIView 扩展
public extension UIView {
    
       /** xib适配 */
    func layoutXib(_ size:AdapterSize = .phone ) {
        for constant in self.constraints {
            if constant.constant > 0.5 {
                constant.constant = AdaWidth(constant.constant,size)
            }
        }
        layoutChildView(view: self)
    }
    
    fileprivate func layoutView(_ size:AdapterSize = .phone){
        var returnRect = CGRect()
        returnRect.size.width = ADAPTATION_WIDTH(self.XlbWidth, size)
        returnRect.size.height = ADAPTATION_WIDTH(self.XlbHeight, size)
        returnRect.origin.x = ADAPTATION_WIDTH(self.XlbX, size)
        returnRect.origin.y = ADAPTATION_WIDTH(self.XlbY, size)
        self.frame = returnRect
        
        if self.isKind(of: UITextField.self) {
            let viewField = self as! UITextField
            var newFont:UIFont?
            if let font = viewField.font {
                newFont = getFontWith(font)
                viewField.font = newFont
            }
            if let attributedStr:NSMutableAttributedString = viewField.attributedText?.mutableCopy() as? NSMutableAttributedString {
                attributedStr.enumerateAttributes(in: NSRange(location: 0,length: viewField.text?.utf16.count ?? 0), options: .longestEffectiveRangeNotRequired, using: { (dic, range, stop) in
                    attributedStr.removeAttribute(.font, range: range)
                    let font:UIFont = dic[NSAttributedString.Key.font] as! UIFont
                    attributedStr.addAttributes([NSAttributedString.Key.font:(font == newFont) ? newFont! : getFontWith(font)], range: range)
                })
                viewField.attributedText = attributedStr
            }
            
            /** 设置PlaceHolder */
            if viewField.attributedPlaceholder?.length ?? 0 > 0 {
                let placeAttributedStr:NSMutableAttributedString = viewField.attributedPlaceholder?.mutableCopy() as! NSMutableAttributedString
                placeAttributedStr.enumerateAttributes(in: NSRange(location: 0,length: viewField.placeholder?.utf16.count ?? 0), options: .longestEffectiveRangeNotRequired, using: { (dic, range, stop) in
                    placeAttributedStr.removeAttribute(.font, range: range)
                    let font:UIFont = dic[NSAttributedString.Key.font] as! UIFont
                    placeAttributedStr.addAttributes([NSAttributedString.Key.font:(font == newFont) ? newFont! : getFontWith(font)], range: range)
                })
                viewField.attributedPlaceholder = placeAttributedStr
            }
            
        } else if self.isKind(of: UIButton.self) {
            let viewButton = self as! UIButton
            if let font = viewButton.titleLabel?.font {
                viewButton.titleLabel!.font = getFontWith(font)
            }
        } else if self.isKind(of: UILabel.self) {
            let viewLabel = self as! UILabel
            var newFont:UIFont?
            if let font = viewLabel.font {
                newFont = getFontWith(font)
                viewLabel.font = newFont
            }
            if let attributedStr:NSMutableAttributedString = viewLabel.attributedText?.mutableCopy() as? NSMutableAttributedString {
                attributedStr.enumerateAttributes(in: NSRange(location: 0,length: viewLabel.text?.utf16.count ?? 0), options: .longestEffectiveRangeNotRequired, using: { (dic, range, stop) in
                    attributedStr.removeAttribute(.font, range: range)
                    let font:UIFont = dic[NSAttributedString.Key.font] as! UIFont
                    attributedStr.addAttributes([NSAttributedString.Key.font:(font == newFont) ? newFont! : getFontWith(font)], range: range)
                })
                viewLabel.attributedText = attributedStr
            }
        } else if self.isKind(of: UITextView.self) {
            let viewTextView  = self as! UITextView
            var newFont:UIFont?
            if let font = viewTextView.font {
                newFont = getFontWith(font)
                viewTextView.font = newFont
            }
            if let attributedStr:NSMutableAttributedString = viewTextView.attributedText.mutableCopy() as? NSMutableAttributedString {
                attributedStr.enumerateAttributes(in: NSRange(location: 0,length: viewTextView.text?.utf16.count ?? 0), options: .longestEffectiveRangeNotRequired, using: { (dic, range, stop) in
                    attributedStr.removeAttribute(.font, range: range)
                    let font:UIFont = dic[NSAttributedString.Key.font] as! UIFont
                    attributedStr.addAttributes([NSAttributedString.Key.font:(font == newFont) ? newFont! : getFontWith(font)], range: range)
                })
                viewTextView.attributedText = attributedStr
            }
        }
    }
   
    /** View转Image */
    func imageWithView() ->(UIImage) {
        let imageSize = self.bounds.size
        UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.main.scale)
        self.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
    
    var XlbX: CGFloat {
        get { return self.frame.origin.x}
        set(value) {
            var origin = self.frame.origin
            origin.x = value
            self.frame.origin = origin
        }
    }
    var XlbY: CGFloat {
        get { return self.frame.origin.y}
        set(value) {
            var origin = self.frame.origin
            origin.y = value
            self.frame.origin = origin
        }
    }
    var XlbCenterX: CGFloat {
        get { return self.center.x}
        set(value) {
            var center = self.center
            center.x = value
            self.center = center
        }
    }
    var XlbCenterY: CGFloat {
        get { return self.center.y}
        set(value) {
            var center = self.center
            center.y = value
            self.center = center
        }
    }
    var XlbWidth: CGFloat {
        get { return self.frame.size.width}
        set(value) {
            var size = self.frame.size
            size.width = value
            self.frame.size = size
        }
    }
    var XlbRight: CGFloat {
        get { return self.frame.size.width + self.frame.origin.x}
    }
    var XlbHeight: CGFloat {
        get { return self.frame.size.height}
        set(value) {
            var size = self.frame.size
            size.height = value
            self.frame.size = size
        }
    }
    var XlbBottom: CGFloat {
        get { return self.frame.size.height + self.frame.origin.y}
    }
    var XlbOrigin: CGPoint {
        get { return self.frame.origin}
        set(value) {
            self.frame.origin = value
        }
    }
    var XlbSize: CGSize {
        get { return self.frame.size}
        set(value) {
            self.frame.size = value
        }
    }
}

//递归获取子视图
private func layoutChildView(view:UIView) {
    let subViews = view.subviews
    if subViews.count == 0 {
        return
    }
    for subView in subViews {
        subView.layoutXib(.phone)
        subView.layoutView()
    }
}

func getFontWith(_ font:UIFont) ->UIFont {
    let fontSize:CGFloat! = font.pointSize
    let fontName:String! = font.fontName
    
    var fontType:FontType = .Regular
    
    if fontName.hasSuffix("Medium") {
        fontType = .Medium
    } else
    if fontName.hasSuffix("Regular") {
        fontType = .Regular
    } else
    if fontName.hasSuffix("Semibold") {
        fontType = .Semibold
    } else
    if fontName.hasSuffix("Bold") {
        fontType = .Bold
    } else
    if fontName.hasSuffix("Heavy") {
        fontType = .Heavy
    } else
    if fontName.hasSuffix("Light") {
        fontType = .Light
    } else
    if fontName.hasSuffix("Ultralight") {
        fontType = .Ultralight
    } else
    if fontName.hasSuffix("Thin") {
        fontType = .Thin
    }
    
    return FONT(fontSize,fontType)
}


/** 字体 */
public enum FontType:String{
    case Medium = "PingFangSC-Medium"
    case Thin = "PingFangSC-Thin"
    case Regular = "PingFangSC-Regular"
    case Semibold = "PingFangSC-Semibold"
    case Bold = "Helvetica-Bold"
    case Light = "PingFangSC-Light"
    case Heavy = ".SFUI-Heavy"
    case Ultralight = "PingFangSC-Ultralight"
    case Number = "DINEngschrift"
    case Saira = "Saira-Black"
}

public enum AdapterSize:CGFloat{
    ///普通设计尺寸375
    case phone = 375.0
    ///iPhoneX系列设计尺寸414
    case phoneMax = 414.0
}
/** 获取屏幕宽度 */
public var MAINSCREEN_WIDTH: CGFloat {
    get {
        return UIScreen.main.bounds.size.width
    }
}
public func ADAPTATION_WIDTH( _ value:CGFloat, _ size:AdapterSize = .phone) -> CGFloat {
    return (MAINSCREEN_WIDTH > 428 ? 428 : MAINSCREEN_WIDTH) * value / size.rawValue
}
public func FONT(_ Font:CGFloat,_ type:FontType = .Regular, size:AdapterSize = .phone) -> UIFont {
    let font:UIFont! = UIFont.init(name: type.rawValue, size:(MAINSCREEN_WIDTH > 428 ? 428 : MAINSCREEN_WIDTH) * Font / size.rawValue)
    return font
}

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