I have a cell with multiline UILabels, but when the text's label does not fit the frame, no dots are shown. How can I fix this?
How to add dots to UILabel if text does not fit frame
Asked Answered
Did you really need to add a bounty to this. There are hundreds of answers to this sort of question. #11302665 and google.co.uk/… simple google search. –
Perjury
@Perjury i tried to fix this problem according to previous answers, but it doesn't help me. PS: My label's property "numberOfLines" set to "2", if it's equal to "1", it's works. –
Fordone
numberOfLines is 1 by default. Try to set it 0 if you dont know the exact numberOfLines and use sizeToFit method of UILable. –
Frizzly
@iProgrammer I have fixed maximum number of lines - 2. And sizeToFit works awful all the time. –
Fordone
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
I think the correct option is
NSLineBreakByTruncatingTail
. The one you mention in your answer is deprecated. –
Chindwin I tried to use NSLineBreakByTruncatingTail, not UILineBreakModeTailTruncation. –
Fordone
That won't work for multiline labels. The
lineBreakMode
should be NSLineBreakByWordWrapping
or NSLineBreakByCharWrapping
. –
Dishpan @brunobasas Thanks for editing. Anyway, some readers may find useful the original answer as it may depend on ios version. It'd be nice to keep both alternatives in the answer :) –
Longo
I have another similar question with my explanation: #20904475 –
Fordone
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...
Nice shot dude ! –
Chronology
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
in Swift 4 just need to give 'b' small. like(yourLabel.adjustsFontSizeToFitWidth = false; yourLabel.lineBreakMode = NSLineBreakMode.byTruncatingTail) –
Gratifying
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];
I know this solution, but at the same time I hope there is more simple way.) –
Fordone
For SWIFT 3+
label.adjustsFontSizeToFitWidth = false;
label.lineBreakMode = NSLineBreakMode.byTruncatingTail;
Swift 4.0 Example:
extension String {
/*
Truncates the string to the specified length number of characters and appends an optional trailing string if longer.
- Parameter length: Desired maximum lengths of a string
- Parameter trailing: A 'String' that will be appended after the truncation.
- Returns: 'String' object.
*/
func trunc(length: Int, trailing: String = "…") -> String {
return (self.count > length) ? self.prefix(length) + trailing : self
}
}
let str = "I might be just a little bit too long".truncate(10)
ref. link String Truncate
© 2022 - 2024 — McMap. All rights reserved.