動態計算寬時, 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;
}