objective-c 将 UITextView 框架设置为内容大小不再适用于 Xcode 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18859637/
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-09-03 20:58:54  来源:igfitidea点击:

Setting UITextView frame to content size no longer works in Xcode 5

objective-cxcode5

提问by Silverstar

The code snippet below worked to resize a UITextView frame to it's content height, before installing Xcode 5 but it doesn't work since the upgrade:

下面的代码片段用于在安装 Xcode 5 之前将 UITextView 框架的大小调整为其内容高度,但自升级以来它不起作用:

CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;

I've searched and haven't found the fix. Any thoughts?

我已经搜索过,但没有找到修复程序。有什么想法吗?

回答by jaredsinclair

There's new stuff for this on iOS 7.

在 iOS 7 上有新的东西。

To get the "fitted" size used by the text view after it's updated its text, call usedRectForTextContainer:on the textView's layoutManagerproperty, passing the textView's textContainerproperty as an argument.

要获得文本视图在更新文本后使用的“适合”大小,请调用usedRectForTextContainer:textView 的layoutManager属性,将 textView 的textContainer属性作为参数传递。

Word of warning about scrolling: Be advised, though, that changing the frame size of a text view afterit has updated it's text can have unexpected visual bugs if scrolling is disabled on your text view. If this happens, set scrolling enabled before editing the text of the text view, then disabling it after it's updated (if you need scrolling to remain disabled).

关于滚动的警告:不过,请注意,如果在文本视图上禁用滚动,则在更新文本视图更改文本视图的框架大小可能会出现意外的视觉错误。如果发生这种情况,请在编辑文本视图的文本之前设置滚动启用,然后在更新后禁用它(如果您需要滚动以保持禁用状态)。

回答by Matheus Abreu

To work in iOS 7 (Xcode 5), just:

要在 iOS 7 (Xcode 5) 中工作,只需:

  1. Give the entire space to receive the text, by setting:

    [myTextView setScrollEnabled:YES];

  2. Pass the real text:

    myTextView.text = theTextVariable;or myTextView.text = @"The text...";

  3. Autoresize textView:

    [myTextView sizeToFit];

  4. Disable scroll:

    [myTextView setScrollEnabled:NO];

  1. 给整个空间来接收文本,通过设置:

    [myTextView setScrollEnabled:YES];

  2. 传递真实文本:

    myTextView.text = theTextVariable;或者 myTextView.text = @"The text...";

  3. 自动调整文本视图:

    [myTextView sizeToFit];

  4. 禁用滚动:

    [myTextView setScrollEnabled:NO];

P.S: myTextView can be use also as self.myTextView or _myTextView

PS:myTextView 也可以用作 self.myTextView 或 _myTextView

And have fun!

玩得开心!

回答by c roald

I believe the correct way to force a textView to update its contentSize is by calling

我相信强制 textView 更新其 contentSize 的正确方法是调用

[textView layoutIfNeeded]

However, in iOS 7.0 and 7.1 this seems still not to work reliably unless you first set

但是,在 iOS 7.0 和 7.1 中,除非您首先设置,否则这似乎仍然无法可靠地工作

textView.layoutManager.allowsNonContiguousLayout = false;

It's not clear to me whether this is a bug or not since I can't really find a good explanation of what "non-contiguous layout" even means.

我不清楚这是否是一个错误,因为我无法真正找到“非连续布局”甚至意味着什么的一个很好的解释。

(My personal use case is updating textView.text = newValueprogrammatically, then trying to resize the textView appropriately.)

(我的个人用例是以textView.text = newValue编程方式更新,然后尝试适当调整 textView 的大小。)

回答by topLayoutGuide

[textView sizeToFit];

Is what you need.

是你需要的。

All you need to do is make sure that:

您需要做的就是确保:

[textView setScrollEnabled:YES];

BEFORE you set the UITextView text content.

在设置 UITextView 文本内容之前。

You can then:

然后你可以:

[textView sizeToFit];
[textView setScrollEnabled:NO];

After you've set the text. Same as writing your own bling function or employing complicated bounding rect methods. Why use something so complicated when the solution is as simple as three lines?

设置好文字后。与编写自己的 bling 函数或使用复杂的边界矩形方法相同。当解决方案像三行一样简单时,为什么要使用如此复杂的东西?

That said, wrap those functions like so:

也就是说,像这样包装这些函数:

- (void) setText:(NSString *)theTextToAdd andResizeTheDamnTextView:(UITextView *)textView {
    [textView setScrollEnabled:YES];
    [textView setText:theTextToAdd];
    [textView sizeToFit];
    [textView setScrollEnabled:NO];
}

And define it in a per-file or global basis to avoid having to manually write or copy/paste the four lines and call it every time. Just call it as:

并在每个文件或全局基础上定义它,以避免必须手动编写或复制/粘贴四行并每次调用它。只需将其称为:

[yourTextViewIvar setText:@"DUMMY STRING" andResizeTheDamnTextView:yourTextViewIvar];

If that doesn't work:

如果这不起作用:

[yourTextViewIvar setText:[self setText:@"DUMMY STRING" andResizeTheDamnTextView:yourTextViewIvar]];

And you'll be golden.

你会是金色的。

I think..

我认为..

That's Pseudocode. Just FYI.

那是伪代码。仅供参考。

回答by Javier Roberto

A easier solution is use that:

一个更简单的解决方案是使用:

[textViewExample sizeToFit];    

This work for me.

这对我有用。