您的位置:首页 > 移动开发 > Swift

swiftswift3.0自己封装的快速构建页面的方法

2017-04-28 10:02 363 查看
//#param mark 控件

    func creatLabel(frame:CGRect,text:String,textColor:UIColor,textFont:CGFloat,textAlignment:NSTextAlignment)
-> UILabel {

        let label =
UILabel.init(frame: frame)

        label.text = text

        label.textColor = textColor

        label.font =
UIFont.systemFont(ofSize: textFont)

        label.textAlignment = textAlignment

        scorllView!.addSubview(label)

        return label

    }

    

    func creatBtn(frame:CGRect,text:String,textColorNor:UIColor,textColorHig:UIColor,textFont:CGFloat,imageNor:String,imageHig:String,btnSet:Bool,target:Selector,textAlignment
: NSTextAlignment,borderColor :
UIColor) -> UIButton {

        

        let btn =
UIButton.init(frame: frame)

        btn.setTitle(text, for: .normal)

        btn.setTitleColor(textColorNor, for: .normal)

        btn.setTitleColor(textColorHig, for: .selected)

        btn.setTitleColor(textColorHig, for: .highlighted)

        btn.setImage(UIImage.init(named: imageNor), for: .normal)

        btn.setImage(UIImage.init(named: imageHig), for: .highlighted)

        btn.setImage(UIImage.init(named: imageHig), for: .selected)

        btn.isEnabled = btnSet

        

        if
IS_IPHONE_4_OR_LESS {

            

            btn.titleEdgeInsets =
UIEdgeInsetsMake(0,
-20, 0,
40)

            btn.imageEdgeInsets =
UIEdgeInsetsMake(5,UIScreen.main.bounds.width
/ 2 - 45,
5, -(UIScreen.main.bounds.width
/ 2 - 115))

            

        }

        

        if
IS_IPHONE_5 ||
IS_IPHONE_SE{

            

            btn.titleEdgeInsets =
UIEdgeInsetsMake(0,
-20, 0,
40)

            btn.imageEdgeInsets =
UIEdgeInsetsMake(5,UIScreen.main.bounds.width
/ 2 - 45,
5, -(UIScreen.main.bounds.width
/ 2 - 115))

            

        }

        

        if
IS_IPHONE_6 ||
IS_IPHONE_7{

            

            btn.titleEdgeInsets =
UIEdgeInsetsMake(0,
-20, 0,
40)

            btn.imageEdgeInsets =
UIEdgeInsetsMake(5,UIScreen.main.bounds.width
/ 2 - 55,
5, -(UIScreen.main.bounds.width
/ 2 - 130))

            

        }

        

        if
IS_IPHONE_6P ||
IS_IPHONE_7P{

            

            btn.titleEdgeInsets =
UIEdgeInsetsMake(0,
-20, 0,
40)

            btn.imageEdgeInsets =
UIEdgeInsetsMake(5,UIScreen.main.bounds.width
/ 2 - 60,
5, -(UIScreen.main.bounds.width
/ 2 - 150))

            

            

        }

        

        

        

        btn.layer.masksToBounds =
true

        btn.layer.cornerRadius =
5

        btn.layer.borderColor = borderColor.cgColor

        btn.layer.borderWidth =
1

        btn.titleLabel?.font =
UIFont.systemFont(ofSize: textFont)

        btn.titleLabel?.textAlignment = textAlignment

        if btn.isEnabled
== true {

            

            btn.backgroundColor =
UIColor.white

        }else {

            btn.backgroundColor =
UIColor.white

    }

        

        btn.addTarget(self, action: target, for: .touchUpInside)

        

        scorllView!.addSubview(btn)

        return btn

    }

    

    

    func textfield(frame:CGRect,text:String,textFont:CGFloat,textColor:UIColor,textAlignment:NSTextAlignment,placeholder:String,borderStyle:UITextBorderStyle,borderColor
: UIColor) -> UITextField {

        

        let textfield =
UITextField.init(frame: frame)

        

        textfield.font =
UIFont.systemFont(ofSize: textFont)

        textfield.textColor = textColor

        textfield.textAlignment = textAlignment

        textfield.placeholder = placeholder

        textfield.delegate =
self

        textfield.borderStyle = borderStyle

        textfield.clearButtonMode =
UITextFieldViewMode.whileEditing
//叉号一直显示

        textfield.adjustsFontSizeToFitWidth =
true

        textfield.backgroundColor =
UIColor.white

        textfield.keyboardType =
UIKeyboardType.default

        textfield.layer.masksToBounds =
true

        textfield.layer.cornerRadius =
5

        textfield.layer.borderColor = borderColor.cgColor

        textfield.layer.borderWidth =
1

        scorllView!.addSubview(textfield)

        return textfield

    }

    

    func textFieldView(frame:CGRect,text:String,textFont:CGFloat,textColor:UIColor,textAlignment:NSTextAlignment,placeholder:String,borderColor
: UIColor) -> WTextVIew {

        

        let textFieldView =
WTextVIew.init(frame: frame)

        textFieldView.font =
UIFont.systemFont(ofSize: textFont)

        textFieldView.textColor = textColor

        textFieldView.textAlignment = textAlignment

        textFieldView.delegate =
self

        textFieldView.text = text

        textFieldView.layer.masksToBounds =
true

        textFieldView.layer.cornerRadius =
5

        textFieldView.layer.borderWidth =
1

        textFieldView.layer.borderColor = borderColor.cgColor

        textFieldView.backgroundColor =
UIColor.white

        textFieldView.placeholder = placeholder

        scorllView!.addSubview(textFieldView)

        return textFieldView

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: