简体   繁体   English

占位符文本不以编程方式创建的UITextField为中心

[英]Placeholder text not centered for UITextField created programmatically

I am creating a UITextField programmatically and placing it inside a UIView. 我正在以编程方式创建一个UITextField并将其放在UIView中。 The font size is set to 15.0f (system font). 字体大小设置为15.0f(系统字体)。 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. 我在SO上找到了一些关于模糊文本等的参考资料,并尝试使用整数值设置文本框的框架,但它没有什么区别。

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. 添加问题图像以便澄清 - 如图所示,占位符文本更靠近顶部而不是垂直居中。 替代文字

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;

This does not work as expected on iOS7. 这在iOS7上无法正常工作。 On iOS7 you will have to override TextField class and 在iOS7上,您必须覆盖TextField类和

- (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. 适用于iOS7和早期版本。

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

Changing the minimum line height using NSParagraphStyle was the only solution that worked for me: 使用NSParagraphStyle更改最小行高是唯一对我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
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM