Swift cell的自适应高度

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


废话不说 直接上代码


    func stringHeightWith(fontSize:CGFloat,width:CGFloat)->CGFloat

    {
        let font = UIFont.systemFontOfSize(fontSize)
        let size = CGSizeMake(width,CGFloat.max)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = .ByWordWrapping;
        let  attributes = [NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle.copy()]
        
        let text = self as NSString
        let rect = text.boundingRectWithSize(size,options:.UsesLineFragmentOrigin,attributes: attributes,context:nil)
        return rect.size.height
    }
原文链接:https://www.f2er.com/swift/323371.html

猜你在找的Swift相关文章