我有UITextField类的子类并执行以下代码
- (void)drawPlaceholderInRect:(CGRect)rect
{
[self.placeHolderTextColor setFill];
[self.placeholder drawInRect:rect
withFont:self.placeHolderFont
lineBreakMode:NSLineBreakByTruncatingTail
alignment:NSTextAlignmentLeft];
}
Run Code Online (Sandbox Code Playgroud)
我也写了
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];
}
}
Run Code Online (Sandbox Code Playgroud)
Vad*_*off 16
drawInRect方法似乎在iOS7中表现不同,您可以尝试添加以下行并将其用作直接绘制的矩形.它也向后兼容iOS7之前的版本.
CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6305 次 |
| 最近记录: |