46

I have UILabel with number of lines = 2 system font size = 15 minimum font size = 8 Line break mode - Truncate tail

When I set long text which have type NSString for UILabel it works fine and shows multiline text (scaled if needed). When I am trying to set text with type NSAttributedString it ignores minimum font size and Autoshrink so I see one line text with maximum font size.

Is it possible to solve this problem

Looks something like this (Label size is const)

-----------------------
| normal NSString  Text|
| very  very  long ... | 
-----------------------

---------------------------
|NSAttributedString tex...|
---------------------------
Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54

3 Answers3

138

I found a way to do this:

label.adjustsFontSizeToFitWidth = true
label.attributedText = attributedString
label.lineBreakMode = .ByTruncatingTail // this did the trick!

It only works if the third line is set after setting the attributed string. It seems like the attributed string overrides line break behavior when set (among other things).

Julian B.
  • 3,605
  • 2
  • 29
  • 38
  • Doesn't work if i append other nsattributed string with alignment right – TomSawyer Apr 13 '16 at 19:40
  • 11
    even if not using adjustsFontSizeToFitWidth, it works only if you set lineBreakMode after having set the attributedText. thanks for sharing! – Fabio Napodano Jul 18 '16 at 15:43
  • 1
    Works with attributes in `NSMutableAttributedString`, too: set up the attributed string first; THEN add the attributes, including the linebreak mode last in the attribute dictionary. – leanne Aug 14 '17 at 21:14
  • @TomSawyer setting .byTruncatingMiddle worked for me, .ByTruncatingTail was still truncating text at the end – Mariam K. Mar 22 '18 at 16:48
  • @Julian B not work for me used [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html] for NsAttributedString – Bhavin Chauhan Jun 13 '18 at 12:17
  • The same issue faced in Xcode 10.1 also, thanks for your help – Faris Muhammed Jul 16 '19 at 13:18
11

minimumFontSize is deprecated as of iOS6. Additionally, adjustsFontSizeToFitWidth only works when numberOfLines is set to 1. UILabel will not resize text across multiple lines because there is ambiguity around handling line breaks while shrinking the font.

Use minimumScaleFactor to set the smallest size that the text should be scaled.

The following code will populate a UILabel with attributed string of font size 20, and scale it down by half to a minimum size of 10.

self.label.lineBreakMode = NSLineBreakByTruncatingTail;
NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan! Presenting the great... Hulk Hogan! Presenting the great... Hulk Hogan! Presenting the great... Hulk Hogan! Presenting the great... Hulk Hogan! Presenting the great... Hulk Hogan! Presenting the great... Hulk Hogan!"];
[hogan addAttribute:NSFontAttributeName
              value:[UIFont systemFontOfSize:20.0]
              range:NSMakeRange(0, [hogan length])];
[self.label setAttributedText:hogan];
self.label.adjustsFontSizeToFitWidth = YES;
self.label.numberOfLines = 1;
self.label.minimumScaleFactor = 0.5;
Adam Kaplan
  • 1,954
  • 17
  • 16
Andy Obusek
  • 12,614
  • 4
  • 41
  • 62
  • 2
    Should numberOfLines = 0 for multiple lines? – Bagusflyer Jul 18 '14 at 06:35
  • 19
    adjustsFontSizeToFitWidth works with multiple lines as well. However, it doesn't work with attributed strings in multiple lines. – Kamran Khan Jun 06 '15 at 16:44
  • @KamranKhan what would one have to do to adjust line spacing for multiple lines as well as minimumScaleFactor ? Any idea? – Mohsin Khubaib Ahmed Nov 07 '19 at 06:34
  • For multiple lines and attibuted text, I had to use the brute force approach: Turn off auto adjust, set the font large (programatically), then size down iteratively until sizeThatFits() says the text fits. – Openningapps Nov 09 '22 at 22:09
0
NSMutableAttributedString *muAtrStr = [[NSMutableAttributedString alloc]initWithString:@"2"];
NSAttributedString *atrStr = [[NSAttributedString alloc]initWithString:@"\ndays" attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:8]}];
[muAtrStr appendAttributedString:atrStr];
self.lbl.numberOfLines = 0;
[self.lbl setAttributedText:muAtrStr];
Swati
  • 1,417
  • 1
  • 15
  • 35