我有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中它正在显示,但是当我运行代码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]; } }