UITextField is an editable area where one can use to get input from user using keyboard.
We often come to a situation where extra padding is required from the left or right depending on requirement. Padding is already provided by UITextField for textfield of border styles “RoundedRect” and “Bezel” but for other styles it’s not.
To have extra padding subclass a UITextField and override “textRectForBounds” and “editingRectForBounds”.
Below is the code snippet adding extra padding from left, you can add padding from left as well..
Code Snippet
class MYUITextField: UITextField {
required init?(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
}
override func textRectForBounds(bounds: CGRect) -> CGRect {
return CGRectMake(10, bounds.origin.y, bounds.size.width-10, bounds.size.height); // here 10 is padding or spacing from left
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
return self.textRectForBounds(bounds);
}
}
0 Comment(s)