Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField text overlaps clear button

I am adding a textfield to my view like this:

UITextField* tf_email = [[UITextField alloc] initWithFrame:CGRectMake((320-btnImage1.size.width)/2, 170, 175, 35)];
    [tf_email setBackgroundColor:[UIColor clearColor]];
    [tf_email setBorderStyle:UITextBorderStyleRoundedRect];
    [tf_email setClearButtonMode:UITextFieldViewModeWhileEditing];
    [tf_email setReturnKeyType:UIReturnKeyDone];
    [tf_email setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [tf_email setEnablesReturnKeyAutomatically:NO];
    [tf_email setDelegate:self];    
    [tf_email setOpaque:YES];
    tf_email.tag=1;
    tf_email.font = TTSTYLEVAR(font);
    tf_email.layer.cornerRadius = 10;
    tf_email.keyboardType = UIKeyboardTypeEmailAddress;
    [tf_email setAutocorrectionType:UITextAutocorrectionTypeNo];
    tf_email.placeholder = @"[email protected]";
    [self.view addSubview:tf_email];

When I enter long text in to this field, the text and the clear button overlaps. Does any one know how to fix this?

like image 437
Khurram Ali Avatar asked Dec 02 '11 05:12

Khurram Ali


1 Answers

Create a subclass of UITextField and override the methods given below,

/*< Place holder position >*/
- (CGRect)textRectForBounds:(CGRect)bounds {

    bounds.size.width = bounds.size.width - 20.0f;
    return bounds;
}

/*< Text Posiotion >*/
- (CGRect)editingRectForBounds:(CGRect)bounds {

    bounds.size.width = bounds.size.width - 20.0f;
    return bounds;
}

Cheers !!!

like image 52
Augustine P A Avatar answered Nov 15 '22 05:11

Augustine P A