1
votes

I have a UITextView inside a UIScrollView. if I try to display a long text with a certain font size, the text is not showing. With shorter text or decreasing the font size the text shows up again. What am I doing wrong?

Screenshot of the autolayout here (the item selected is the textview): enter image description here

2
Show me your code and what result you are gettingJogendar Choudhary
you need to increase the height of the textView as when font increases top padding increase and seems if there is no content for that small heightSh_Khan
The height of the TextView should be equal to textView.contentSize.height to show the whole content.. To achieve this, you need to add a height constraint and then through code after assigning the text to TextView set the heightConstraint.constant=textView.contentSize.height; then [self.view layoutIfNeeded];it should work..iphonic
@iphonic, I tried your suggestion but it's still not working.edare
@Giovanni You also need to set scrollview.contentSize = textView.frame.origin.y + textView.frame.size.height; after you set the heightConstraint.iphonic

2 Answers

0
votes

The UITextView is inherited from UIScrollView

open class UITextView : UIScrollView, UITextInput, UIContentSizeCategoryAdjusting

So it is a bad practice to put ScrollView inside scrollView. The reason why you don't see a long text in UITextView is that UITextView became scrollable. Also you didn't specify your scrollView content width so the with is calculated is as one line of your long text. But you can scroll it because you will scroll scrollView. Better way is to set label inside scrollview and set the label numberOfLines = 0. Make the label bottom padding to the bottom of your scrollView and top to some items in top of label and you will get a scrollable text.

I've just understood that you don't need a scrollView to place your long text.

You can use just a UITextView for your needs.

  1. Remove your scrollView.
  2. Add a UITextView.
  3. Set constraints as described and math them with screenshot
    • Top Space to ReadingTimeLabel
    • Align Bottom to Superview / Bottom Layout Guide
    • Leading space to Superview
    • Trailing space to Superview

UITextViewConstraints

And you will get the result like on the screenshots

Scroller top:

enter image description here

Scrolled bottom:

enter image description here

Note: You can disable UITextView editing in attribute inspector.

But if you for some reason need a scroll view: Set constrains as described, also match them as shown on screenshots.

  1. Add scrollView constraints (Superview is the view where the scrollView is placed in): -Top space to ReadingTimeLabel -Align bottom to Superview/Bottom Layout Guide -Align leading to Superview -Align trailing to Superview

  2. Please a UIView inside scrollView. Use it as container.

  3. Add constraints for container. (Superview is scrollView) -Align Leading to Superview -Align Trailing to Superview -Top Space to SuperView -Bottom Space to SuperView -Equal Width to SuperView

  4. Add a UILabel inside container. (Superview is container).

  5. Add constraints for UILabel. -Align Leading to Superview -Align Trailing to Superview -Top Space to SuperView -Bottom Space to SuperView

  6. For the label set Lines = 0 in Attribute inspector or use the code

label.numberOfLines = 0

scrollView constraints

container constraints

label constraints

And you will get the result like on screenshots

Scrolled top

result 1

Scroller bottom

result 2

0
votes

Try the below code:

textView.sizeToFit()