UILabel 设置行间距 && 自动计算text 的frame

  1. NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:testString];   
  2. NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];   
  3. [paragraphStyle1 setLineSpacing:15];   
  4. [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [testString length])];   
  5. [cLabel setAttributedText:attributedString1];   

  1. [cLabel sizeToFit];  

转自:http://blog.csdn.net/daiyelang/article/details/45840591

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context 

不能返回正确的size


红色的使用

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode

能返回正确size



- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes
是新的函数,你需要设置attributes相关属性.例如:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributes = @{NSFontAttributeName:label.font, NSParagraphStyleAttributeName:paragraphStyle.copy};
        
labelSize = [someText boundingRectWithSize:CGSizeMake(207, 999) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;

你可能感兴趣的:(UILabel 设置行间距 && 自动计算text 的frame)