_content = [UILabel new];
_content.text = @"日落时分,沏上一杯山茶,听一曲意境空远的《禅》,心神随此天籁,沉溺于玄妙的幻境里。仿佛我就是那穿梭于葳蕤山林中的一只飞鸟,时而盘旋穿梭,时而引吭高歌;仿佛我就是那潺潺流泻于山涧的一汪清泉,涟漪轻盈,浩淼长流;仿佛我就是那竦峙在天地间的一座山峦,伟岸高耸,从容绵延。我不相信佛,只是喜欢玄冥空灵的梵音经贝,与慈悲淡然的佛境禅心,在清欢中,从容幽静,自在安然。一直向往走进青的山,碧的水,体悟山水的绚丽多姿,领略草木的兴衰荣枯,倾听黄天厚土之声,探寻宇宙自然的妙趣。走进了山水,也就走出了喧嚣,给身心以清凉,给精神以沉淀,给灵魂以升华。";
_content.font = [UIFont systemFontOfSize:14];
_content.numberOfLines = 0;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:_content.font,
NSFontAttributeName,
paragraphStyle,
NSParagraphStyleAttributeName,
nil];
_size = [_content.text boundingRectWithSize:CGSizeMake(kSCREEN_WIDTH-10, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil].size;
_content.frame = CGRectMake(5, 118, kSCREEN_WIDTH-10, _size.height);
//也可使用下面已封装好的方法。
/**
* 根据设置的行间距和字体大小获得 Label 的高度
* @param maxWidth 最大宽度
* @param lineSpacing 行间距
* @param fontSize 字体大小
*/
+ (CGSize)sizeForText:(NSString *)text withMaxWidth:(CGFloat)maxWidth lineSpacing:(CGFloat)lineSpacing fontSize:(CGFloat)fontSize{
NSMutableDictionary * attrDict = [[NSMutableDictionary alloc] init];
NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = lineSpacing;
style.lineBreakMode = NSLineBreakByWordWrapping;
[attrDict setObject:[UIFont systemFontOfSize:fontSize] forKey:NSFontAttributeName];
[attrDict setObject:style forKey:NSParagraphStyleAttributeName];
CGSize strSize = [text boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attrDict
context:nil].size;
return strSize;
}