swift Label 高度 宽度自适应
2020-08-04 本文已影响0人
江河_ios
简单粗暴 直接上代码
let lableText = self.nameLable.text
let options : NSStringDrawingOptions =
NSStringDrawingOptions.usesLineFragmentOrigin
let string:NSString = lableText! as NSString
let boundingRect = string.boundingRect(with: CGSize.init(width: screenWidth-40, height: 30), options: options, attributes: [NSAttributedString.Key.font:self.nameLable.font!],context: nil)
self.nameLable.frame=CGRect.init(x: 10, y: 0, width: boundingRect.size.width, height: boundingRect.size.height)
// 设置方法
/*
textStr 要处理的字段
*/
func changeLableSizeString(textStr:String,size:CGSize,font:UIFont) - > CGSize {
let lableText = textStr
let options : NSStringDrawingOptions =
NSStringDrawingOptions.usesLineFragmentOrigin
let string:NSString = lableText as NSString
let boundingRect = string.boundingRect(with: size, options: options, attributes: [NSAttributedString.Key.font:font],context: nil)
return boundingRect.size
}