UITextField text overrides the clear button

I add a text box to my view as follows:

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 = @"your@email.com";
    [self.view addSubview:tf_email];

When entering long text in this field, the text and the clear button overlap. Does anyone know how to fix this?

+5
source share
4 answers

I realized what the problem is. In another file, I had a category defined for UITextField. In this category, the text area was very close to the left and right border. This caused an overlap.

Lesson learned: when defining categories, we must use separate files so that changes are easily detected.

+3
source

Subclass UITextFieldand override the methods 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;
}

!!!

+12

@Augustine P A, , - :

-(CGRect) textRectForBounds:(CGRect)bounds
{
    CGRect rect = [super textRectForBounds:bounds];
    rect.size.width = rect.size.width - 20.0f;
    return rect;
}

-(CGRect) editingRectForBounds:(CGRect)bounds
{
    CGRect rect = [super editingRectForBounds:bounds];
    rect.size.width = rect.size.width - 20.0f;
    return rect;
}
+3

[button bringSubviewToFront:self.view];

afert adding a text field.

+1
source

All Articles