Large Text Being Cut Off in UITextView That is Inside UIScrollView
Asked Answered
L

17

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];
}
Leeward answered 9/9, 2013 at 10:59 Comment(4)
What is this +200 where you set self.scrollView.contentSize ?Opus
can u change the scroll view height, just try 500 and see if it increases, looks like the content size setting is not working.Linkwork
For some reason the frame is always 200 points short so I add to add 200 to see the last lines of text.Leeward
The issues is not the the scroll view frame but appears to be an issue with the uitextview's frame.Leeward
K
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.

Kriemhild answered 5/11, 2013 at 19:59 Comment(12)
This helped me with my solution. Thx.Leeward
So good that work. All I found before wan't working on iOS 7.1, but this is the good answer for me.Lindyline
Doesn't help me. But the following helps: #22316255Tsarevna
fixes for ios 8.1 too a bug i had that didnt allow me to scroll all the way up in a very large textWedding
Works also for iOS9.1. Can't believe that Apple does not fix this issue. It seems to be quite old.Kall
This is fix the same thing for me. The funny thing is I've never seen it before and worked with scrolling text views for a while. All of a sudden it started happening and I couldn't figure what I had changed, the code looked identical. Go figure.Urson
Absolutely the solution. I filed it: openradar.me/24435091 — and so should you!Buddy
Can't believe this bug still exists on iOS 9.2.1 (app was build with Xcode 7.2.1)Kingsly
Perfect Solution...Worked for me thanks. I have spent lots of time on this.Woald
Why this answer has not 99999 upvotes? I spent 3 hours to find this answer, thanks a lotHorlacher
You are a geniusRugose
Still have this bug on iOS12. Apple engineers related to this part should be fired.Bellinger
L
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];
Leeward answered 22/11, 2013 at 0:48 Comment(1)
Just setting scrollEnabled:NO did it for me.Patency
D
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.

Diatomaceous answered 7/8, 2015 at 21:36 Comment(1)
Works for me. You should call [self.yourTextView sizeToFit]; in your view controller function viewWillLayoutSubviews or viewDidLayoutSubviews.Chuvash
H
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.

Hayott answered 3/2, 2018 at 21:20 Comment(0)
T
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
Tawnatawney answered 18/11, 2013 at 22:47 Comment(2)
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.Coth
This is the true reason of cutting content in UITextView.Hett
A
5

Try this

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

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

Adust answered 20/10, 2013 at 16:44 Comment(0)
T
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.

Then answered 23/10, 2013 at 18:59 Comment(0)
T
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.

Treble answered 30/4, 2015 at 22:34 Comment(1)
This doesn't contribute anything that hasn't been discussed/answered before.Leeward
J
2

Just try this.

In IOS 8 and Xcode 6.3,

textview.scrollEnabled=YES;
[self.textview setContentInset:UIEdgeInsetsMake(-10.0, 0, -5.0, 0)];
Joyajoyan answered 18/12, 2015 at 7:41 Comment(0)
D
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.

Disloyal answered 9/9, 2013 at 10:59 Comment(0)
H
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.

Heall answered 11/11, 2013 at 18:44 Comment(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.Cystoscope
R
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.

Retiform answered 4/2, 2016 at 0:35 Comment(0)
L
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.

Lallygag answered 25/6, 2016 at 13:17 Comment(0)
F
1

This worked for me:

textView.scrollEnabled = NO;

//resize here

textView.scrollEnabled=YES;
Flavone answered 15/6, 2020 at 10:17 Comment(0)
O
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.

Opus answered 9/9, 2013 at 11:17 Comment(1)
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.Leeward
E
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];
}
Ellon answered 9/4, 2017 at 5:45 Comment(0)
O
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)
Obsidian answered 17/5, 2017 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.