IOS UILabel 长字符串处理

label.lineBreakMode =NSLineBreakMode.byTruncatingMiddle
label.numberOfLines = 0 //默认是1,0表示不限制
label.adjustsFontSizeToFitWidth = true //字体大小自适应
label.allowsDefaultTighteningForTruncation = true //缩小字间距

//根据传入的字符串的字体、最大的宽和高,返回字符串实际占用的宽和高
class func getTextRectSize(text:NSString,font:UIFont,size:CGSize) -> CGRect {
let attributes = [NSFontAttributeName:font]
let option =NSStringDrawingOptions.usesLineFragmentOrigin
let rect:CGRect = text.boundingRect(with:size,options:option, attributes:attributes, context:nil)
print(“rect:(rect)”);
return rect;
}

NSLineBreakMode的枚举:


9967595-788ffefd7fa5c128.png
image.png

猜你喜欢

转载自blog.csdn.net/weixin_34240657/article/details/90866438