swift计算label动态宽度和冲天,uilabel获取文字高度

by admin on 2019年5月2日

  swift计算label动态宽度和可观

 

swift计算label动态宽度和冲天,uilabel获取文字高度。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]swift计算label动态宽度和冲天,uilabel获取文字高度。,
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? [bf88必发唯一官网,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

}

发表评论

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

网站地图xml地图