我想获得UILabel中一些文本的长度,并动态更改UILabel的width属性。
但是我不知道如何在函数boundingrect中设置参数。
这是Apple Developer的文档。
func boundingRect(with size: CGSize,
options: NSStringDrawingOptions = [],
context: NSStringDrawingContext?) -> CGRect
,我试着这样做
let attr = NSMutableAttributedString(string: "test test test test , this is test text. test test test test , this is test text. test test test test , this is test text. ")
//attr.addattributes
print(attr.boundingrect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: 0), option: .truncatesLastVisibleLine
,context: nil)!)
但最后我在打印中得到了错误的宽度,那么为什么以及如何得到正确的宽度呢。