How do you create textfield padding in Swift 4?

后端 未结 3 1823
太阳男子
太阳男子 2021-02-06 04:59

I have a textfield called

nameTextField

I rounded the corners with the

nameTexfield.layer.cornerRadius = 5

相关标签:
3条回答
  • 2021-02-06 05:15

    Better to subclass the textview than to add a left view incase you are planning to add an image as a left view.

    class UITextFieldPadding: UITextField {
    
        let padding = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 0)
    
        override open func textRect(forBounds bounds: CGRect) -> CGRect {
            return bounds.inset(by: padding)
        }
    
        override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
            return bounds.inset(by: padding)
        }
    
        override open func editingRect(forBounds bounds: CGRect) -> CGRect {
            return bounds.inset(by: padding)
        }
    }
    
    0 讨论(0)
  • 2021-02-06 05:19

    As @the4kman says, Swift does support CGRect but the syntax may have changed.

    You can try this for instance:

    @IBOutlet weak var nameTextField: UITextField! {
        didSet {
            nameTextField.layer.cornerRadius =  5
            nameTextField.layer.borderColor = UIColor.black.cgColor
            nameTextField.layer.borderWidth = 1
            let leftView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 2.0))
            nameTextField.leftView = leftView
            nameTextField.leftViewMode = .always
        }
    }
    

    If I do that, I get this fine result

    Hope that helps.

    Update

    You ask for a function instead of setting it in didSet and sure, thats possible, something like:

    func addPaddingAndBorder(to textfield: UITextField) {
        textfield.layer.cornerRadius =  5
        textfield.layer.borderColor = UIColor.black.cgColor
        textfield.layer.borderWidth = 1
        let leftView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 2.0))
        textfield.leftView = leftView
        textfield.leftViewMode = .always
    }
    

    and then you' call that in viewDidLoad for instance like so:

    override func viewDidLoad() {
        super.viewDidLoad()
        addPaddingAndBorder(to: nameTextField)
    }
    
    0 讨论(0)
  • 2021-02-06 05:19

    Customized way to add padding in "left", "right" or "both" side od UITextField.

    Step 1:- Add this UITextfield extension

    extension UITextField {
    
        enum PaddingSide {
            case left(CGFloat)
            case right(CGFloat)
            case both(CGFloat)
        }
    
        func addPadding(_ padding: PaddingSide) {
    
            self.leftViewMode = .always
            self.layer.masksToBounds = true
    
    
            switch padding {
    
            case .left(let spacing):
                let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: spacing, height: self.frame.height))
                self.leftView = paddingView
                self.rightViewMode = .always
    
            case .right(let spacing):
                let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: spacing, height: self.frame.height))
                self.rightView = paddingView
                self.rightViewMode = .always
    
            case .both(let spacing):
                let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: spacing, height: self.frame.height))
                // left
                self.leftView = paddingView
                self.leftViewMode = .always
                // right
                self.rightView = paddingView
                self.rightViewMode = .always
            }
        }        
    }
    

    Step 2: How to use

    // 1.  To add left padding
    yourTextFieldName.addPadding(.left(20)) 
    
    // 2.  To add right padding
    yourTextFieldName.addPadding(.right(20))
    
    // 3. To add left & right padding both
    yourTextFieldName.addPadding(.both(20))
    
    0 讨论(0)
提交回复
热议问题