How to add space at start of UITextField

7 Solutions Collect From Internet About “How to add space at start of UITextField”

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;

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;

Here’s @Kalpesh’s answer in Swift:

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

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

You can subclass UITextField and ovverride 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);
}

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)
-(void)textField:(UITextField *) textField didBeginEditing
{
    if ([textField.text isEqualToString:@""] || textField.text == NULL)
    {
       textField.text = @" ";
    }
}

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