当我有多个textViews时,如何动态修复我的scrollView?

时间:2011-02-21 10:47:56

标签: iphone objective-c xcode interface-builder ios-4.2

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

2 个答案:

答案 0 :(得分:1)

您可以将帧的大小设置为依赖于字符长度,方法是将帧设置为:

CGRectMake (0.0, 0.0, [yourString count]*10, 30.0);

这是我在UIPopover提出变量名时所做的。

答案 1 :(得分:1)

您需要调整文本视图的帧以匹配各自的contentSizes(请参阅此处的顶部答案:How do I size a UITextView to its content?如何执行此操作,使用textview的contentSize属性),然后调整contentSize基于所有控件的框架的scrollView。

假设您的文本视图是垂直连续放置的(如果有空格等,只需调整代码):

// (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;