func countLabelLines() -> Int {
// Call self.layoutIfNeeded() if your view is uses auto layout
let myText = self.text! as NSString
let attributes = [NSAttributedString.Key.font : self.font]
let labelSize = myText.boundingRect(with: CGSize(width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes as [NSAttributedString.Key : Any], context: nil)
return Int(ceil(CGFloat(labelSize.height) / self.font.lineHeight))
}
func isTruncatedOrNot() -> Bool {
if (self.countLabelLines() > self.numberOfLines) {
return true
}
return false
}
try this, and self.bound.width is your label width so if you added Label in stack view make sure label width or stackview have proper constraints.
In your case its returning true every time because it might have constraints issue.