9

我正在尝试根据内容调整文本视图的大小,它也是同级容器和父容器。

下面的代码在 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 解决方案: 如何在 iOS 7 上调整 UITextView 的大小以适应其内容?

但不工作。

任何指针?

@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;
4

2 回答 2

26

似乎UITextView's 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, 0, width, height);
    }

此外,似乎UITextView's setContentSize:方法是从layoutSubviews. 因此,layoutIfNeeded在调用 a之后调用它textView(它本身调用layoutSubviews)应该是正确的。ensureLayoutForTextContainer:layoutManagertextView's contentSize

[someTextView.layoutManager ensureLayoutForTextContainer:someTextView.textContainer];
[someTextView layoutIfNeeded]; 
// someTextView.contentSize should now have correct value
于 2013-11-07T20:57:32.957 回答
1

GrowingTextViewHandler是一个 NSObject 子类,它在用户键入文本时调整文本视图的大小。这是您可以使用它的方法。

 @interface ViewController ()<UITextViewDelegate>

 @property (weak, nonatomic) IBOutlet UITextView *textView;
 @property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
 @property (strong, nonatomic) GrowingTextViewHandler *handler;

 @end

 @implementation ViewController

 - (void)viewDidLoad {
   [super viewDidLoad];
   self.handler = [[GrowingTextViewHandler alloc]initWithTextView:self.textView withHeightConstraint:self.heightConstraint];
   [self.handler updateMinimumNumberOfLines:3 andMaximumNumberOfLine:8];
}

 - (void)textViewDidChange:(UITextView *)textView {
   [self.handler resizeTextViewWithAnimation:YES];
}
@end
于 2015-06-03T10:10:13.190 回答