iOS应用如何减少UI代码-用此方法

iOS应用如何减少UI代码-用此方法,第1张

extension UIView {

    ///获取fram相关

    var maxX: CGFloat { return frame.maxX }

    var maxY: CGFloat { return frame.maxY }

    var minX: CGFloat { return frame.minX }

    var minY: CGFloat { return frame.minY }

    var w: CGFloat { return frame.width }

    var h: CGFloat { return frame.height }

    var x: CGFloat { return frame.origin.x }

    var y: CGFloat { return frame.origin.y }

    ///设置视图

    func setView(_ frame: CGRect, _ backgroundColor: UIColor) {

        self.frame = frame

        self.backgroundColor = backgroundColor

    }

    ///添加视图

    func view(_ frame: CGRect) -> UIView {

        let view = UIView(frame: frame);addSubview(view);return view

    }

    func view(_ frame: CGRect, _ backgroundColor: UIColor) -> UIView {

        let view = UIView(frame: frame)

        view.backgroundColor = backgroundColor

        addSubview(view)

        return view

    }

    func view(_ frame: CGRect, _ backgroundColor: UIColor, _ radius: CGFloat) -> UIView {

        let view = UIView(frame: frame); view.backgroundColor = backgroundColor

        view.corner(radius); addSubview(view); return view

    }

    func view(_ frame: CGRect, _ backgroundColor: UIColor, _ radius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor) -> UIView {

        let view = UIView(frame: frame); view.backgroundColor = backgroundColor

        view.cornerBorder(radius, borderWidth, borderColor);

        addSubview(view); return view

    }

    ///添加滚动视图

    func scrollView(_ frame: CGRect, _ backgroundColor: UIColor) -> UIScrollView {

        let sv = UIScrollView(frame: frame)

        sv.backgroundColor = backgroundColor

        if #available(iOS 11.0, *) { sv.contentInsetAdjustmentBehavior = .never }

        addSubview(sv)

        return sv

    }

    ///添加label

    func label(_ frame: CGRect) -> UILabel {

        let label = UILabel(frame: frame)

        addSubview(label)

        return label

    }

    func label(_ frame: CGRect, _ textColor: UIColor, _ font: UIFont, _ textAlignment: NSTextAlignment) -> UILabel {

        let label = UILabel(frame: frame)

        label.setLabel(textColor, font, textAlignment)

        addSubview(label)

        return label

    }

    func label(_ frame: CGRect, _ text: String, _ textColor: UIColor, _ font: UIFont, _ textAlignment: NSTextAlignment, _ numberOfLines: Int) -> UILabel {

        let label = UILabel(frame: frame)

        label.setLabel(text, textColor, font, textAlignment)

        label.numberOfLines = numberOfLines

        addSubview(label)

        return label

    }

    func label(_ frame: CGRect, _ text: String, _ textColor: UIColor, _ font: UIFont, _ textAlignment: NSTextAlignment) -> UILabel {

        let label = UILabel(frame: frame)

        label.setLabel(text, textColor, font, textAlignment)

        addSubview(label)

        return label

    }

    func label(_ frame: CGRect, _ attributeText: NSAttributedString, _ textAlignment: NSTextAlignment) -> UILabel {

        let label = UILabel(frame: frame)

        label.setLabel(attributeText, textAlignment)

        addSubview(label)

        return label

    }

    func label(_ frame: CGRect, _ backgroundColor: UIColor, _ text: String, _ textColor: UIColor, _ font: UIFont, _ textAlignment: NSTextAlignment) -> UILabel {

        let label = UILabel(frame: frame)

        label.setLabel(text, textColor, font, textAlignment)

        label.backgroundColor = backgroundColor

        addSubview(label)

        return label

    }

    ///添加按钮 返回YLButton

    func button(_ frame: CGRect) -> YLButton {

        let button = YLButton(frame: frame)

        addSubview(button)

        return button

    }

    func button(_ frame: CGRect, _ target: Any?, _ action: Selector) -> YLButton {

        let button = YLButton(frame: frame)

        button.addTarget(target, action: action, for: .touchUpInside)

        addSubview(button)

        return button

    }

    func button(_ frame: CGRect, _ backgroundColor: UIColor, _ tag: Int, _ target: Any?, _ action: Selector) -> YLButton {

        let button = YLButton(frame: frame)

        button.backgroundColor = backgroundColor

        button.tag = tag

        button.addTarget(target, action: action, for: .touchUpInside)

        addSubview(button)

        return button

    }

    func button(_ frame: CGRect, _ tag: Int, _ target: Any?, _ action: Selector) -> YLButton {

        let button = YLButton(frame: frame)

        button.tag = tag

        button.addTarget(target, action: action, for: .touchUpInside)

        addSubview(button)

        return button

    }

    func button(_ frame: CGRect, _ isSelected: Bool, _ target: Any?, _ action: Selector) -> YLButton {

        let button = YLButton(frame: frame)

        button.tag = tag

        button.isSelected = isSelected

        button.addTarget(target, action: action, for: .touchUpInside)

        addSubview(button)

        return button

    }

    func button(_ frame: CGRect, _ isSelected: Bool, _ tag: Int, _ target: Any?, _ action: Selector) -> YLButton {

        let button = YLButton(frame: frame)

        button.tag = tag

        button.isSelected = isSelected

        button.addTarget(target, action: action, for: .touchUpInside)

        addSubview(button)

        return button

    }

    func button(_ frame: CGRect, _ backgroundColor: UIColor, _ tag: Int, _ radius: CGFloat, _ target: Any?, _ action: Selector) -> YLButton {

        let button = YLButton(frame: frame)

        button.addTarget(target, action: action, for: .touchUpInside)

        button.corner(radius)

        button.backgroundColor = backgroundColor

        button.tag = tag;addSubview(button);return button

    }

    func button(_ frame: CGRect, _ radius: CGFloat, _ tag: Int, _ target: Any?, _ action: Selector) -> YLButton {

        let button = YLButton(frame: frame)

        button.addTarget(target, action: action, for: .touchUpInside)

        button.corner(radius)

        button.tag = tag

        addSubview(button)

        return button

    }

    func button(_ frame: CGRect, _ tag: Int, _ radius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor, _ target: Any?, _ action: Selector) -> YLButton {

        let button = YLButton(frame: frame)

        button.addTarget(target, action: action, for: .touchUpInside)

        button.cornerBorder(radius, borderWidth, borderColor)

        button.tag = tag;addSubview(button);return button

    }

    func button(_ frame: CGRect, _ radius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor, _ target: Any?, _ action: Selector) -> YLButton {

        let button = YLButton(frame: frame)

        button.addTarget(target, action: action, for: .touchUpInside)

        button.cornerBorder(radius, borderWidth, borderColor)

        addSubview(button);return button

    }

    ///添加按钮 返回UIButton

    func uiButton(_ frame: CGRect, _ target: Any?, _ action: Selector) -> UIButton {

        let button = UIButton(frame: frame)

        button.addTarget(target, action: action, for: .touchUpInside)

        addSubview(button);return button

    }

    func uiButton(_ frame: CGRect, _ backgroundColor: UIColor, _ target: Any?, _ action: Selector) -> UIButton {

        let button = UIButton(frame: frame)

        button.backgroundColor = backgroundColor

        button.addTarget(target, action: action, for: .touchUpInside)

        addSubview(button)

        return button

    }

    func uiButton(_ frame: CGRect, _ backgroundColor: UIColor, _ tag: Int, _ target: Any?, _ action: Selector) -> UIButton {

        let button = UIButton(frame: frame); button.tag = tag

        button.backgroundColor = backgroundColor

        button.addTarget(target, action: action, for: .touchUpInside)

        addSubview(button); return button

    }

    func uiButton(_ frame: CGRect, _ tag: Int, _ target: Any?, _ action: Selector) -> UIButton {

        let button = UIButton(frame: frame); button.tag = tag

        button.addTarget(target, action: action, for: .touchUpInside)

        addSubview(button); return button

    }

    func uiButton(_ frame: CGRect, _ backgroundColor: UIColor, _ radius: CGFloat, _ target: Any?, _ action: Selector) -> UIButton {

        let button = UIButton(frame: frame)

        button.backgroundColor = backgroundColor

        button.addTarget(target, action: action, for: .touchUpInside)

        button.corner(radius)

        addSubview(button)

        return button

    }

    func uiButton(_ frame: CGRect, _ backgroundColor: UIColor, _ tag: Int, _ radius: CGFloat, _ target: Any?, _ action: Selector) -> UIButton {

        let button = UIButton(frame: frame); button.tag = tag

        button.backgroundColor = backgroundColor

        button.addTarget(target, action: action, for: .touchUpInside)

        button.corner(radius)

        addSubview(button)

        return button

    }

    func uiButton(_ frame: CGRect, _ tag: Int, _ radius: CGFloat, _ target: Any?, _ action: Selector) -> UIButton {

        let button = UIButton(frame: frame); button.tag = tag

        button.addTarget(target, action: action, for: .touchUpInside)

        button.corner(radius)

        addSubview(button)

        return button

    }

    func uiButton(_ frame: CGRect, _ radius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor, _ target: Any?, _ action: Selector) -> UIButton {

        let button = UIButton(frame: frame)

        button.addTarget(target, action: action, for: .touchUpInside)

        button.cornerBorder(radius, borderWidth, borderColor)

        addSubview(button)

        return button

    }

    func uiButton(_ frame: CGRect, _ radius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor, _ tag: Int, _ target: Any?, _ action: Selector) -> UIButton {

        let button = UIButton(frame: frame)

        button.addTarget(target, action: action, for: .touchUpInside)

        button.cornerBorder(radius, borderWidth, borderColor)

        button.tag = tag

        addSubview(button)

        return button

    }

    ///添加输入框

    func textField(_ frame: CGRect,  _ text: String, _ placeholder: String, _ textColor: UIColor, _ textFont: UIFont, _ textAlignment: NSTextAlignment, _ adaptView: UIView) -> HWKeyBoardTextField {

        let tf = HWKeyBoardTextField(frame: frame)

        tf.adaptView(adaptView, beyondY: contentMargin)

        tf.setTextField(text, textColor, textFont, textAlignment, placeholder)

        addSubview(tf)

        return tf

    }

    func textField(_ frame: CGRect,  _ text: String, _ placeholder: String, _ textColor: UIColor, _ textFont: UIFont, _ textAlignment: NSTextAlignment, _ keyboard: UIKeyboardType, _ adaptView: UIView) -> HWKeyBoardTextField {

        let tf = HWKeyBoardTextField(frame: frame)

        tf.adaptView(adaptView, beyondY: contentMargin)

        tf.setTextField(text, textColor, textFont, textAlignment, placeholder, keyboard)

        addSubview(tf)

        return tf

    }

    func textField(_ frame: CGRect, _ placeholder: String, _ textColor: UIColor, _ textFont: UIFont, _ textAlignment: NSTextAlignment, _ keyboard: UIKeyboardType, _ adaptView: UIView) -> HWKeyBoardTextField {

        let tf = HWKeyBoardTextField(frame: frame)

        tf.adaptView(adaptView, beyondY: contentMargin)

        tf.setTextField(textColor, textFont, textAlignment, placeholder, keyboard)

        addSubview(tf)

        return tf

    }

    func textField(_ frame: CGRect, _ isEnable: Bool, _ text: String, _ placeholder: String, _ textColor: UIColor, _ textFont: UIFont, _ textAlignment: NSTextAlignment, _ keyboard: UIKeyboardType, _ adaptView: UIView) -> HWKeyBoardTextField {

        let tf = HWKeyBoardTextField(frame: frame)

        tf.isEnabled = isEnable

        tf.adaptView(adaptView, beyondY: contentMargin)

        tf.setTextField(text, textColor, textFont, textAlignment, placeholder, keyboard)

        addSubview(tf)

        return tf

    }

    ///添加图片视图

    func imageView(_ frame: CGRect) -> UIImageView {

        let iv = UIImageView(frame: frame)

        addSubview(iv);return iv

    }

    func imageView(_ frame: CGRect, _ image: String, _ contentMode: ContentMode) -> UIImageView {

        let iv = UIImageView(frame: frame)

        iv.setImage(name: image, contentMode: contentMode)

        addSubview(iv)

        return iv

    }

    func imageView(_ frame: CGRect, _ image: String, _ radius: CGFloat) -> UIImageView {

        let iv = UIImageView(frame: frame)

        iv.setImage(name: image, contentMode: .scaleAspectFit)

        iv.corner(radius)

        addSubview(iv)

        return iv

    }

    func imageView(_ frame: CGRect, _ backgroundColor: UIColor, _ image: String, _ radius: CGFloat) -> UIImageView {

        let iv = UIImageView(frame: frame)

        iv.backgroundColor = backgroundColor

        iv.setImage(name: image, contentMode: .scaleAspectFit)

        iv.corner(radius);addSubview(iv);return iv

    }

    ///添加转换器 width: 51, height: 31

    func uiSwitch(_ frame: CGRect, _ onTintColor: UIColor, _ isOn: Bool, _ target: Any?, _ action: Selector) -> UISwitch {

        let aSwitch = UISwitch(frame: frame)

        aSwitch.onTintColor = onTintColor

        aSwitch.isOn = isOn

        aSwitch.addTarget(target, action: action, for: .valueChanged)

        addSubview(aSwitch)

        return aSwitch

    }

    ///添加手势

    func addTapGuesture(_ numberOfTouchs: Int, _ target: Any, _ action: Selector) {

        self.isUserInteractionEnabled = true

        let tap = UITapGestureRecognizer()

        tap.numberOfTouchesRequired = numberOfTouchs//手指个数

        tap.addTarget(target, action: action)

        self.addGestureRecognizer(tap)

    }

  

    

    func corner(_ cornerRadius: CGFloat) {

        self.layer.masksToBounds = true

        self.layer.cornerRadius = cornerRadius

    }

    func cornerBorder(_ cornerRadius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor) {

        self.layer.masksToBounds = true

        self.layer.cornerRadius = cornerRadius

        self.layer.borderColor = borderColor.cgColor

        self.layer.borderWidth = borderWidth

    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/993179.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存