使用自定义样式在Swift中创建UITextField扩展 [英] Create UITextField Extension in Swift with custom styling

查看:367
本文介绍了使用自定义样式在Swift中创建UITextField扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图上有几个看起来必须相同的UITextField.我发现我可以创建一个扩展程序,该扩展程序将对文本字段(那些我希望具有相同样式的文本字段)进行预样式设置.

I have several UITextFields on the view that have to look the same. I figured out that I might create an Extension that will pre-style my text fields (those that I want to have the same style).

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
}()

因此,大多数这些属性都应包含在扩展名中,并且我希望能够在声明变量时向其中添加几个属性.

so most of these attributes should be included into the extension and I'd like to be able to add couple of them to it when I declare the variable.

你能帮我吗?我一直在寻找如何创建扩展程序,但似乎没有任何效果.

can you help me out? I've been searching on how to create an extension but nothing seems to work out for me.

谢谢!

推荐答案

像这样创建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))

这篇关于使用自定义样式在Swift中创建UITextField扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆