使用AutoLayout使用文本垂直调整UITextView的大小

时间:2015-01-24 20:16:52

标签: ios iphone autolayout uitextview

我希望使用here给出的答案来解决我的问题,但是UITextView没有intrinsicContentSize方法。有谁知道我怎么做这个?在Android上,我所要做的就是在布局文件中说“wrap-content”。但是在iOS这里付出了代价。

关于Resize a UITextField while typing (by using Autolayout)的建议,我不确定为什么这些人在委托方法的签名中使用IBAction。无论如何,它们对我不起作用。

更新

我以为我用以下方法解决了这个问题。但“增长”是暂时的。该字段仅在我输入时增长。一旦我停止输入,它将返回到较小的尺寸。我怎么能让它更永久?

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString
*)text {   [self resizeTextViewToFitContent:textView]; }

- (void)resizeTextViewToFitContent:(UITextView *)textView {
    CGRect frame = textView.frame;
    frame.size.height=[self heightOfTextView:textView]+5;
    textView.frame=frame; }

-(CGFloat)heightOfTextView:(UITextView *)textView {
    CGRect reviewTextFrame = [textView.attributedText boundingRectWithSize:CGSizeMake(textView.bounds.size.width, MAXFLOAT)
                                                                   options:NSStringDrawingUsesLineFragmentOrigin
                                                                   context:nil];
    return CGRectGetHeight(reviewTextFrame); }

0 个答案:

没有答案