0

I am adding a UITextField programatically into a tableViewCell and currently the cursor starts right up against the left hand side of the textField. The textFields I have added using the xib file always have a gap here.

Does anyone know how to apply this gap manually?

This is my textField:

enter image description here

This is what I want my textField to look like:

enter image description here

Also are there any specifics on the expected dimensions, font, color or a UITextField?

I think the height should be 31 but I'm not sure what other characteristics I should set to make my custom one look like the added xib one.

Thanks for your help :)

EDIT:

Thanks for the help - looking at the answers provided my situation is slightly different as I am using keepLayout rather than setting up a frame with a rectangle. Is there any way to solve this using keepLayout or do I need to use a frame?

sam_smith
  • 6,023
  • 3
  • 43
  • 60

4 Answers4

2

use category for that follow the Bellow steps. You can create category like this:-

enter image description here

enter image description here

#import <UIKit/UIKit.h>

@interface UITextField (mytextFiled)


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

.m file

#import "UITextField+bgImageset.h"

@implementation UITextField (mytextFiled)
#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 + 5,
                      bounds.size.width - 20, bounds.size.height - 10);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [self textRectForBounds:bounds];
}

@end

using above category that my textfiled Look's Like bellow I am setting image for textfiled so that look bit different..

enter image description here

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

The way I achieved this in a recent app is this -

usernameTextField = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 10.0f, 40.0f)]; usernameTextField.leftView.backgroundColor = [UIColor clearColor]; usernameTextField.leftViewMode = UITextFieldViewModeAlways;

It is not a good practice to extend UITextField with a category as discussed here.

Community
  • 1
  • 1
indyfromoz
  • 4,045
  • 26
  • 24
0

This can be done by using a textField.leftView here you add any view i.e. image or empty view just like

    txtField.leftViewMode = UITextFieldViewModeAlways;
    txtField.leftView = imageView;
Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
0

My answer is similar to @Nitin's. But instead of creating a category, I have subclassed UITextField to achieve :

enter image description here

The code is as follows :

.h :

#import <UIKit/UIKit.h>

@interface W3BTextField : UITextField

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end

.m :

#import "W3BTextField.h"

@implementation W3BTextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self) {

        self.edgeInsets = UIEdgeInsetsMake(6.5, 10, 6.5, 5);

    }
   return self;
}

- (CGRect)textRectForBounds:(CGRect)bounds {
    return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [super editingRectForBounds:UIEdgeInsetsInsetRect(bounds, self.edgeInsets)];
}

@end

Now all this is done programmatically, without using XIBs/storyboards.

Used in class like this :

W3BTextField* registrationFullName = [[W3BTextField alloc] initWithFrame:CGRectMake(30, 25, 260, 35)];

registrationFullName.placeholder = @"Full name";

registrationFullName.font = [UIFont fontWithName:@"HelveticaNeue" size:15];

registrationFullName.textColor = UIColorFromRGB(0x333333);

registrationFullName.layer.cornerRadius = 1.0f;

registrationFullName.backgroundColor = [UIColor whiteColor];

registrationFullName.delegate = self;

[registrationFullName addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
n00bProgrammer
  • 4,261
  • 3
  • 32
  • 60