How to use custom UITextField in storyboards

1k Views Asked by At

Please let me know how to use this custom UITextField in my UIViewController

I am using storyboards.

#import <UIKit/UIKit.h>

@interface MYTextField : UITextField

@end


@implementation MYTextField

- (CGRect)textRectForBounds:(CGRect)bounds {
    int margin = 10;
    CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
    return inset;
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    int margin = 10;
    CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
    return inset;
}
2

There are 2 best solutions below

0
On
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;

this code add some margin to left side of text field. Try this worked for me.

0
On

Instead of using a normal UITextField, you have to use your custom MYTextField.

In code:

MYTextField *myTextField = [[MYTextField alloc] init];

If you use Interface Builder:

Select your UITextField in your Interface Builder file, go to the Identity Inspector and set the correct sub class:

enter image description here