iPad UITextField - 使用 contentVerticalAlignment = UIControlContentVerticalAlignmentCenter 调整框架大小时光标未居中

发布于 2024-09-28 23:56:14 字数 490 浏览 2 评论 0原文

我正在开发一款通用的 iPhone/iPad 应用程序。我根据用户输入调整一些 UITextField 的大小。我还有一些代码来移动 UITextFields,以便它们的位置在调整大小时有意义。这段代码并不太短,但基本上可以归结为修改 UITextFields 的框架。

我的 UITextFields 的内容都垂直居中

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

通过设置文本中心, 。但是,当我在 iPad 上调整文本字段的大小时,光标有时会移到框的顶部。这对用户来说看起来很奇怪,因为文本位于中心。

这种情况总是发生在 iPad 上,而不会发生在 iPhone 上。更糟糕的是,我无法隔离代码的哪个特定部分导致它发生——除非我停止调整大小,问题就会消失。

还有其他人遇到过这种情况吗?您能够隔离问题的根源吗?

谢谢。

I am working on a universal iPhone/iPad app. I resize some of my UITextFields as the user types. I also have some code to move the UITextFields around so that their locations make sense as they are resized. This code is not too short, but basically it comes down to modifying the frames of the UITextFields.

My UITextFields all have their content vertically centered by setting

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

The text centers just fine. But when I resize the textfield on the iPad, the cursor sometimes goes to the top of the box. This looks strange to the user as the text is in the center.

This always happens on the iPad, never on the iPhone. Making matters worse, I can't isolate which particular part of the code causes it to happen -- except that if I stop resizing things, the problem goes away.

Has anyone else ever encountered this? Were you able to isolate the source of the problem?

Thanks.

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

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

发布评论

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

评论(2

原来分手还会想你 2024-10-05 23:56:14

我弄清楚是什么触发了这个错误。当通过layoutSubviews 和UIControlEventEditingChanged 触发的方法调整textField 的大小时,就会发生这种情况。

I figured out what triggers the bug. It happens when textField is resized both by layoutSubviews and by a method triggered by UIControlEventEditingChanged.

不如归去 2024-10-05 23:56:14

为了更好地控制文本字段文本对齐方式,您可以使用 UITextField 的子类,在其中重写以下方法:

- (CGRect)textRectForBounds:(CGRect)bounds
{
    CGRect rect = [super textRectForBounds:bounds];
    rect.origin.y = (bounds.size.height - rect.size.height) / 2;
    rect = CGRectInset(rect, 3.0f, 0.0f);
    return rect;
}

- (CGRect)editingRectForBounds:(CGRect)bounds
{
    CGRect rect = [super editingRectForBounds:bounds];
    rect.origin.y = (bounds.size.height - rect.size.height) / 2;
    rect = CGRectInset(rect, 3.0f, 0.0f);
    return rect;
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds
{
    CGRect rect = [super placeholderRectForBounds:bounds];
    rect.origin.y = (bounds.size.height - rect.size.height) / 2;
    rect = CGRectInset(rect, 3.0f, 0.0f);
    return rect;
}

For better control of text field text alignment you can use subclass of UITextField where you override following methods:

- (CGRect)textRectForBounds:(CGRect)bounds
{
    CGRect rect = [super textRectForBounds:bounds];
    rect.origin.y = (bounds.size.height - rect.size.height) / 2;
    rect = CGRectInset(rect, 3.0f, 0.0f);
    return rect;
}

- (CGRect)editingRectForBounds:(CGRect)bounds
{
    CGRect rect = [super editingRectForBounds:bounds];
    rect.origin.y = (bounds.size.height - rect.size.height) / 2;
    rect = CGRectInset(rect, 3.0f, 0.0f);
    return rect;
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds
{
    CGRect rect = [super placeholderRectForBounds:bounds];
    rect.origin.y = (bounds.size.height - rect.size.height) / 2;
    rect = CGRectInset(rect, 3.0f, 0.0f);
    return rect;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文