swift计算label动态宽度和高度

 

func getLabHeigh(labelStr:String,font:UIFont,width:CGFloat) -> CGFloat {

        let statusLabelText: NSString = labelStr

        let size = CGSizeMake(width, 900)

        let dic = NSDictionary(object: font, forKey: NSFontAttributeName)

        let strSize = statusLabelText.boundingRectWithSize(size, options: .UsesLineFragmentOrigin, attributes: dic as? [String : AnyObject], context: nil).size

        return strSize.height

    }

    

    func getLabWidth(labelStr:String,font:UIFont,height:CGFloat) -> CGFloat {

        let statusLabelText: NSString = labelStr

        let size = CGSizeMake(900, height)

        let dic = NSDictionary(object: font, forKey: NSFontAttributeName)

        let strSize = statusLabelText.boundingRectWithSize(size, options: .UsesLineFragmentOrigin, attributes: dic as? [String : AnyObject], context: nil).size

        return strSize.width

    }

posted on 2016-08-24 11:04  纯情的小公鸡  阅读(5156)  评论(0编辑  收藏  举报