為以編程方式創建的UITextField不居中的占位符文本

[英]Placeholder text not centered for UITextField created programmatically


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?

我正在以編程方式創建一個UITextField,並將它放在一個UIView中。字體大小設置為15.0f(系統字體)。但是出現的占位符不在文本視圖中居中。有人知道怎么解決這個問題嗎?

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上找到了一些用於模糊文本等的引用,並嘗試用整數值設置textfield的框架,但這沒有什么區別。

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.

注意:我的意思是,文本並不是垂直地居中在textfield中(它有點朝上)。因此,設置文本對齊不是解決方案。

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

添加問題的圖像以進行澄清——如圖所示,占位符文本更傾向於頂部,而不是垂直於中心。

4 个解决方案

#1


56  

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;

#2


18  

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

這在iOS7上沒有預期的效果。在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和早期版本。

#3


2  

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

#4


0  

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

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/09/08/34918d7aa8da8628629d392dade04c29.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com