我有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]; } }
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。