-1

我需要一种方法来检查TTTAttributedLabelhttps://github.com/TTTAttributedLabel/TTTAttributedLabel)是否被截断并根据它执行自定义逻辑。

我正在寻找一些假设的属性,例如BOOL isTruncated. 我该怎么做?

4

2 回答 2

0

如果你有单行标签,那么你可以做类似的事情,

CGSize size = [yourLabel.text sizeWithAttributes:@{NSFontAttributeName : yourLabel.font}];
if (size.width > yourLabel.bounds.size.width) {

    NSLog(@"your font is truncated!");
}

您可以创建一种返回bool(截断或不截断)并接受标签作为参数的方法!

于 2017-11-22T10:23:02.537 回答
0
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 
        }

`

于 2017-11-22T10:08:45.973 回答