/* * * 设置富文本(大小) * * @param neirong 字符串 * @param lineSpace 行间距 * * @return NSMutableAttributedString */ +(NSMutableAttributedString *)attributedString:(NSString *)neirong space:(NSInteger)lineSpace font:(NSInteger)font; /* * * 计算富文本大小 * * @param width 宽度 * @param neirong 字符串 * @param lineSpace 行距 * * @return CGRect */ + (CGRect)boundingRectWithSize:(NSInteger)width attributedString:(NSString *)neirong space:(NSInteger)lineSpace font:(NSInteger)font;
+(NSMutableAttributedString *)attributedString:(NSString *)neirong space:(NSInteger)lineSpace font:(NSInteger)font{ NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:neirong]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:lineSpace]; // 调整行间距 [attributedString setAttributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:[UIFont systemFontOfSize:font],} range:NSMakeRange( 0, [neirong length])]; return attributedString; } +(CGRect)boundingRectWithSize:(NSInteger)width attributedString:(NSString *)neirong space:(NSInteger)lineSpace font:(NSInteger)font{ CGRect rect = [[Common attributedString:neirong space:lineSpace font:font] boundingRectWithSize:CGSizeMake(width, 2000) options:NSStringDrawingUsesLineFragmentOrigin context:nil]; return rect; }
效果图: