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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38850239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:07:40  来源:igfitidea点击:

Create UITextField Extension in Swift with custom styling

iosswiftxcodeswift2

提问by mrGott

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

我在视图上有几个 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
}()

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.

你能帮我吗?我一直在寻找如何创建扩展,但似乎对我没有任何帮助。

Thank you!

谢谢!

回答by ?zgür Ersil

you can do it with extensiondefinition , if the method is new you can use the code below, if already exist make an overridemethod

你可以用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
    }
}

回答by Nirav D

Create extensionof UITextFieldlike this

创建extensionUITextField这样

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

call this function like this

像这样调用这个函数

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

回答by Dary

Swift 4:Use UITextFieldExtensionon UITextField @IBOutlet

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

@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
    }
}