Calculate the width and height of a text
/** * 计算一段文字的宽高 * * @param size 这段文字的最大宽高 * @param options NSStringDrawingUsesLineFragmentOrigin * @param attributes 文字的字体属性 * @param context nil * * @return 返回的是 CGRect 类型,frame */- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);
The first method calculates the height of the text
过时的方法
// 首先设置文字的宽和高 CGFloat textW = ?; CGSize textMaxWH = CGSizeMake(textW, ?); //设置文字的行数 设为0->会自动换行 text_label.numberOfLines = 0; CGFloat textH = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:textMaxWH].height;
The second method calculates the height of the text
// 首先设置文字的宽和高 CGFloat textW = ?; CGSize textMaxWH = CGSizeMake(textW, ?); //设置文字的行数 设为0->会自动换行 text_label.numberOfLines = 0; //设置内部文字的字体属性 NSDictionary *attr = @{ NSFontAttributeName : [UIFont systemFontOfSize:14] }; CGFloat textH = [text boundingRectWithSize:textMaxWH options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size.height;
Calculates the width of a sentence of text
//设置内部文字的字体属性 NSDictionary *attr = @{NSFontAttributeName:[UIFont systemFontOfSize:17]}; CGFloat textW = [text sizeWithAttributes:attr].width;
Frame Style Layout Paragraph sub, set width height