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:52

Try this

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

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

查看更多
叼着烟拽天下
3楼-- · 2019-01-06 12:53

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.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-06 12:55

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
查看更多
混吃等死
5楼-- · 2019-01-06 12:57

Just try this.

In IOS 8 and Xcode 6.3,

textview.scrollEnabled=YES;
[self.textview setContentInset:UIEdgeInsetsMake(-10.0, 0, -5.0, 0)];
查看更多
beautiful°
6楼-- · 2019-01-06 12:58

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.

查看更多
Animai°情兽
7楼-- · 2019-01-06 13:00

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.

查看更多
登录 后发表回答