57

I'm having a serious problem that I just can't seem to fix and it's driving me insane for the last two days. I have searched far and wide and I can't find a solution, even though I have tried many.

I have a UITextView inside a UIScrollView. I am able to dynamically resize the UITextView inside the scrollview to display the text. But when the UITextView contains very large text it gets cut off when I scroll almost to the end. However, the UIScrollView's frame is still being sized correctly.

I read these posts: this this and many similar ones.

The UIScrollview and UITextview are both created in the xib using AutoLayout.

Here is my current code and a screenshot as you can see the blank spot in the screenshot should be filled with text. please help.

enter image description here

- (void)viewDidAppear:(BOOL)animated
{
    CGRect frame = self.longDescField.frame;
    frame.size.height = self.longDescField.contentSize.height;
    self.longDescField.frame = frame;

    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width,  self.longDescField.contentSize.height + 200);
    self.scrollView.scrollEnabled = YES;
    [self.scrollView flashScrollIndicators];
}
Community
  • 1
  • 1
sudo
  • 1,648
  • 1
  • 20
  • 22
  • What is this +200 where you set self.scrollView.contentSize ? – LudoZik Sep 09 '13 at 11:13
  • can u change the scroll view height, just try 500 and see if it increases, looks like the content size setting is not working. – DogCoffee Sep 09 '13 at 11:39
  • For some reason the frame is always 200 points short so I add to add 200 to see the last lines of text. – sudo Sep 09 '13 at 11:40
  • The issues is not the the scroll view frame but appears to be an issue with the uitextview's frame. – sudo Sep 09 '13 at 11:48

17 Answers17

139

This issue has existed since iOS 7 and is still present in iOS 12.

However, I wasn't able to keep the normal scrolling behaviour by setting scrollEnabled = NO before the resize, as @igz recommended. Instead I switched scrolling on and off after the resize

// Resize text view here

textView.scrollEnabled = NO;
textView.scrollEnabled = YES;

This forced the cut off text to render correctly.

Mike
  • 1,995
  • 1
  • 14
  • 16
14

Thanks everyone for your help. This is ultimately what ended up working for me in iOS7.

I had to disable auto layout for this particular xib.

Then did the following:

[textView setScrollEnabled:YES];
[textView setText:text];
[textView sizeToFit];
[textView setScrollEnabled:NO];
sudo
  • 1,648
  • 1
  • 20
  • 22
12

For me the solution was to put sizeToFit after customizing the textView

[self.yourTextView sizeToFit];

This should be the last thing you do when manipulating the textview, should not be before you populate the content text.

meda
  • 45,103
  • 14
  • 92
  • 122
  • Works for me. You should call [self.yourTextView sizeToFit]; in your view controller function viewWillLayoutSubviews or viewDidLayoutSubviews. – AleX Feb 09 '17 at 12:12
8

This issue can be fixed by setting the contiguous layout property to false.

textView.layoutManager.allowsNonContiguousLayout = false

Although the documentation says that the default value is false, it is actually set to true for a UITextView.

ArunGJ
  • 2,685
  • 21
  • 27
7

Definitely iOS7. I had this same problem applying to all UITextViews that were resized, both xib and code generated. I found the textContainer.size needed adjusting after UITextView frame was changed.

I created this category code to adjust the textContainer.size but it also seems to need adjusting after setting the text value as well, so I have to call adjustAfterFrameChange after any text changes if they are not followed by setting the frame size. This code makes the assumption that UITextView is not doing anything with setFrame: itself so take out setFrame: and call adjustAfterFrameChange manually if you want to avoid that risk

Edit: changed

self.textContainer.size = self.frame.size; // fix for cut off text

to

self.textContainer.size = self.contentSize; // fix for cut off text

@interface UITextView(Extras)

- (void)adjustAfterFrameChange;

@end



@implementation UITextView(Extras)

- (void)adjustAfterFrameChange {
#if defined(__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
    if ([self respondsToSelector:@selector(textContainer)])
        self.textContainer.size = self.contentSize; // fix for cut off text
#endif
}


- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];

    [self adjustAfterFrameChange];
}

@end
K1w1Geek
  • 605
  • 5
  • 5
  • After I set "myTextView.scrollEnabled = false" the text was cut off at the bottom of the textView. I added "self.textContainer.size = self.contentSize" and the text reappeared correctly drawn. Works iOS 8.1.1 using swift. – MB_iOSDeveloper Mar 18 '15 at 10:21
  • This is the true reason of cutting content in UITextView. – Ryan Nov 14 '17 at 23:04
5

Try this

[self.textView setContentInset:UIEdgeInsetsMake(-8.0, 0, -8.0, 0)];

It work for the cut off text display in the UITextView.

zedzhao
  • 517
  • 1
  • 3
  • 17
5

I had a similar issue, wherein long text was getting cut off after a resize of the text view. Turning off scrollingEnabled before the resize seemed to fix it. Sure seems like an IOS 7 bug.

igz
  • 146
  • 6
2

We had the same problem, except the left half or right half of the UITextView was getting cut off. Happened on both iOS 7 and iOS 6, on a variety of phones. Calling:

myTextView.scrollEnabled = NO;

in viewWillAppear worked around the problem.

jlegakis
  • 31
  • 1
2

Just try this.

In IOS 8 and Xcode 6.3,

textview.scrollEnabled=YES;
[self.textview setContentInset:UIEdgeInsetsMake(-10.0, 0, -5.0, 0)];
Pang
  • 9,564
  • 146
  • 81
  • 122
Rana Raza
  • 21
  • 2
1

We had an issue like this with the rollout of iOS7. When we called setText which added a new line (or lines) to our UITextView, the textview wasn't using the correct new height for its redrawing. The setNeedsDisplay, setNeedsLayout, redrawing layers, redrawing the entire view, etc all didn't work. Finally we forced a loss and gain of focus:

[textView resignFirstResponder];
[textView becomeFirstResponder];

This forced the height recalculation and correct redraw. Thankfully it does not cause the keyboard to pop out and in, but it's worth regression testing that on any iOS versions your app supports.

Kelly
  • 1,096
  • 12
  • 22
  • 1
    With a combination of [myTextView setScrollEnabled:NO]; [myTextView setText:myTextFileContents]; [myTextView setScrollEnabled:YES]; followed by your resign / become first responder I managed to get my text view to work as it should. – Symanski Oct 28 '15 at 21:51
1

This happens all the way, from in Interface Builder too.

When text view selected, in Utilities Inspector uncheck the option Shows Vertical Indicator. The cropped text appears now.

CesareoAguirre
  • 1,557
  • 13
  • 11
1

None of these answers worked for me.

I was fooling with the storyboard and somehow it's working now. It still looks wrong in the storyboard but on the device it's now displaying fine.

I did various things, including toggling many of the options for the textfield.

I think what fixed it for me was making the view larger, building, and making it the right size again.

My apologies for a vague uncertain answer, but maybe it helps. This project was originally written for iOS 5, and the text view may not have been messed with much since then.

csga5000
  • 4,062
  • 4
  • 39
  • 52
1

I have the same Problem for a textview (without a scrollview). Solved this (Xcode 7.3.1, iOS 9.3) just by unchecking "Scrolling Enabled" in the Attributes Inspector.

Francesco
  • 231
  • 3
  • 11
0

I may be wrong but I do not understand your problem thoroughly but what is the use of using a UIScrollView since with the UITextView class implements the behavior for a scrollable, multiline text region ?

You should discard the UIScrollView.

LudoZik
  • 917
  • 1
  • 8
  • 20
  • I am using the scroll view because the the whole view is an article and at the top are uilabels and images that make the textview area small. So it makes it easier for the user to read the article if they can scroll the whole page to see all the text of the article and not just have to scrol through a small uitextview box.. Makes sense? Oh and I failed to mention that I am pulling this from a SQLite db and I don't know the sizes of text of the articles stored in the db. – sudo Sep 09 '13 at 11:44
0

I am facing the same situation. I have to disable the UITextView's scrolling and doing that causes the last line is cliped. Here is my solution:

//In the  UITextView subClass, override "gestureRecognizerShouldBegin" and let the scrolling of UITextView remain on.

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && gestureRecognizer.view == self){
        return NO;
    }
    return [super gestureRecognizerShouldBegin:gestureRecognizer];
}
Jagie
  • 2,190
  • 3
  • 27
  • 25
0

In Swift I fixed this issue by simply setting the textContainerInset of my UITextView:

textView.textContainerInset = UIEdgeInsets(top: 0.0, left: 0.0,
                                           bottom: 50.0, right: 0.0)
rolling_codes
  • 15,174
  • 22
  • 76
  • 112
0

This worked for me:

textView.scrollEnabled = NO;

//resize here

textView.scrollEnabled=YES;