How to check if TTTAttributedLabel is currently truncated

329 Views Asked by At

I need a way to check whether a TTTAttributedLabel (https://github.com/TTTAttributedLabel/TTTAttributedLabel) is truncated or not and perform custom logic depending on that.

I am looking for some hypothetical property like BOOL isTruncated. How do I do it?

2

There are 2 best solutions below

0
Y_Y On
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 
        }

`

1
Ketan Parmar On

If you have single line label then you can do something like,

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

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

you can create one method which return bool(truncated or not) and accepts label as a parameter!