我需要一种方法来检查TTTAttributedLabel
(https://github.com/TTTAttributedLabel/TTTAttributedLabel)是否被截断并根据它执行自定义逻辑。
我正在寻找一些假设的属性,例如BOOL isTruncated
. 我该怎么做?
我需要一种方法来检查TTTAttributedLabel
(https://github.com/TTTAttributedLabel/TTTAttributedLabel)是否被截断并根据它执行自定义逻辑。
我正在寻找一些假设的属性,例如BOOL isTruncated
. 我该怎么做?
如果你有单行标签,那么你可以做类似的事情,
CGSize size = [yourLabel.text sizeWithAttributes:@{NSFontAttributeName : yourLabel.font}];
if (size.width > yourLabel.bounds.size.width) {
NSLog(@"your font is truncated!");
}
您可以创建一种返回bool
(截断或不截断)并接受标签作为参数的方法!
Use the below method to get what you want:
`
func isTruncated(label:UILabel) -> Bool{
let text: String = label.text
if text.isEmpty()
{
return false
}
let boundingBox = text.boundingRect(with: label.frame.size.width, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: label.font!], context: nil)
return label.frame.size.height >= boundingBox.height
}
`