UITextField 文本与清除按钮重叠

发布于 2024-12-19 06:57:44 字数 1057 浏览 0 评论 0原文

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

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];

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半城柳色半声笛 2024-12-26 06:57:44

创建 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;
}

干杯!

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 !!!

辞慾 2024-12-26 06:57:44

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

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

I figured what the issue was. In another file, I had a category defined for UITextField. This category, specified the text area to be very close to the left and right border. That was causing the overlap.

Lesson learnt: When defining categories, we should use separate files so that the modifications are easily detectable.

海之角 2024-12-26 06:57:44

@Augustine PA,我认为更好的方法是调用 super 并修改结果,如下所示:

-(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;
}

@Augustine P A, I think a better approach is to call the super and modify the result like this:

-(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;
}
定格我的天空 2024-12-26 06:57:44

添加文本字段后,调用

[button BringSubviewToFront:self.view];

After adding the text field, call

[button bringSubviewToFront:self.view];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文