1

I want my UITextView font size to fill the width of the view.

So if I have the text:

"I wish I \n
knew what \n
I was doing"

I would hope it would expand to be a good size for the width of the device.

UILabels have adjustsFontSizeToFitWidth but if I make a separate label for each line then the font sizes will be different because the strings are different lengths.

How do I pick the right size programmatically?

2
  • You'll have to calculate the needed font size so the text meets the desired width. Commented Sep 16, 2015 at 3:49
  • I may be wrong, being only young in The Force; but isn't Attributed Text meant to deal with these very issues? Commented Mar 5, 2016 at 23:57

1 Answer 1

3

Try this code:

extension UITextView {
    func increaseFontSize () {
        self.font =  UIFont(name: self.font.fontName, size: self.frame.size.height / 4)!
    }
}

This extension will set font size of textView according to your textView's height.

And you can use it this way:

textV.increaseFontSize()

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure why this was accepted. Shouldn't the calculation be based on the text in the text view, in addition to the text view's width? I thought the idea was to size the font so the text filled the text view's width.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.