Maysam
Maysam

Reputation: 7367

CSS like Padding for UITextField

It's my problem:
enter image description here
Look at the first character, bottom-left of k is not shown.

I tried this:

- (CGRect)textRectForBounds:(CGRect)bounds{
    return CGRectInset( bounds , 20 , 20 );
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return CGRectInset( bounds , 20 , 20 );
}

And also this:

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 10, 20)] ;
myText.leftView = paddingView;
myText.leftViewMode = UITextFieldViewModeAlways;

but no chance!

Upvotes: 0

Views: 335

Answers (2)

Suresh Varma
Suresh Varma

Reputation: 9740

Sorry for being so late in answering but may be my answer can help someone looking out for similar solution

Here is what I do for padding. Add the below code at the top of the .m file where you need the paading

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"

@implementation UITextField (custom)
- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 8,
                  bounds.size.width - 20, bounds.size.height - 16);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [self textRectForBounds:bounds];
}
@end

#pragma clang diagnostic pop

P.S. The directives are used to prevent from warning about categories overriding methods.

Upvotes: 1

Neeku
Neeku

Reputation: 3653

Not sure if this is a good solution, but what I had done once I'd faced something like this, was to append a blank space to the beginning of the string, that fixed the issue, starting the text with a bit of indention. stringByAppendingString.

Upvotes: 1

Related Questions