3

I'm trying to load a UITextField with rounded corners into a table view for iOS. For the most part, everything is working fine. However, the left-most character gets partially cut off due to the corner radius property. Is there a way to set a margin on the text that's inputted into a UITextField, so that it displays properly? Here's my code:

        textInput = [[UITextField alloc] init];
        textInput.backgroundColor = [UIColor whiteColor];
        textInput.placeholder = @"example@gmail.com";
        textInput.textColor = [UIColor blackColor];
        textInput.keyboardType = UIKeyboardTypeEmailAddress;
        textInput.returnKeyType = UIReturnKeyDone;

        [[textInput layer] setBorderColor:[[UIColor whiteColor] CGColor]];
        [[textInput layer] setBorderWidth:2.3];
        [[textInput layer] setCornerRadius:15];
        [textInput setClipsToBounds: YES];
        [textInput setDelegate:self];

    [self.contentView addSubview:textInput];
        [textInput release];
Chris
  • 1,180
  • 1
  • 9
  • 17

3 Answers3

1

I figured out a solution. I created a custom textfield, subclassed from UITextField:

#import "CR_customTextField.h"

@implementation CR_customTextField

- (CGRect)textRectForBounds:(CGRect)bounds {
    int margin = 10;
    CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
    return inset;
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    int margin = 10;
    CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
    return inset;
}

@end
sooper
  • 5,991
  • 6
  • 40
  • 65
Chris
  • 1,180
  • 1
  • 9
  • 17
  • I was able to use the leftView of the field like it's used in this answer http://stackoverflow.com/a/11716069/390557 – wreckgar23 Jun 24 '13 at 10:49
0

Use this

[textInput sizeToFit];

it May Helps you

MadhuP
  • 2,019
  • 17
  • 22
  • that shrunk the size of the UITextField considerably, and did nothing to make the inputted text more visible. – Chris Dec 29 '12 at 14:24
0
   textInput.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);

//helps to start writing text from given pixel

Yogesh Lolusare
  • 2,162
  • 1
  • 24
  • 35