1

Basically I want this How do I size a UITextView to its content? but backwards. That is, making the text inside the UITextView adjust to its frame, that is, making the font size big enough to fill its bounding box.

It shouldn't be that hard! UILabel has the method "adjustsFontSizeToFitWidth", I'm looking for something similar for the UITextView.

In a bigger picture, what I'm trying to do is:

Draw one glyph of a custom font I've created (not letters but other type of symbols: svg drawings) and being able to add to it a Gesture Recognizer (tapping or dragging it) and also being able to resize it. I want to start at a certain font size, then sizeToFit, and then being able to resize that frame and having the font automatically decide a font Size to fit the frame.

I tried using UILabels but they chop some of my custom glyphs (vertically) when I sizeToFit, whereas the UITextView doesn't.

Thanks for your help! :)

Community
  • 1
  • 1
pcm
  • 11
  • 2

1 Answers1

0

I used aksh1t solution #2 from StackOverflow 23621536

In my usage, I set the UITextViews from an API call returning a NSDictionary, then resized the font to fit in the UITextView:

        [_alertLine1TextView setText:[_alertResultsDictionary objectForKey:@"line1"]];

        CGFloat fontSize = 25.0;
        CGFloat minSize = 9.0;
        while (fontSize > minSize &&  [_alertLine1TextView sizeThatFits:(CGSizeMake(_alertLine1TextView.frame.size.width, FLT_MAX))].height >= _alertLine1TextView.frame.size.height ) {
            fontSize -= 1.0;
            _alertLine1TextView.font = [_alertLine1TextView.font fontWithSize:fontSize];
        }

Tested under iOS 7.1.2 and iOS 8.0 beta5

Community
  • 1
  • 1
Kent
  • 1,374
  • 1
  • 15
  • 29