2

I want to give padding from left into the textfield.I have added a background image to textfield due to which text in textfield always start from very left edge.I have tried below solution but that does not work for too many text fields.

    UIView *fieldEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    self.txt_fname.leftViewMode = UITextFieldViewModeAlways;
    self.txt_fname.leftView     = fieldEmail;

Please suggest me some simple solutin in which i don't have to create a seprate view for giving the padding.

The answer accepted in the question does not work for me. Set padding for UITextField with UITextBorderStyleNone

Community
  • 1
  • 1
TechChain
  • 8,404
  • 29
  • 103
  • 228
  • 1
    possible duplicate of [Set padding for UITextField with UITextBorderStyleNone](http://stackoverflow.com/questions/3727068/set-padding-for-uitextfield-with-uitextborderstylenone) – Kirit Modi Jun 27 '15 at 05:07
  • Move your 3rd line code to 2nd line, also add text field delegate – Sujay Jun 27 '15 at 05:32
  • @Sujay this is not a good solution becouse this above code user need to set for each textfield for example if yo have 20 textfiled in register form then you have to code for 20 textfiled like above individual. – Nitin Gohel Jun 27 '15 at 05:41
  • @Nitin, you are absolutely correct. we can prefer above method for few fields. For multiple text field we should use subclassing method, but as deepak is doing for only 1 field so that is easiest and preferred solution – Sujay Jun 27 '15 at 06:28
  • @Sujay as you can see in OP he said `I have tried below solution but that does not work for too many text fields.` so i suggest to use my answer is create a Category that apply for allUITextField is much helpful. – Nitin Gohel Jun 27 '15 at 06:45
  • @nitin, yes then subclassing will be the best option. – Sujay Jun 27 '15 at 06:48
  • you still in confused subclass and category both are different thing too :D – Nitin Gohel Jun 27 '15 at 06:49

2 Answers2

6

You can create a Category of UITextFiled like following code:

.H class

#import <UIKit/UIKit.h>

@interface UITextField (Textpadding)


-(CGRect)textRectForBounds:(CGRect)bounds;
-(CGRect)editingRectForBounds:(CGRect)bounds;
@end

.M class

#import "UITextField+Textpadding.h"

@implementation UITextField (Textpadding)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
-(CGRect)textRectForBounds:(CGRect)bounds {



    return CGRectMake(bounds.origin.x+20 , bounds.origin.y ,
                      bounds.size.width , bounds.size.height-2 );
}
-(CGRect)editingRectForBounds:(CGRect)bounds {
    return [self textRectForBounds:bounds];
}

This category will setting your all text-filed padding by default at run time.

How to create Category

  • Press cmd+n for new class create and select Objective-c class.

enter image description here

  • Then select Category like following.

enter image description here

  • Set Class name.

enter image description here

  • Set file name.

enter image description here

That's it now do code in this class as my answer.

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
1

Another easier and quicker solution can be that you make an imageview and add image for textfield in that. Then add the real text field on top of imageview, and move it as required, for instance as you want left padding, you can move text field a little away from imageview's x-cordinate.

Munahil
  • 2,381
  • 1
  • 14
  • 24