根据iOS7中的内容调整UItextview的大小

前端之家收集整理的这篇文章主要介绍了根据iOS7中的内容调整UItextview的大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在根据内容&也是兄弟姐妹和父母容器.

以下代码在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解决方案:
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属性.以下方法对我有用.
- (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,width,height);
    }

此外,似乎UITextView的setContentSize:方法是从layoutSubviews调用的.所以,在调用ensureLayoutForTextContainer之后,在一个textView(它本身调用layoutSubviews)上调用layoutIfNeeded:在其layoutManager中,应该使textView的contentSize正确.

[someTextView.layoutManager ensureLayoutForTextContainer:someTextView.textContainer];
[someTextView layoutIfNeeded]; 
// someTextView.contentSize should now have correct value
原文链接:https://www.f2er.com/iOS/330595.html

猜你在找的iOS相关文章