是否有一个好的方法来调整UITextView的大小以符合其内容?例如,我有一个包含一行文本的UITextView:

"Hello world"

然后我添加了另一行文本:

"Goodbye world"

在Cocoa Touch中是否有一个好的方法来获取矩形来保存文本视图中的所有行,这样我就可以相应地调整父视图?

另一个例子是,查看Calendar应用程序中用于事件的notes字段——注意单元格(以及它包含的UITextView)如何展开以保存notes字符串中的所有文本行。


当前回答

这适用于iOS 6.1和iOS 7:

- (void)textViewDidChange:(UITextView *)textView
{
    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    textView.frame = newFrame;
}

或在Swift(适用于Swift 4.1在iOS 11)

let fixedWidth = textView.frame.size.width
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
textView.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)

如果你想要支持iOS 6.1,那么你还应该:

textview.scrollEnabled = NO;

其他回答

如果你正在使用滚动视图和内容视图,你想增加高度取决于TextView的内容高度,那么这段代码将帮助你。

希望这能有所帮助,它在iOS9.2中工作得很完美

当然还有set textview。scrollEnabled = NO;

-(void)adjustHeightOfTextView
{
    //this pieces of code will helps to adjust the height of the uitextview View W.R.T content
    //This block of code work will calculate the height of the textview text content height and calculate the height for the whole content of the view to be displayed.Height --> 400 is fixed,it will change if you change anything in the storybord.


CGSize textViewSize = [self.textview sizeThatFits:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];//calulcate the content width and height

float textviewContentheight =textViewSize.height;
self.scrollview.contentSize = CGSizeMake(self.textview.frame.size.width,textviewContentheight + 400);//height value is passed
self.scrollview.frame =CGRectMake(self.scrollview.frame.origin.x, self.scrollview.frame.origin.y, self.scrollview.frame.size.width, textviewContentheight+400);

CGRect Frame = self.contentview.frame;
Frame.size.height = textviewContentheight + 400;
[self.contentview setFrame:Frame];

self.textview.frame =CGRectMake(self.textview.frame.origin.x, self.textview.frame.origin.y, self.textview.frame.size.width, textviewContentheight);
[ self.textview setContentSize:CGSizeMake( self.textview.frame.size.width,textviewContentheight)];
self.contenview_heightConstraint.constant = 

self.scrollview.bounds.size.height;
    NSLog(@"%f",self.contenview_heightConstraint.constant);
}

这工作得很好,当我需要使文本在一个UITextView适合一个特定的区域:

// The text must already be added to the subview, or contentviewsize will be wrong.

- (void) reduceFontToFit: (UITextView *)tv {
    UIFont *font = tv.font;
    double pointSize = font.pointSize;

    while (tv.contentSize.height > tv.frame.size.height && pointSize > 7.0) {
        pointSize -= 1.0;
        UIFont *newFont = [UIFont fontWithName:font.fontName size:pointSize];
        tv.font = newFont;
    }
    if (pointSize != font.pointSize)
        NSLog(@"font down to %.1f from %.1f", pointSize, tv.font.pointSize);
}

你试过[textView sizeThatFits:textView. txt]吗?范围)?

编辑:sizeThatFits返回大小,但并不实际调整组件的大小。我不确定如果这是你想要的,或者如果[textView sizeToFit]是更多的你要寻找的。无论哪种情况,我不知道它是否完全适合你想要的内容,但这是第一件要尝试的事情。

对我来说最简单的解决方案是在Storyboard的textView上放一个高度约束,然后将textView和高度约束连接到代码:

@IBOutlet var myAwesomeTextView: UITextView!
@IBOutlet var myAwesomeTextViewHeight: NSLayoutConstraint!

然后在设置文本和段落样式后,在viewDidAppear中添加这个:

self.myAwesomeTextViewHeight.constant = self.myAwesomeTextView.contentSize.height

一些注意事项:

与其他解决方案相比,必须将isScrollEnabled设置为true才能使其工作。 在我的例子中,我在代码中设置自定义属性的字体,因此我必须在viewDidAppear中设置高度(在此之前它不会正常工作)。如果你没有在代码中更改任何文本属性,你应该能够在viewDidLoad或设置文本后的任何地方设置高度。

禁用滚动

添加constaints

然后添加文本

[yourTextView setText:@"your text"];
[yourTextView layoutIfNeeded];

如果你使用UIScrollView,你也应该添加这个;

[yourScrollView layoutIfNeeded];

-(void)viewDidAppear:(BOOL)animated{
    CGRect contentRect = CGRectZero;

    for (UIView *view in self.yourScrollView.subviews) {
         contentRect = CGRectUnion(contentRect, view.frame);
    }
    self.yourScrollView.contentSize = contentRect.size;
}