Chris
Chris

Reputation: 1220

iOS: Why does UITextField with setCornerRadius cut off text

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 = @"[email protected]";
        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];

Upvotes: 3

Views: 1049

Answers (3)

Yogesh Lolusare
Yogesh Lolusare

Reputation: 2212

   textInput.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);

//helps to start writing text from given pixel

Upvotes: 0

Chris
Chris

Reputation: 1220

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

Upvotes: 1

MadhuP
MadhuP

Reputation: 2019

Use this

[textInput sizeToFit];

it May Helps you

Upvotes: 0

Related Questions