Ios UITextView,使用自动布局动态扩展到滚动视图中的文本
我有一个包含一些组件的Ios UITextView,使用自动布局动态扩展到滚动视图中的文本,ios,uiscrollview,uitextview,autolayout,Ios,Uiscrollview,Uitextview,Autolayout,我有一个包含一些组件的UIScrollView,其中一个组件是UITextView,我想要的是让UITextView随UIScrollView动态扩展,事实上我使用的是autoLayout,因此此代码不起作用: CGRect frame = self.detailTextView.frame; frame.size.height = self.detailTextView.contentSize.height; self.detailTextView.frame = fr
UIScrollView
,其中一个组件是UITextView
,我想要的是让UITextView
随UIScrollView
动态扩展,事实上我使用的是autoLayout
,因此此代码不起作用:
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];
我想要的是帮助我用故事板元素来做这件事
将文本视图的一个高度约束拖动到控制器中,以创建出口,而不是设置框架。然后在viewWillLayoutSubviews中获取contentSize并设置文本视图高度约束的常量。这将完成您在上面代码中使用框架所做的工作。只需确保文本视图从所有边缘到滚动视图都有约束,以便滚动视图能够正确调整自身大小。不要设置框架,而是将文本视图的一个高度约束拖动到控制器中以创建出口。然后在viewWillLayoutSubviews中获取contentSize并设置文本视图高度约束的常量。这将完成您在上面代码中使用框架所做的工作。只需确保文本视图具有从所有边到滚动视图的约束,以便滚动视图能够正确调整自身大小。在我的例子中,我所做的是将滚动视图保留为超级视图,并为子视图添加所有约束,而uitextview具有从所有边到滚动视图的约束。然后更新了-viewdilayoutsubviews方法中的内容大小。以下是代码片段:
-(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)];
}
在我的例子中,我所做的是将scrollview保留为超级视图,并为子视图添加所有约束,而uitextview具有从所有边到scrollview的约束。然后更新了-viewdilayoutsubviews方法中的内容大小。以下是代码片段:
-(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)];
}
这在某种程度上是可行的,但它们的约束更改的时间比它们应该更改的时间要晚,这样您就可以在加载视图后实际看到它在移动。有没有办法解决这个问题?在设置文本后设置高度约束<代码>self.textviewHeightConstraint.constant=self.textview.contentSize.height代码>这在某种程度上是可行的,但它们的约束更改的时间比它们应该更改的时间要晚得多,这样您就可以在加载视图后实际看到它在移动。有没有办法解决这个问题?在设置文本后设置高度约束<代码>self.textviewHeightConstraint.constant=self.textview.contentSize.height代码>