以编程方式创建的 UITextField 的占位符文本未居中

发布于 2024-09-18 08:37:44 字数 914 浏览 3 评论 0原文

我正在以编程方式创建 UITextField 并将其放置在 UIView 中。字体大小设置为15.0f(系统字体)。但出现的占位符并未在文本视图中居中。有人知道如何解决这个问题吗?

我找到了一些关于模糊文本等的参考,并尝试使用整数值设置文本字段的框架,但它没有什么区别。

UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(5.0f, 6.0f, 278.0f, 32.0f)];
[txtField setPlaceholder:@"My placeholder"];
[txtField setFont:[UIFont systemFontOfSize:15.0]];
[txtField setBorderStyle:UITextBorderStyleRoundedRect];
[txtField setAutocorrectionType:UITextAutocorrectionTypeNo];
[txtField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[txtField setKeyboardType:UIKeyboardTypeEmailAddress];
[txtField setReturnKeyType:UIReturnKeyDone];
[txtField setClearButtonMode:UITextFieldViewModeWhileEditing];

感谢您的帮助

注意:我的意思是文本在文本字段中没有垂直居中(有点朝顶部)。因此,设置文本对齐方式并不是解决方案。

添加问题图像以进行澄清 - 如图所示,占位符文本更靠近顶部,而不是垂直居中。 替代文本

I am creating a UITextField programmatically and placing it inside a UIView. The font size is set to 15.0f (system font). But the placeholder that appears is not centered in the text view. Any one know how to resolve this?

I found some references on SO for blurred text etc and tried setting the frame of the textfield with integer values, but it doesn't make a difference.

UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(5.0f, 6.0f, 278.0f, 32.0f)];
[txtField setPlaceholder:@"My placeholder"];
[txtField setFont:[UIFont systemFontOfSize:15.0]];
[txtField setBorderStyle:UITextBorderStyleRoundedRect];
[txtField setAutocorrectionType:UITextAutocorrectionTypeNo];
[txtField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[txtField setKeyboardType:UIKeyboardTypeEmailAddress];
[txtField setReturnKeyType:UIReturnKeyDone];
[txtField setClearButtonMode:UITextFieldViewModeWhileEditing];

Thank you for any help

Note: I mean that the text is not centered vertically in the textfield (it is a bit towards the top). So setting the text alignment is not the solution.

Adding an image of the issue for clarification - as seen in the image, the placeholder text is more towards the top and not in the center vertically.
alt text

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

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

发布评论

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

评论(4

笑红尘 2024-09-25 08:37:45

您需要添加:

txtField.textAlignment = NSTextAlignmentCenter; // Pre-iOS6 SDK: UITextAlignmentCenter

请记住,这会调整占位符文本的对齐方式以及用户将输入的文本。

如何垂直居中

由于原始问题已更新为请求如何垂直对齐占位符文本,答案隐藏在注释中,具体操作方法如下:

txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

You need to add:

txtField.textAlignment = NSTextAlignmentCenter; // Pre-iOS6 SDK: UITextAlignmentCenter

Keep in mind, this adjusts both the alignment of the placeholder text as well as the text the user will enter in.

How to center vertically

Since the original question was updated to request how to vertically align the placeholder text and that answer is buried in the comments, here is how to do that:

txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
蓝色星空 2024-09-25 08:37:45

这在 iOS7 上无法按预期工作。在 iOS7 上,您必须重写 TextField 类和

- (void) drawPlaceholderInRect:(CGRect)rect

方法。

像这样:

- (void) drawPlaceholderInRect:(CGRect)rect
{
    [[UIColor blueColor] setFill];
    CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize);
    [[self placeholder] drawInRect:placeholderRect withFont:self.font lineBreakMode:NSLineBreakByWordWrapping alignment:self.textAlignment];
}

适用于 iOS7 及更早版本。

This does not work as expected on iOS7. On iOS7 you will have to override TextField class and

- (void) drawPlaceholderInRect:(CGRect)rect

method.

Like this:

- (void) drawPlaceholderInRect:(CGRect)rect
{
    [[UIColor blueColor] setFill];
    CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize);
    [[self placeholder] drawInRect:placeholderRect withFont:self.font lineBreakMode:NSLineBreakByWordWrapping alignment:self.textAlignment];
}

Works for both iOS7 and earlier versions.

失退 2024-09-25 08:37:45

我只使用了颜色,但我们还需要设置字体。

这对我有用:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:
@{
     NSForegroundColorAttributeName:[UIColor whiteColor], 
     NSFontAttributeName: [UIFont systemFontOfSize:12]
}];

I was using just the color, but we need to set the font as well.

This one worked for me:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:
@{
     NSForegroundColorAttributeName:[UIColor whiteColor], 
     NSFontAttributeName: [UIFont systemFontOfSize:12]
}];
四叶草在未来唯美盛开 2024-09-25 08:37:45

使用 NSParagraphStyle 更改最小行高是唯一对我有用的解决方案:

    NSMutableParagraphStyle *style = [self.addressBar.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy];
style.minimumLineHeight = self.addressBar.font.lineHeight - (self.addressBar.font.lineHeight - placeholderFont.lineHeight) / 2.0;

self.addressBar.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Placeholder text" attributes:@{
     NSForegroundColorAttributeName: [UIColor colorWithRed:79/255.0f green:79/255.0f blue:79/255.0f alpha:0.5f],
     NSFontAttributeName : placeholderFont,
     NSParagraphStyleAttributeName : style
}];

Changing the minimum line height using NSParagraphStyle was the only solution that worked for me:

    NSMutableParagraphStyle *style = [self.addressBar.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy];
style.minimumLineHeight = self.addressBar.font.lineHeight - (self.addressBar.font.lineHeight - placeholderFont.lineHeight) / 2.0;

self.addressBar.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Placeholder text" attributes:@{
     NSForegroundColorAttributeName: [UIColor colorWithRed:79/255.0f green:79/255.0f blue:79/255.0f alpha:0.5f],
     NSFontAttributeName : placeholderFont,
     NSParagraphStyleAttributeName : style
}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文