当我有多个textViews时,如何动态修复我的scrollView? [英] How can i fix my scrollView dynamically when i have more than one textViews in it?

查看:82
本文介绍了当我有多个textViews时,如何动态修复我的scrollView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个scrollView,它由detailView中的3个textViews,按钮和标签组成。我正在使用3个文本视图,因为我的视图标题,日期和描述需要不同的字体。问题有时候描述很长,有时它的标题很小而且相同。因为标题和描述之间有很多空格,所以视图并不好看。是否可以动态设置textView的大小和滚动视图,或者是否有更好的方法来解决此问题。 thanx提前

I have a scrollView which consists of 3 textViews, buttons and labels in a detailView. i am using 3 text view because i need different fonts for my view title, date and description. problem is sometimes description is long and sometimes its small and same thing with headings. then view doesn't loog good at all because of alot of empty spaces between title and description. Is it possible to set the size of textView and scroll view dynamically or is there any better approach to solve this problem. thanx in advance

推荐答案

您需要调整文本视图的帧以匹配各自的contentSizes(请参阅以下顶部答案: 如何根据内容调整UITextView的大小?关于如何做到这一点,使用textview的contentSize属性),然后根据所有控件的框架调整scrollView的contentSize。

You need to adjust the frames of the text views to match their respective contentSizes (see the top answer here: How do I size a UITextView to its content? on how to do that, uses the contentSize property of the textview), then you adjust the contentSize of the scrollView based on the frames of all the controls.

假设你的textviews连续垂直放置(如果有间距,只需调整代码等):

Assuming your textviews are placed consecutively vertically (just adjust the code if there is spacing, etc.):

// (first adjust each text view's frame per the linked answer)
...
// then adjust the frames of the content
CGRect topTextViewFrame = topTextView.frame;
CGRect middleTextViewFrame = middleTextView.frame;
middleTextViewFrame.origin.y = topTextViewFrame.origin.y + topTextViewFrame.size.height;
middleTextView.frame = middleTextViewFrame;
CGRect bottomTextViewFrame = bottomTextView.frame;
bottomTextViewFrame.origin.y = middleTextViewFrame.origin.y + middleTextViewFrame.size.height;
// then adjust your other controls based on these frames, for example:
CGRect myButtonFrame = myButton.frame;
myButtonFrame.origin.y = bottomTextViewFrame.origin.y + bottomTextViewFrame.size.height;
// finally adjust the contentSize of the scrollview assuming the button is the bottom element
CGSize csize = myScrollView.contentSize;
csize.height = myButtonFrame.origin.y + myButtonFrame.size.height;
myScrollView.contentSize = csize;

这篇关于当我有多个textViews时,如何动态修复我的scrollView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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