2
UILabel *label
label.numberOfLines = 1;
label.text = @"abc\nabc";
label.lineBreakMode = NSLineBreakByTruncatingTail;

display as: abc expected: abc... I've tried to set attributed text and set line break mode in paragraph attribute, did not work either -_-//

Add: if replace '\n' character with text over one line, then it displays as well. Such as: set label width 100pt, set text 'abcabcabcabcabcbacb' and blabla more than one line, display as 'abcabcabcabca...'.

So if the text able to be truncated is related to the key char '\n'.

IsEE
  • 45
  • 6

3 Answers3

0
    let label = UILabel()
    label.frame = CGRect(x: 100, y: 200, width: 50, height: 25)
    label.backgroundColor = UIColor.red
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakMode.byTruncatingTail
    label.text = "abc\nabc"

Give number of lines to 0 and height in 1 line range(say 21),then you will get expected answer. if you give number of lines is 1,There is nothing to truncate in the first line.it just ignore the second line text.

Angel
  • 112
  • 15
  • ops! I find if I remove '\n' and append two lines words, then it'll be truncated by '...'. So it's related to '\n' character. Not solved now -_-///. – IsEE Nov 16 '17 at 02:13
  • As we set the height to 25. and width to 50 ,and not resizing the frame of label depending on the text size. It will truncate the tail. what is your actual requirement? – Angel Nov 16 '17 at 05:34
  • The requirement is to truncate text by set numberOfLines instead of visible text height. It works well if not end the last visible line with '\n'. – IsEE Nov 16 '17 at 08:07
  • Please check the Updated Answer.Hope it will help – Angel Nov 22 '17 at 09:24
0

Create a label with height 21(or as per your wish).And set the leading ,trailing, top and height constraints.Make height constraints >= to height. Connect the height constraints to class.

 label.numberOfLines = 5;    //set number of lines as you need.
 label.lineBreakMode = NSLineBreakMode.byTruncatingTail  // set as truncate tail to get …
 let strings = "iOS is a mobile operating system created and developed by Apple Inc. exclusively for its hardware. It is the operating system that presently powers many of the company's mobile devices, including the iPhone, iPad, and iPod Touch"
 label.text = strings
 let size: CGSize = label.text!.size(withAttributes [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14.0)])
 labelHeightConstraint.constant = size.height
Angel
  • 112
  • 15
  • Appreciate your answer, but I'm not able to use AutoLayout, using another layout engine in the app and coding by JavaScript. Now I treat the question as a bug in iOS, is there a way to cross it? – IsEE Dec 12 '17 at 07:21
0

Try this.

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 200, 25)];
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0; //  use 0 for unlimited.
label.preferredMaxLayoutWidth = self.view.frame.size.width;
label.text = @" abc \n abc \n abc \n abc \n abc \n abc \n abc \n abc \n abc \n";
[label sizeToFit];
[label setNeedsDisplay];
[self.view addSubview:label];

Please check the discussion too. Adjust UILabel height depending on the text

Angel
  • 112
  • 15