16

I am creating an app that contain multiple UITextField. In the textField i have sated border type to none and background image. It display fine, But now my text is started from the lest border which does not look good, like this

enter image description here

How can i add space at the start of the textfield?

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
  • 1
    Your problem seems to be same as this [uitextfield-align-left-margin](http://stackoverflow.com/questions/5674655/uitextfield-align-left-margin) – iCoder Jun 17 '13 at 11:08

8 Answers8

20

Try this

UILabel * leftView = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
leftView.backgroundColor = [UIColor clearColor];

textField.leftView = leftView;

textField.leftViewMode = UITextFieldViewModeAlways;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Kalpesh
  • 5,336
  • 26
  • 41
9

You should subclass UITextField and override drawText function.

check this for help OR You can do following:

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, height_of_textfiled)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Apurv
  • 17,116
  • 8
  • 51
  • 67
  • does this affect my text? means i am sending the data to the web-service so does this code add any malicious data to my actual text? –  Jun 17 '13 at 12:51
  • Now the code will work fine. you forgot to mention `leftViewMode`. – Grey_Code Jun 10 '14 at 12:25
9

Here's @Kalpesh's answer in Swift 3.x:

let leftView = UILabel(frame: CGRectMake(10, 0.0, 7, 26))
leftView.backgroundColor = .clearColor()

customField.leftView = leftView
customField.leftViewMode = .Always
customField.contentVerticalAlignment = .Center

Swift 4.x

let leftView = UILabel(frame: CGRect(x: 10, y: 0, width: 7, height: 26))
leftView.backgroundColor =.clear

customField.leftView = leftView
customField.leftViewMode = .always
customField.contentVerticalAlignment = .center
ZGski
  • 2,398
  • 1
  • 21
  • 34
7

Objective c

For padding only placeholder this code will work

usernameTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"    Your text"];

For padding both placeholder and text of the UITextField this below code will work

usernameTF.layer.sublayerTransform = CATransform3DMakeTranslation(40, 0, 30);

Swift 3.0

For padding only placeholder this code will work

yourTextField.attributedPlaceholder = NSAttributedString(string: "    YourText")

For padding both placeholder and text of the UITextField this below code will work

usernameTF.layer.sublayerTransform = CATransform3DMakeTranslation(40, 0, 30)
Nilesh
  • 533
  • 7
  • 19
5

You can subclass UITextField and override textRectForBounds and editingRectForBounds.

For example,

- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 5, bounds.size.width - 20, bounds.size.height - 10);
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 5, bounds.size.width - 20, bounds.size.height - 10);
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
qiyu
  • 71
  • 6
5

Swift 4

yourTextField.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 10)
oscar castellon
  • 3,048
  • 30
  • 19
1

You can put the textfield background image in an image view,above the imageview put your textfield a space left.

Phantomcho
  • 302
  • 1
  • 6
-1
-(void)textField:(UITextField *) textField didBeginEditing {
    if ([textField.text isEqualToString:@""] || textField.text == NULL) {
       textField.text = @" ";
    }
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59