ensureLayoutForTextContainer

ensureLayoutForTextContainer

本文介绍了根据iOS7中的内容调整UItextview的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据内容和& ;;调整文本视图的大小它也是兄弟容器和父容器。

I'm trying to resize a text view according to content & also it's sibling and parent container.

以下代码在iOS 6中正常工作

Below code is working fine in iOS 6

   if (/* less than ios 7 */) {
        CGRect frame = _textView.frame;
        CGSize conSize = _textView.contentSize;
        CGFloat difference = conSize.height - frame.size.height;

        frame.size.height += difference;
        _textView.frame = frame;

        UIScrollView *parentView = (UIScrollView *)_textView.superview;
        // adjust views residing below this text view.

        // sibling view
        UIView *belowView = // access it somehow
        CGRect frame1 = belowView.frame;

        frame1.origin.y += difference;
        belowView.frame = frame1;
        // adjust parent scroll view, increase height.
        CGSize frame3 = parentView.contentSize;

        frame3.height += difference;
        parentView.contentSize = frame3;
    } else {
       // tried
       [_textView sizeToFit];
       [_textView layoutIfNeeded];
       [parentView sizeToFit];
       [parentView layoutIfNeeded];
    }

尝试遵循iOS 7解决方案:

Tried to follow iOS 7 solution from:How do I size a UITextView to its content on iOS 7?

但不能正常工作。

任何指针?

来自@NSBouzouki的工作代码解决方案

if (/* ios 7 */) {
             [_textView.layoutManager ensureLayoutForTextContainer:_textView.textContainer];
            [_textView layoutIfNeeded];
}
            CGRect frame = _textView.frame;
            CGSize conSize = _textView.contentSize;
            CGFloat difference = conSize.height - frame.size.height;

            frame.size.height += difference;
            _textView.frame = frame;

            UIScrollView *parentView = (UIScrollView *)_textView.superview;
            // adjust views residing below this text view.

            // sibling view
            UIView *belowView = // access it somehow
            CGRect frame1 = belowView.frame;

            frame1.origin.y += difference;
            belowView.frame = frame1;
            // adjust parent scroll view, increase height.
            CGSize frame3 = parentView.contentSize;

            frame3.height += difference;
            parentView.contentSize = frame3;


推荐答案

看来 UITextView的 contentSize 属性未在iOS 7中正确设置,直到 viewDidAppear:。这可能是因为 NSLayoutManager 懒惰地放置文本,并且必须为 contentSize 布置整个文本才是正确的。 ensureLayoutForTextContainer:方法强制提供文本容器的布局,之后 usedRectForTextContainer:可用于获取边界。为了正确获得总宽度和高度,必须考虑 textContainerInset 属性。以下方法适用于我。

It seems UITextView's contentSize property is not correctly set in iOS 7 till viewDidAppear:. This is probably because NSLayoutManager lays out the text lazily and the entire text must be laid out for contentSize to be correct. The ensureLayoutForTextContainer: method forces layout of the provided text container after which usedRectForTextContainer: can be used for getting the bounds. In order to get total width and height correctly, textContainerInset property must be taken into account. The following method worked for me.

 - (CGRect)contentSizeRectForTextView:(UITextView *)textView
    {
        [textView.layoutManager ensureLayoutForTextContainer:textView.textContainer];
        CGRect textBounds = [textView.layoutManager usedRectForTextContainer:textView.textContainer];
        CGFloat width =  (CGFloat)ceil(textBounds.size.width + textView.textContainerInset.left + textView.textContainerInset.right);
        CGFloat height = (CGFloat)ceil(textBounds.size.height + textView.textContainerInset.top + textView.textContainerInset.bottom);
        return CGRectMake(0, 0, width, height);
    }

此外,似乎 UITextView的 setContentSize: layoutSubviews 调用方法。因此,在 textView 上调用 layoutIfNeeded (它本身调用 layoutSubviews layoutManager 上调用 ensureLayoutForTextContainer:后,应该 textView的 contentSize 正确。

Additionally, it seems UITextView's setContentSize: method is called from layoutSubviews. So, calling layoutIfNeeded on a textView (which itself calls layoutSubviews) after calling ensureLayoutForTextContainer: on its layoutManager, should make the textView's contentSize correct.

[someTextView.layoutManager ensureLayoutForTextContainer:someTextView.textContainer];
[someTextView layoutIfNeeded];
// someTextView.contentSize should now have correct value

这篇关于根据iOS7中的内容调整UItextview的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:48