2

I'm building an app and it seems like a simple issue but I can't for the life of me fix it.

On a screen, I have a large amount of text, provided through a UITextView. I would like it to essentially be static, without the scroll bar.

If I resize it manually in design view, and run it on an iPhone 7, the text fits nicely. However, if I run it on an iPhone 5c, it is too small due to the screen dimensions and then naturally reduces it and adds a scroll.

Is there a way I can tell the text to display it all and resize it accordingly, similar to the SizeToFit() command?

halfer
  • 19,824
  • 17
  • 99
  • 186
Super_Simon
  • 1,250
  • 8
  • 25

3 Answers3

1

Just use the label if you don't want the scroll behavior.

label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 0

This will fix the font size if label is not big enough.

HMHero
  • 2,333
  • 19
  • 11
0

Assign constraints (tiny red lines in screenshot) to your UITextView then it will fit on all sizes correctly enter image description here

And like @Orkhan Alikhanov said, use UITextView.scrollEnabled = false – to disable scroll inside the textview

swift2geek
  • 1,697
  • 1
  • 20
  • 27
0

If you do not want to scroll why are you using a text field instead of a label? Either way, you can use the answer here UIDevice currentDevice model possible values to determine what device you are using and then you can set the font size based on that information.

I suggest creating a function that does this calculation and then returns the font size so you can just call that whenever you are setting font.

CSjunkie
  • 535
  • 2
  • 8
  • 28
  • If I use a label, it only stays on one line. I didn't realise you could have paragraphs on labels? – Super_Simon Aug 24 '17 at 15:46
  • If you set the label lines = 0 it will adjust height to display all the content. You also need to make sure that your constraints allow it to grow and that it does not have exact height constraints. – CSjunkie Aug 24 '17 at 15:48