【问题标题】:Resize UItextview according to it's content in iOS7根据iOS7中的内容调整UItextview的大小
【发布时间】:2013-11-19 04:06:56
【问题描述】:

我正在尝试根据内容以及它的兄弟容器和父容器来调整文本视图的大小。

以下代码在 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;

【问题讨论】:

    标签: ios ios7 uitextview sizetofit


    【解决方案1】:

    似乎 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'ssetContentSize:方法是从layoutSubviews调用的。因此,在textView 上调用layoutIfNeeded(它本身调用layoutSubviews在其layoutManager 上调用ensureLayoutForTextContainer:,应该使textView's contentSize 正确。

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

    【讨论】:

    • 点击以下链接获取准确答案:trio-hasim.blogspot.in/2014/01/…
    • 另请注意,在 viewDidLayoutSubview 中计算完布局后,至少应进行调整大小,以确保 layoutContainer 上的一些数学运算已经执行。如果您从 Internet 更新内容,请使用 layoutIfNeeded 触发布局,然后更新 viewDidLayoutSubview 中的大小。
    【解决方案2】:

    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
    

    【讨论】:

      猜你喜欢
      • 2010-09-08
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 2013-10-03
      • 1970-01-01
      相关资源
      最近更新 更多