How do you create textfield padding in Swift 4?

后端 未结 3 1822
太阳男子
太阳男子 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)
        }
    }
    

提交回复
热议问题