How to Check If Uilabel Is Truncated

How to check if UILabel is truncated?

You can calculate the width of the string and see if the width is greater than label.bounds.size.width

NSString UIKit Additions has several methods for computing the size of the string with a specific font. However, if you have a minimumFontSize for your label that allows the system to shrink the text down to that size. You may want to use sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: in that case.

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

Swift ios how to find out Label text is truncated or not

  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.

Check for Truncation in UILabel - iOS, Swift

You can use the sizeWithAttributes method from NSString to get the number of lines your UILabel has. You will have to cast your label text to NSString first to use this method:

func countLabelLines(label:UILabel)->Int{

if let text = label.text{
// cast text to NSString so we can use sizeWithAttributes
var myText = text as NSString
//A Paragraph that we use to set the lineBreakMode.
var paragraph = NSMutableParagraphStyle()
//Set the lineBreakMode to wordWrapping
paragraph.lineBreakMode = NSLineBreakMode.ByWordWrapping

//Calculate the size of your UILabel by using the systemfont and the paragraph we created before. Edit the font and replace it with yours if you use another
var labelSize = myText.sizeWithAttributes([NSFontAttributeName : UIFont.systemFontOfSize(17), NSParagraphStyleAttributeName : paragraph.copy()])

//Now we return the amount of lines using the ceil method
var lines = ceil(CGFloat(size.height) / label.font.lineHeight)
return Int(lines)
}

return 0

}

Edit

If this method doesn't work for you because your label hasn't a fixed width, you can use boundingRectWithSize to get the size of the label and use that with the ceil method.

func countLabelLines(label:UILabel)->Int{

if let text = label.text{
// cast text to NSString so we can use sizeWithAttributes
var myText = text as NSString

//Set attributes
var attributes = [NSFontAttributeName : UIFont.systemFontOfSize(UIFont.systemFontSize())]

//Calculate the size of your UILabel by using the systemfont and the paragraph we created before. Edit the font and replace it with yours if you use another
var labelSize = myText.boundingRectWithSize(CGSizeMake(label.bounds.width, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes, context: nil)

//Now we return the amount of lines using the ceil method
var lines = ceil(CGFloat(labelSize.height) / label.font.lineHeight)
return Int(lines)
}

return 0

}

How to check UILabel whether is truncated or not in UITableViewCell?

You can't be entirely certain of the width of your cells when you first display your table view because the user might have the iOS device set up for portrait or for landscape (and they can also rotate it at will).

What you should be doing here is add your "Show More" button to all cells and then show or hide it depending on if the text is truncated.

And the right place to show or hide that button is in the UITableViewDelegate willDisplay:forRowAt: function.

Perhaps something like:

func tableView(_ tableView: UITableView, 
willDisplay cell: UITableViewCell,
forRowAt indexPath: IndexPath)
{
// previously set the titleLabel to the text in `cellForRowAtIndexPath`
let labelIsTruncated = cell.titleLabel.isTruncated
// if label is truncated, isHidden should be false.
cell.showMoreButton.isHidden = (labelIsTruncated == false)
}


Related Topics



Leave a reply



Submit