對準我有子類UITextField類,做了下面的代碼的UITextField佔位符串總是頂在ios7
- (void)drawPlaceholderInRect:(CGRect)rect
{
[self.placeHolderTextColor setFill];
[self.placeholder drawInRect:rect
withFont:self.placeHolderFont
lineBreakMode:NSLineBreakByTruncatingTail
alignment:NSTextAlignmentLeft];
}
我也寫 self.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
行代碼
此佔位符文本正確中心對齊在ios6但不是在ios7它顯示頂部對齊。
雖然我輸入的文字出現居中,但只有佔位符字符串的問題。
我試着用xib設置佔位符字符串。在XIB中顯示正常,但是當我運行代碼時,textfield佔位符是頂部對齊的。
針對此問題的任何解決方法?
Vadoff的答案爲我工作。這仍然是完全實現,可以幫助任何人有同樣的問題。
drawInRect
方法ios7被棄用,drawInRectWithAttributes
工作
- (void)drawPlaceholderInRect:(CGRect)rect
{
[self.placeHolderTextColor setFill];
CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.placeHolderFont.pointSize)/2 - 2, rect.size.width, self.placeHolderFont.pointSize);
rect = placeholderRect;
if(iOS7) {
NSMutableParagraphStyle* style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByTruncatingTail;
style.alignment = self.placeHolderTextAlignment;
NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:style,NSParagraphStyleAttributeName, self.placeHolderFont, NSFontAttributeName, self.placeHolderTextColor, NSForegroundColorAttributeName, nil];
[self.placeholder drawInRect:rect withAttributes:attr];
}
else {
[self.placeholder drawInRect:rect
withFont:self.placeHolderFont
lineBreakMode:NSLineBreakByTruncatingTail
alignment:self.placeHolderTextAlignment];
}
}
我有同樣的問題,沒有解決呢。 – Vadoff