-1
textSearchField.hidden = NO;
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 50, 20)];
textSearchField.leftView = paddingView;
textSearchField.leftViewMode = UITextFieldViewModeAlways;
[search_view addSubview:textSearchField];

I want to add left padding - 10 px for UITextfield using Objective-C. If any other solution for this?

  • https://stackoverflow.com/questions/3727068/set-padding-for-uitextfield-with-uitextborderstylenone – Som Parkash Apr 30 '19 at 05:46
  • I have tried two solutions. 1. category of custom textfield - but it will apply all textfield throughout application . 2. Using lefteview property - the above code is not working for me. there is no change x and y position. – Seethalakshmi_user8943692 Apr 30 '19 at 05:50
  • @BhaveshNayi, all people when getting trouble with code then only will come to stackoverflow. i know this existing questions. i want to know y this code not working for me. – Seethalakshmi_user8943692 Apr 30 '19 at 05:54
  • @Seethalakshmi_user8943692 i think issue is when adding textfield. Check my answer and let me know. – Bhavesh Nayi Apr 30 '19 at 05:57

3 Answers3

2

You can create programmatically by using below code:

CGRect someRect = CGRectMake(0.0, 0.0, 100.0, 30.0);
DKTextField* textField = [[DKTextField alloc] initWithFrame:someRect];
textField.padding = 1.0
[self.view addSubview:textField];

Use Below Classes for it

DKTextField.h

#import <UIKit/UIKit.h>

@interface DKTextField : UITextField

@property (nonatomic) IBInspectable CGFloat padding;

@end

DKTextField.m

#import "DKTextField.h"

IB_DESIGNABLE
@implementation DKTextField

@synthesize padding;

-(CGRect)textRectForBounds:(CGRect)bounds{
    return CGRectInset(bounds, padding, padding);
}

-(CGRect)editingRectForBounds:(CGRect)bounds{
    return [self textRectForBounds:bounds];
}

@end

You just have to give your selected UITextField to DKTextField Class

Whatever the padding you want just add it like below image.

0

//TRY this

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.placeholder = @"enter text";
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:textField];

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 50, 20)];
paddingView.backgroundColor = [UIColor darkGrayColor];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;

Check IMAGE

enter image description here

Bhavesh Nayi
  • 705
  • 4
  • 15
0

textSearchField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, screenWidth - 10 - 40, search_height- 10)];

textSearchField.borderStyle = UITextBorderStyleRoundedRect;
textSearchField.font = [UIFont systemFontOfSize:15];
textSearchField.placeholder = @"Search...";
textSearchField.autocorrectionType = UITextAutocorrectionTypeNo;
textSearchField.keyboardType = UIKeyboardTypeDefault;
textSearchField.returnKeyType = UIReturnKeySearch;
textSearchField.clearButtonMode = UITextFieldViewModeWhileEditing;
textSearchField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textSearchField.delegate = self;

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 50, 20)];
paddingView.backgroundColor = [UIColor darkGrayColor];
textSearchField.leftView = paddingView;
textSearchField.leftViewMode = UITextFieldViewModeAlways;[![enter image description here][1]][1]