[关闭]
@buoge 2017-06-28T10:55:53.000000Z 字数 636 阅读 840

iOS UILable 计算文字高度

iOS


uilable 一定要是 .byWordWrapping 计算才正确

  1. public static func stringSize(calculateString: String, fontSize:CGFloat,width:CGFloat,lineSpacing:CGFloat=0)->CGSize {
  2. let font = FontUtils.getThinFont(fontSize)
  3. let size = CGSize(width: width,height: CGFloat.greatestFiniteMagnitude)
  4. let paragraphStyle = NSMutableParagraphStyle()
  5. paragraphStyle.lineSpacing = lineSpacing
  6. paragraphStyle.lineBreakMode = .byWordWrapping;
  7. let attributes = [NSFontAttributeName:font,
  8. NSParagraphStyleAttributeName:paragraphStyle.copy()]
  9. let text = calculateString as NSString
  10. let rect = text.boundingRect(with: size, options:.usesLineFragmentOrigin, attributes: attributes, context:nil)
  11. return rect.size
  12. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注