swift2 使用自定义样式在Swift中创建UITextField扩展

ifmq2ha2  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(382)

我在视图上有几个UITextFields必须看起来相同。我发现我可以创建一个扩展,它将预先设置我的文本字段(我希望具有相同样式的字段)的样式。

let passTextField: UITextField = {
    let tf = UITextField()
    //tf.backgroundColor = UIColor.blueColor()
    tf.translatesAutoresizingMaskIntoConstraints = false
    tf.layer.cornerRadius = 25
    tf.layer.borderColor = UIColor(r: 34, g: 140, b: 204, a: 1).CGColor
    tf.layer.borderWidth = 2.0
    tf.layer.masksToBounds = true
    /* Paddings */
    tf.leftView = UIView(frame: CGRectMake(0, 0, 25, 0))
    tf.leftViewMode = UITextFieldViewMode.Always
    tf.rightView = UIView(frame: CGRectMake(0, 0, 25, 0))
    tf.rightViewMode = UITextFieldViewMode.Always
    /* Place Holder Formating */
    let attributes = [
        NSForegroundColorAttributeName: UIColor(r: 34, g: 140, b: 204, a: 1),
        NSFontAttributeName : UIFont(name: "HelveticaNeue-Thin", size: 16)! // Note the !
    ]
    tf.attributedPlaceholder = NSAttributedString(string: "Email", attributes:attributes)

    return tf
}()

因此这些属性中的大多数都应该包含在扩展中,我希望在声明变量时能够添加几个属性。
你能帮我吗?我一直在寻找如何创建一个扩展,但似乎没有为我工作。
谢谢你,谢谢你

lndjwyie

lndjwyie1#

像这样创建UITextFieldextension

extension UITextField {
    class func attributedTextField(frame: CGRect) -> UITextField {
        let textField = UITextField(frame: frame)
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.layer.cornerRadius = 25
        textField.layer.borderColor = UIColor(r: 34, g: 140, b: 204, a: 1).CGColor
        textField.layer.borderWidth = 2.0
        textField.layer.masksToBounds = true
        /* Paddings */
        textField.leftView = UIView(frame: CGRectMake(0, 0, 25, 0))
        textField.leftViewMode = UITextFieldViewMode.Always
        textField.rightView = UIView(frame: CGRectMake(0, 0, 25, 0))
        textField.rightViewMode = UITextFieldViewMode.Always
        /* Place Holder Formating */
        textField attributes = [
                          NSForegroundColorAttributeName: UIColor(r: 34, g: 140, b: 204, a: 1),
                          NSFontAttributeName : UIFont(name: "HelveticaNeue-Thin", size: 16)! // Note the !
                          ]
        textField.attributedPlaceholder = NSAttributedString(string: "Email", attributes:attributes)
        return textField
    } 
}

像这样调用这个函数

let tf = UITextField.attributedTextField(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
bzzcjhmw

bzzcjhmw2#

你可以用extension定义来实现,如果方法是新的,你可以使用下面的代码,如果已经存在,生成一个override方法

extension UITextField {

    func underlined(){
        let border = CALayer()
        let width = CGFloat(1.0)
        border.borderColor = UIColor.lightGrayColor().CGColor
        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width:  self.frame.size.width, height: self.frame.size.height)
        border.borderWidth = width
        self.layer.addSublayer(border)
        self.layer.masksToBounds = true
    }
}
rsl1atfo

rsl1atfo3#

Swift 4:使用UITextField****扩展名在UITextField上**@IBOutlet**

@IBOutlet weak var normalTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    normalTextField.toStyledTextField()
}
extension UITextField {
    func toStyledTextField() { // Give Round Border and Left Placholder Padding
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 10
        self.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 0))
        self.leftViewMode = UITextField.ViewMode.always
    }
}

相关问题