2010-09-08 45 views
43

我正在以編程方式創建UITextField並將其置於UIView內。字體大小設置爲15.0f(系統字體)。但是出現的佔位符不在文本視圖中居中。任何人都知道如何解決這個問題?以編程方式創建的未佔中的UITextField的佔位符文本

我在模糊文本等方面發現了一些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]; 

感謝您的幫助

注:我的意思是文本不是在文本框(這是接近頂部位)垂直居中。所以設置文本對齊不是解決方案。

添加問題圖片以進行說明 - 如圖所示,佔位符文本更靠近頂部,而不是垂直居中。 ​​

+0

+1謝謝您的問題幫我... – 2013-05-29 05:28:08

回答

56

您需要添加:

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

請記住,這個調整佔位符文本的兩種定向以及文本的用戶將進入

如何中心。垂直

由於原來的問題已更新,要求如何垂直對齊佔位符文本,並將答案埋在評論中,他重新如何做到這一點:

txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
+1

對不起關於那個。我猜這個問題有點誤導。我的意思是說文本不是垂直居中。更新了問題。 – lostInTransit 2010-09-08 08:50:46

+1

然後將我們鏈接到屏幕截圖,以便我們可以確切地看到你在說什麼。我從來沒有見過這個,我已經做了一點點。 – jer 2010-09-08 10:05:35

+1

添加了圖像。請讓我知道這是否有助於指出問題。不知道我會做錯什麼。創建文本字段的方法非常簡單! – lostInTransit 2010-09-08 11:49:51

18

這不符合預期的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及更早版本。

+7

使用lineHeight而不是pointSize在我看來給出了更好的結果。 – 2014-01-16 13:15:33

2

我只使用顏色,但我們也需要設置字體。

這一個爲我工作:

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

這應該是被接受的答案。我不知道爲什麼如果你沒有設置字體屬性,佔位符將會錯位。也許是因爲佔位符字體和文本字體不一樣。 'contentVerticalAlignment'不起作用(至少對我而言)。 – kientux 2016-12-16 11:01:38

+0

如果您使用比佔位符更大的默認字體大小,則這不起作用。 – mattsson 2017-05-10 10:51:29

0

更改使用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 
}]; 
相關問題