ios label尺寸_iOS 根据字符串长度计算label的尺寸

  • Post author:
  • Post category:其他


//计算字符串高度

– (CGSize)labelAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize

{

NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];

paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;

NSDictionary* attributes [email protected]{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};

CGSize labelSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;

labelSize.height=ceil(labelSize.height);

labelSize.width=ceil(labelSize.width);

return labelSize;

}

示例:

[self labelAutoCalculateRectWith:str FontSize:14 MaxSize:CGSizeMake(270, 2000)];

原文:http://www.cnblogs.com/huangzs/p/4541067.html



版权声明:本文为weixin_39926014原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。