How to add dots to UILabel if text does not fit frame
If the linebreakmode doesnt work foryou, another option is to actually calculate the length your string is going to take and if it is going to be longer than the label size, add "..." yourself.
You will have to play with the following code to handle for multi line (but the idea is similar)
For example, Take you label (for example detailLabel)
UILabel* detailLabel = cell.detailTextLabel;
CGSize expectedDetailLabelSize = [detailLabel.text sizeWithFont:detailLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:detailLabel.lineBreakMode];
If your are using AttributedStrings, be aware that you'll have to set the .lineBreakMode
to your NSMutableParagraphStyle
.
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byTruncatingTail
// Add paragraphStyle to attributes, create AttributedString...
For swift 2.0 it will be:
yourLabel.adjustsFontSizeToFitWidth = false;
yourLabel.lineBreakMode = NSLineBreakMode.ByTruncatingTail
For swift 4.0 it will be:
yourLabel.adjustsFontSizeToFitWidth = false;
yourLabel.lineBreakMode = NSLineBreakMode.byTruncatingTail
Have you tried this?
Prior Swift 5:
yourLabel.adjustsFontSizeToFitWidth = NO;
yourLabel.lineBreakMode = NSLineBreakByTruncatingTail;
Swift 5:
yourLabel.adjustsFontSizeToFitWidth = false
yourLabel.lineBreakMode = .byTruncatingTail
Prior iOS6 use UILineBreakModeTailTruncation