Large Text Being Cut Off in UITextView That is Inside UIScrollView
IphoneIosUiscrollviewUitextviewIphone Problem Overview
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.
- (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];
}
Iphone Solutions
Solution 1 - Iphone
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.
Solution 2 - Iphone
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];
Solution 3 - Iphone
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.
Solution 4 - Iphone
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
Solution 5 - Iphone
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.
Solution 6 - Iphone
Try this
[self.textView setContentInset:UIEdgeInsetsMake(-8.0, 0, -8.0, 0)];
It work for the cut off text display in the UITextView.
Solution 7 - Iphone
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.
Solution 8 - Iphone
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.
Solution 9 - Iphone
Just try this.
In IOS 8 and Xcode 6.3,
textview.scrollEnabled=YES;
[self.textview setContentInset:UIEdgeInsetsMake(-10.0, 0, -5.0, 0)];
Solution 10 - Iphone
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.
Solution 11 - Iphone
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.
Solution 12 - Iphone
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.
Solution 13 - Iphone
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.
Solution 14 - Iphone
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.
Solution 15 - Iphone
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];
}
Solution 16 - Iphone
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)
Solution 17 - Iphone
This worked for me:
textView.scrollEnabled = NO;
//resize here
textView.scrollEnabled=YES;