UIScrollView大小匹配UITextView大小

我有UITextView的文本块的UIScrollView 。 文本可以是来自服务器的任何数量的单词。

我想滚动视图是正确的滚动大小来匹配UITextView的大小。

目前的做法是什么?

截图:

在这里输入图像说明

UITextViewUIScrollView的扩展。 所以你可以使用contentSize propery来获得它的内容大小。

你可以使用下面的东西来获得你的textview的高度

 CGSize size = [@"Your string" sizeWithFont:[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:22] constrainedToSize:CGSizeMake(500, CGFLOAT_MAX)]; _textView.frame = CGRectMake(x,y, width, size.height); _scrollview.frame = CGRectMake(x,y, width, size.height); 

或者可以使用littl hack:p使用Label来获得文本视图的高度。

  UILabel *tempLabel = [[UILabel alloc]initWithFrmae:_textView.bounds]; tempLabel.numberOfLines = 0; // Important to do tempLabel.text = @"Your Text"; [tempLabel sizeToFit]; // This will resize your Label and now you can use it's height to manage your textView. 

现在使用这个tempLabel的高度

 tempLabel.frame.size.height