Indent the text in a UITextField

后端 未结 8 1315
日久生厌
日久生厌 2020-12-07 08:35

My question is as simple as the title, but here\'s a little more:

I have a UITextField, on which I\'ve set the background image. The problem is that the text hugs so

8条回答
  •  佛祖请我去吃肉
    2020-12-07 09:07

    My 2 cents:

    class PaddedTextField : UITextField {
        var insets = UIEdgeInsets.zero
        var verticalPadding:CGFloat = 0
        var horizontalPadding:CGFloat = 0
    
        override func textRect(forBounds bounds: CGRect) -> CGRect {
            return CGRect(x: bounds.origin.x + insets.left, y: bounds.origin.y + insets.top, width: bounds.size.width - (insets.left + insets.right), height: bounds.size.height - (insets.top + insets.bottom));
        }
    
        override func editingRect(forBounds bounds: CGRect) -> CGRect {
            return textRect(forBounds: bounds)
        }
    }
    

提交回复
热议问题