iOS6可以使用如下方式来获取字符串的size值
size = [text sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];
但此方法已经在iOS7中被废弃了,注意.
//获取富文本的UILabel高度 iOS7
- (CGSize)getStringRect:(NSString*)aString
{
CGSizesize;
NSAttributedString* atrString = [[NSAttributedStringalloc] initWithString:aString];
NSRangerange =NSMakeRange(0, atrString.length);
NSDictionary* dic = [atrStringattributesAtIndex:0effectiveRange:&range];
size = [aString boundingRectWithSize:CGSizeMake(237, 200)options:NSStringDrawingUsesLineFragmentOriginattributes:diccontext:nil].size;
returnsize;
}
//获取字符串的大小ios6
- (CGSize)getStringRect_:(NSString*)aString
{
CGSizesize;
UIFont*nameFont=[UIFontfontWithName:@"Helvetica"size:13];
size=[aString sizeWithFont:nameFontconstrainedToSize:CGSizeMake(237, 200)lineBreakMode:NSLineBreakByCharWrapping];
returnsize;
}
//宽度固定,获取新的高度,设置了label的attributedText,富文本
+ (CGSize) measureAttributedTextLabelSize:(UILabel*)label{
CGSizeconstraint =CGSizeMake(label.frame.size.width,20000.0f);
CGSizeactualSize = [label.attributedTextboundingRectWithSize:constraintoptions:NSStringDrawingUsesLineFragmentOrigincontext:nil].size;
returnactualSize;
}
//宽度固定,获取新的高度,设置了label的text
+ (CGSize) measureLabelSize:(UILabel*)label{
CGSizeconstraint =CGSizeMake(label.frame.size.width,20000.0f);
CGSizeactualSize = [label.textsizeWithFont:label.fontconstrainedToSize:constraintlineBreakMode:label.lineBreakMode];
returnactualSize;
}