Large Text Being Cut Off in UITextView That is Ins

2019-01-06 12:26发布

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];
}

16条回答
冷血范
2楼-- · 2019-01-06 12:40

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.

查看更多
ら.Afraid
3楼-- · 2019-01-06 12:41

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.

查看更多
迷人小祖宗
4楼-- · 2019-01-06 12:42

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.

查看更多
We Are One
5楼-- · 2019-01-06 12:42

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.

查看更多
走好不送
6楼-- · 2019-01-06 12:45

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.

查看更多
看我几分像从前
7楼-- · 2019-01-06 12:49

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];
}
查看更多
登录 后发表回答