I want to put the left margin of a UITextField
's text at 10 px. What is the best way to do that?
You can do it by extending UITextField
class and overriding two methods:
- (CGRect)textRectForBounds:(CGRect)bounds; - (CGRect)editingRectForBounds:(CGRect)bounds;
Here is the code:
The interface in MYTextField.h
@interface MYTextField : UITextField @end
Its implementation in MYTextField.m
@implementation MYTextField static CGFloat leftMargin = 28; - (CGRect)textRectForBounds:(CGRect)bounds { bounds.origin.x += leftMargin; return bounds; } - (CGRect)editingRectForBounds:(CGRect)bounds { bounds.origin.x += leftMargin; return bounds; } @end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With