UITextView的动态扩展到使用自动布局的滚动视图内的文本 [英] UITextView that expands dynamically to text inside a scroll view using auto layout

查看:177
本文介绍了UITextView的动态扩展到使用自动布局的滚动视图内的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的UIScrollView ,其中包括一些组件,而这些组件之一是的UITextView ,我想要什么是让的UITextView 的UIScrollView ,其实我用的autoLayout ,所以这个code不工作:

I have a UIScrollView that includes some components, and one of these components is a UITextView, what I want is to let the UITextView dynamically expands with the UIScrollView, in fact I use autoLayout, so this code is not working:

    CGRect frame = self.detailTextView.frame;
    frame.size.height = self.detailTextView.contentSize.height;
    self.detailTextView.frame = frame;
    scroll.contentSize = CGSizeMake(scroll.contentSize.width,
                                    300 + self.detailTextView.frame.size.height);
    [self.detailTextView setFrame:frame];

我要的是帮助我的故事板元素这样做。

What I want is to help me doing this with storyboard elements.

推荐答案

,我所做的是不停的滚动型超级视图,并增加了子视图的所有约束和UITextView的有来自所有边缘滚动视图约束。然后更新-viewDidLayoutSubviews方法的内容大小。这里是code片断:

in my case, what I did is kept the scrollview as super view and added all constraints for the subviews and the uitextview has constraint from all edges to the scrollview. then updated the content size in -viewDidLayoutSubviews method. here is the code snippet:

-(void)viewDidLayoutSubviews{
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12.0]};

    CGRect rect = [_detailText.text boundingRectWithSize:CGSizeMake(_detailText.frame.size.width - 10.0, MAXFLOAT)
                                             options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                          attributes:attributes
                                             context:nil];
    CGRect frame = _detailText.frame;
    frame.size.height = ceil(rect.size.height) + _detailText.textContainerInset.top +             _detailText.textContainerInset.bottom;
    _detailText.frame = frame;
    _detailText.contentSize = CGSizeMake(_detailText.frame.size.width, _detailText.frame.size.height);
    [_contentScrollView setContentSize:CGSizeMake(_contentScrollView.frame.size.width, _detailText.frame.origin.y + _detailText.frame.size.height)];
}

这篇关于UITextView的动态扩展到使用自动布局的滚动视图内的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆