0

Im trying to get text to fill the entire bounds of a label no matter how long or short the text string is. I want the largest possible font size without any truncation or clipping.

I set up my label like so:

var messageTitle = TTTAttributedLabel()
messageTitle.setTranslatesAutoresizingMaskIntoConstraints(false)
messageTitle.font = AppTheme.largeMessageFont()
messageTitle.verticalAlignment = .Bottom
messageTitle.numberOfLines = 0
messageTitle.adjustsFontSizeToFitWidth = true
messageTitle.minimumScaleFactor = 0.2

And set the various constraints to set the size of the label to be 250 x 250.

I'm pretty sure this used to work. The label text now gets truncated if it is long when it should be shrinking the size of the text

grabury
  • 4,797
  • 14
  • 67
  • 125
  • an example in objective-c is given here: http://stackoverflow.com/questions/4865458/dynamically-changing-font-size-uilabel and it should be easy to adopt to. – Volker Jan 15 '15 at 08:08
  • That looks like it changes the size of the label's frame? My label must remain the same size – grabury Jan 15 '15 at 08:31
  • you can iterate your font size until it matches the given label size based on this code – Volker Jan 15 '15 at 08:33
  • 1
    It's also worth mentioning that you are using the default initializer `init`, which will not correctly set up many internal properties of `TTTAttributedLabel`. You must use `initWithFrame:` even if you specify `CGRectZero` for the initial frame. – Jonathan Hersh Jan 21 '15 at 02:34

1 Answers1

0

You can set the font into maximum font size then add this attribut

    theLabel.adjustsFontSizeToFitWidth = true
    theLabel.minimumScaleFactor = 0.5 //this is the minimum scale factor, set it as small as you want
F. Suyuti
  • 327
  • 3
  • 18