UITextField文本重叠清除按钮

Khu*_*Ali 5 objective-c uitextfield ios

我正在为我的视图添加一个文本字段,如下所示:

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];
Run Code Online (Sandbox Code Playgroud)

当我在此字段中输入长文本时,文本和清除按钮重叠.有谁知道如何解决这一问题?

Aug*_*P A 12

创建一个子类UITextField并覆盖下面给出的方法,

/*< 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;
}
Run Code Online (Sandbox Code Playgroud)

干杯!!!


Khu*_*Ali 3

我想到了问题所在。在另一个文件中,我为 UITextField 定义了一个类别。该类别指定文本区域非常靠近左右边框。这导致了重叠。

经验教训:定义类别时,我们应该使用单独的文件,以便可以轻松检测到修改。