Swift 3 Solution
I think the best solution is to (1) create a UILabel with the same properties as the label you are checking for truncation, call (2) .sizeToFit() , (3) compare the attributes of the dummy label with your actual label.
For example, if you want to check whether one aligned label with a different width is trimmed or not, you can use this extension:
extension UILabel { func isTruncated() -> Bool { let label = UILabel(frame: CGRect(x: 0, y: 0, width: CGFloat.greatestFiniteMagnitude, height: self.bounds.height)) label.numberOfLines = 1 label.font = self.font label.text = self.text label.sizeToFit() if label.frame.width > self.frame.width { return true } else { return false } } }
... but again, you can easily change the above code to suit your needs. So let your label be multi-line and have different heights. Then the extension will look something like this:
extension UILabel { func isTruncated() -> Bool { let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude)) label.numberOfLines = 0 label.font = self.font label.text = self.text label.sizeToFit() if label.frame.height > self.frame.height { return true } else { return false } } }
Saoud Rizwan Jan 26 '17 at 10:31 on 2017-01-26 10:31
source share