【发布时间】:2013-10-06 06:43:08
【问题描述】:
我有 UITextField 类的子类,并做了下面的代码
- (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 中它显示正确,但是当我运行代码时,文本字段占位符顶部对齐。
有什么解决方法吗?
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];
}
}
【问题讨论】:
-
我也有同样的问题,还没有解决办法。
标签: uitextfield ios7