1. 程式人生 > >swiftswift3.0自己封裝的快速構建頁面的方法

swiftswift3.0自己封裝的快速構建頁面的方法

//#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

ifIS_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))

        }

ifIS_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))

        }

ifIS_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))

        }

ifIS_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

    }