动态计算UILabel的可观,swift总括label动态宽度和惊人

  swift总结label动态宽度和可观

 

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

动态计算UILabel的可观,swift总括label动态宽度和惊人。        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

    }

  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

    }

// 获得字符串的冲天

extension UILabel{

func getTextHeight(text :String,width: CGFloat,fontSize :CGFloat = 14)
-> CGFloat {

//依照text的字体大小和控件的增加率以及字体内容动态计算label的可观

let textNormal = text as NSString

func getTheHeightForLabel(font:UIFont,width:CGFloat,text:String)
->UILabel{

let size = CGSizeMake(width, 1000)

let label =UILabel()

let font = UIFont.systemFontOfSize(fontSize)

let textW = width

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

label.text= text

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

label.font= font

return strSize.height

label.numberOfLines=0

}

let textAtt:NSDictionary= [NSFontAttributeName: font]

那样就能够依照后台重回的数额开展差别高度的设置了

let textSize:CGSize=CGSizeMake(textW,CGFloat(MAXFLOAT))

let textH:CGFloat= label.text!.boundingRectWithSize(textSize,options:
.UsesLineFragmentOrigin,attributes: textAttas?
[String:AnyObject],context:nil).size.height

label.frame=CGRectMake(0,0, textW, textH)

return label

}

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website