动态计算宽时, CGSize size = CGSizeMake(1000000000000.0,height)的高度固定宽写无穷大;同理动态计算高度CGSize size = CGSizeMake(width, 1000000000000.0);宽度固定高度写无穷大。
//动态计算高度
-(CGFloat)heightWithWidth:(CGFloat)width font:(CGFloat)font str:(NSString *)string{
UIFont * fonts = [UIFontsystemFontOfSize:font];
CGSize size =CGSizeMake(width, 1000000000000.0);
NSDictionary * dict = [NSDictionarydictionaryWithObjectsAndKeys:fonts,NSFontAttributeName ,nil];
size = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOriginattributes:dict context:nil].size;
return size.height;
}
//动态计算宽度
-(CGFloat)heightWithWidth:(CGFloat)height font:(CGFloat)font str:(NSString *)string{
UIFont * fonts = [UIFont systemFontOfSize:font];
CGSize size = CGSizeMake(1000000000000.0,height);
NSDictionary * dict = [NSDictionarydictionaryWithObjectsAndKeys:fonts,NSFontAttributeName ,nil];
size = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
return size.width;
}