swift 自定义TextField 的边框颜色和 左右 视图

import UIKit


class HZXTextField: UITextField {

//swift的二次构造

    override init(frame: CGRect) {

        super.init(frame: frame)

    }

//便利构造 重写 init方法



 convenience  init(var leftView: UIView?,frame: CGRect,var rightView: UIView?,var color:UIColor?,boardWidth:CGFloat,boardRadius : CGFloat)

    {

        self.init(frame:frame)

        self.leftViewMode = UITextFieldViewMode.Always

        self.leftViewMode = UITextFieldViewMode.Always

        self.leftView = leftView

        self.rightView = rightView

        if color == nil

        {

          //默认一个颜色

            color = UIColor(red: 0.2588, green: 0.7490, blue: 0.6471, alpha: 1)

        }

        self.layer.borderColor = color?.CGColor

         //边框宽度

        self.layer.borderWidth = boardWidth

        self.layer.masksToBounds = true

        //边框圆角

        self.layer.cornerRadius = boardRadius  

    }

    required init(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

}


//直接调用即可

 override func  viewDidLoad()

    {

      

        

        var huTF = HZXTextField(leftView: nil, frame: CGRectMake(40, 420, 200, 40), rightView: nil, color: UIColor.orangeColor(), boardWidth: 0.8, boardRadius: 8)

        self.view.addSubview(huTF)

       

    }

    



你可能感兴趣的:(iOS,Swift,自定义textField)