0

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;
}
Jasper
  • 7,031
  • 3
  • 35
  • 43
Abhishek
  • 51
  • 1
  • 9
  • This should not be that hard to answer but i don´t understand exactly what is it that you want to do? Are you using Storyboard? Edit your question and make 4 spaces before you post code, makes it more readable. – Mr UziBazooka Jun 11 '15 at 11:00
  • Yes I am using Story board I want to Apply left margin in my textfield and i follow this code but I am unable to Implement this.http://stackoverflow.com/questions/5674655/uitextfield-align-left-margin – Abhishek Jun 11 '15 at 11:04

2 Answers2

5

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

Jasper
  • 7,031
  • 3
  • 35
  • 43
0
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.

Pradumna Patil
  • 2,180
  • 3
  • 17
  • 46