HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了根据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

大佬总结

以上是大佬教程为你收集整理的根据iOS7中的内容调整UItextview的大小全部内容,希望文章能够帮你解决根据iOS7中的内容调整UItextview的大小所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。