Swift cell的自适应高度

前端之家收集整理的这篇文章主要介绍了Swift cell的自适应高度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


废话不说 直接上代码


  1. func stringHeightWith(fontSize:CGFloat,width:CGFloat)->CGFloat
  2.  
  3. {
  4. let font = UIFont.systemFontOfSize(fontSize)
  5. let size = CGSizeMake(width,CGFloat.max)
  6. let paragraphStyle = NSMutableParagraphStyle()
  7. paragraphStyle.lineBreakMode = .ByWordWrapping;
  8. let attributes = [NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle.copy()]
  9. let text = self as NSString
  10. let rect = text.boundingRectWithSize(size,options:.UsesLineFragmentOrigin,attributes: attributes,context:nil)
  11. return rect.size.height
  12. }

猜你在找的Swift相关文章