/*根据传过来的文字内容、字体大小、宽度和最大尺寸动态计算文字所占用的size * text 文本内容 * fontSize 字体大小 * maxSize size(宽度,1000) * return size (计算的size) */ - (CGSize)labelAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize { NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping; NSDictionary* attributes =@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy}; CGSize labelSize; //如果是IOS6.0 if (![text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]){ labelSize = [text sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping]; } //如果系统为iOS7.0 else { // iOS7中用以下方法替代过时的iOS6中的sizeWithFont:constrainedToSize:lineBreakMode:方法 labelSize = [text boundingRectWithSize: maxSize options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size; } labelSize.height=ceil(labelSize.height); labelSize.width=ceil(labelSize.width); return labelSize; }