How to add dots to UILabel if text does not fit frame
Asked Answered
F

6

19

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?

Fordone answered 13/10, 2013 at 22:1 Comment(4)
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
L
56

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

Longo answered 13/10, 2013 at 22:9 Comment(5)
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: #20904475Fordone
C
13

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...
Christo answered 28/3, 2018 at 8:40 Comment(1)
Nice shot dude !Chronology
S
5

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
Skylark answered 17/8, 2016 at 4:10 Comment(1)
in Swift 4 just need to give 'b' small. like(yourLabel.adjustsFontSizeToFitWidth = false; yourLabel.lineBreakMode = NSLineBreakMode.byTruncatingTail)Gratifying
B
3

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];
Bercy answered 6/12, 2013 at 17:35 Comment(1)
I know this solution, but at the same time I hope there is more simple way.)Fordone
H
0

For SWIFT 3+

label.adjustsFontSizeToFitWidth = false;
label.lineBreakMode = NSLineBreakMode.byTruncatingTail;
Halothane answered 15/1, 2018 at 22:39 Comment(0)
S
0

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

Sad answered 27/11, 2019 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.