NSAttributedString ignores Autoshrink and numberOfLines for UILabel (iOS 6)

后端 未结 3 1558
臣服心动
臣服心动 2021-01-31 09:01

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 UI

3条回答
  •  佛祖请我去吃肉
    2021-01-31 09:24

    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;
    

提交回复
热议问题