Large text is cut out in a UITextView, which is inside a UIScrollView

I have a serious problem that I just can’t fix, and it makes me crazy for the last two days. I have searched all over the world and I cannot find a solution, although I have tried a lot.

I have a UITextView inside a UIScrollView . I can dynamically resize the UITextView inside the scroll to display the text. But when the UITextView contains very large text, it is cropped when I scroll to the very end. However, the UIScrollView frame is still properly configured.

I read these posts: this and many others.

UIScrollView and UITextView are created in xib using AutoLayout.

Here is my current code and screenshot, as you can see that the empty space 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]; } 
+50
ios iphone uitextview uiscrollview
Sep 09 '13 at 10:59 on
source share
16 answers

This issue exists with iOS 7 and is still present in iOS 12.

However, I was unable to maintain normal scroll behavior by setting scrollEnabled = NO before resizing as recommended by @igz. Instead, I switched on and off after resizing

 // Resize text view here textView.scrollEnabled = NO; textView.scrollEnabled = YES; 

This caused the cropped text to display correctly.

+128
Nov 05 '13 at 19:59
source share

Thank you all for your help. Ultimately it ended up working in iOS7.

I had to turn off the automatic layout for this particular xib.

Then the following was done:

 [textView setScrollEnabled:YES]; [textView setText:text]; [textView sizeToFit]; [textView setScrollEnabled:NO]; 
+14
Nov 22 '13 at 0:48
source share

For me, the solution was to put sizeToFit after setting the textView

 [self.yourTextView sizeToFit]; 

This should be the last thing you do when manipulating a text view, it should not be before filling in the text of the content.

+10
Aug 07 '15 at 21:36
source share

Definitely iOS7. I had the same issue that applies to all UITextViews that have been modified, with both xib and code. I found that textContainer.size needs to be configured after the UITextView frame has been changed.

I created this category code to edit textContainer.size, but it seems to also need to be adjusted after setting the text value, so I need to call adjustAfterFrameChange after changing any text if they are not followed by setting the frame size. This code makes the assumption that the UITextView does nothing with setFrame: it takes out setFrame itself: and call adjustAfterFrameChange manually if you want to avoid this risk

Edit: Modified

 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 
+7
Nov 18 '13 at 22:47
source share

try it

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

It works to turn off a text display in a UITextView.

+5
Oct 20 '13 at 16:44
source share

I had a similar problem in which long text was cut off after resizing the text view. Disabling scrollingEnabled before resizing seemed to fix it. Of course, it seems like an iOS 7 bug.

+4
Oct 23 '13 at 18:59
source share

You can fix this problem by setting the continuous layout property to false.

 textView.layoutManager.allowsNonContiguousLayout = false 

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

+4
Feb 03 '18 at 21:20
source share

Just try this one.

In iOS 8 and Xcode 6.3,

 textview.scrollEnabled=YES; [self.textview setContentInset:UIEdgeInsetsMake(-10.0, 0, -5.0, 0)]; 
+2
Dec 18 '15 at 7:41
source share

We are having trouble deploying iOS7. When we called setText, which added a new row (or rows) to our UITextView, the text view did not use the correct new height to redraw it. SetNeedsDisplay, setNeedsLayout, redrawing layers, redrawing the whole view, etc., All this did not work. Finally, we forced to lose and gain focus:

 [textView resignFirstResponder]; [textView becomeFirstResponder]; 

This led to a recalculation of the height and the correct repainting. Fortunately, this does not cause the keyboard to pop up and appear, but you should try regression testing on any version of iOS supported by your application.

+1
Nov 11 '13 at 18:44
source share

This comes completely from the Interface Builder interface.

When the text view mode is selected, in the "Utility inspector" uncheck the box "Show vertical indicator". Now the cropped text will appear.

+1
Nov 08 '14 at 17:11
source share

We had the same problem, except that the left half or right half of the UITextView was cropped. It happened both on iOS 7 and iOS 6, on different phones. Vocation:

 myTextView.scrollEnabled = NO; 

in viewWillAppear worked on the issue.

+1
Apr 30 '15 at 22:34
source share

I have the same problem for a text view (without scrolling). Solved this (Xcode 7.3.1, iOS 9.3) by simply unchecking the "Scrolling enabled" checkbox in the Attributes Inspector.

+1
Jun 25 '16 at 13:17
source share

Maybe I'm wrong, but I don’t understand your problem completely, but why use a UIScrollView, since with the UITextView class it implements behavior for a scrollable multi-line text area?

You must cancel the UIScrollView.

0
Sep 09 '13 at 11:17
source share

None of these answers worked for me.

I lied to the storyboards and somehow worked now. It still looks wrong in the storyboard, but on the device that is now displayed normally.

I have done various things , including switching many text field options.

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

My apologies for the vague vague answer, but maybe this helps. This project was originally written for iOS 5, and since then the text presentation may not have been too much.

0
04 Feb '16 at 0:35
source share

I faced the same situation. I need to disable UITextView scrolling, and this causes the last row to be copied. 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]; } 
0
Apr 09 '17 at 5:45 on
source share

In Swift, I fixed this problem by simply setting the textContainerInset my UITextView :

 textView.textContainerInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 50.0, right: 0.0) 
0
May 17 '17 at 19:37
source share



All Articles