ios 大文本在 UIScrollView 内部的 UITextView 中被截断

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18696706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:02:51  来源:igfitidea点击:

Large Text Being Cut Off in UITextView That is Inside UIScrollView

iphoneiosuiscrollviewuitextview

提问by sudo

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 UITextViewinside a UIScrollView. I am able to dynamically resize the UITextViewinside the scrollview to display the text. But when the UITextViewcontains very large text it gets cut off when I scroll almost to the end. However, the UIScrollView's frame is still being sized correctly.

我有一个UITextView里面的UIScrollView. 我能够动态调整UITextView滚动视图内部的大小以显示文本。但是当UITextView包含非常大的文本时,当我几乎滚动到最后时它会被切断。但是,UIScrollView的框架的大小仍然正确。

I read these posts: thisthisand many similar ones.

我阅读了这些帖子:this this和许多类似的帖子。

The UIScrollviewand UITextvieware both created in the xib using AutoLayout.

UIScrollviewUITextview均在厦门国际银行使用自动版式创建。

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

回答by Mike

This issue has existed since iOS 7 and is still present in iOS 12.

此问题自 iOS 7 以来就存在,并且在 iOS 12 中仍然存在。

However, I wasn't able to keep the normal scrolling behaviour by setting scrollEnabled = NObefore the resize, as @igz recommended. Instead I switched scrolling onand offafter the resize

但是,scrollEnabled = NO正如@igz 建议的那样,我无法通过在调整大小之前进行设置来保持正常的滚动行为。相反,我切换滚动onoff调整大小后

// Resize text view here

textView.scrollEnabled = NO;
textView.scrollEnabled = YES;

This forced the cut off text to render correctly.

这迫使截断的文本正确呈现。

回答by sudo

Thanks everyone for your help. This is ultimately what ended up working for me in iOS7.

感谢大家的帮助。这最终在 iOS7 中为我工作。

I had to disable auto layoutfor this particular xib.

我不得不为这个特定的 xib禁用自动布局

Then did the following:

然后做了以下事情:

[textView setScrollEnabled:YES];
[textView setText:text];
[textView sizeToFit];
[textView setScrollEnabled:NO];

回答by meda

For me the solution was to put sizeToFitafter customizing the textView

对我来说,解决方案是sizeToFit在自定义 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.

这应该是您在操作 textview 时做的最后一件事,而不应该是在填充内容文本之前。

回答by K1w1Geek

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.

肯定是iOS7。我有同样的问题适用于所有调整大小的 UITextViews,包括生成的 xib 和代码。我发现在更改 UITextView 框架后需要调整 textContainer.size 。

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

我创建了这个类别代码来调整 textContainer.size 但它似乎也需要在设置文本值后进行调整,所以我必须在任何文本更改后调用 adjustAfterFrameChange 如果它们之后没有设置框架大小。这段代码假设 UITextView 没有对 setFrame: 本身做任何事情,所以如果你想避免这种风险,请取出 setFrame: 并手动调用 adjustAfterFrameChange

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

回答by ArunGJ

This issue can be fixed by setting the contiguous layout property to false.

这个问题可以通过将 contiguous layout 属性设置为 false 来解决。

textView.layoutManager.allowsNonContiguousLayout = false

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

尽管文档说默认值为 false,但实际上对于 UITextView 设置为 true。

回答by zedzhao

Try this

尝试这个

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

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

它适用于 UITextView 中截断的文本显示。

回答by igz

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.

我有一个类似的问题,在调整文本视图大小后,长文本被截断。在调整大小之前关闭 scrollingEnabled 似乎可以解决它。当然看起来像一个IOS 7错误。

回答by jlegakis

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:

我们遇到了同样的问题,除了 UITextView 的左半部分或右半部分被切断了。发生在 iOS 7 和 iOS 6 上,在各种手机上。调用:

myTextView.scrollEnabled = NO;

in viewWillAppear worked around the problem.

在 viewWillAppear 中解决了这个问题。

回答by Rana Raza

Just try this.

试试这个。

In IOS 8 and Xcode 6.3,

在 IOS 8 和 Xcode 6.3 中,

textview.scrollEnabled=YES;
[self.textview setContentInset:UIEdgeInsetsMake(-10.0, 0, -5.0, 0)];

回答by csga5000

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 thinkwhat 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.

对于一个含糊不清的不确定答案,我深表歉意,但也许会有所帮助。这个项目最初是为 iOS 5 编写的,从那时起文本视图可能就没有被搞得一团糟。