0
我试图根据文本的长度来获取标签的高度。但是,如果这个词不能合并成一行,那么这个词的一部分就会转移到下一行。该方法只返回一行的高度。谁能帮我 ?错误的标签高度
- (CGFloat)heightFromText:(NSString *)text{
if (!text) {
return 0.0;
}
UIFont *font = [UIFont systemFontOfSize:16.0];
UIColor *color = [UIColor colorWithColorCode:@"438fbe"];
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle alloc];
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
NSDictionary *attributes = @{NSForegroundColorAttributeName : color, NSFontAttributeName : font, NSParagraphStyleAttributeName : paragraphStyle};
NSAttributedString *attributed = [[NSAttributedString alloc] initWithString:text attributes:attributes];
CGRect needed = [attributed boundingRectWithSize:CGSizeMake(self.width, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil];
CGFloat textHeight = ceilf(needed.size.height);
return textHeight;
}
因此,当我创建标签指定什么高度? 0.0? – MacUser 2015-01-21 11:23:37
没关系 - 0应该没问题 – dijipiji 2015-01-21 12:16:52