0

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

Piscean
  • 3,069
  • 12
  • 47
  • 96

2 Answers2

1

You could set the size of the frame to be dependent on the character length by setting the frame at the onset to be:

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

This is what I did when I had a UIPopover come up with a variable name.

SushiGrass Jacob
  • 19,425
  • 1
  • 25
  • 39
1

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.

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;
Community
  • 1
  • 1
Bogatyr
  • 19,255
  • 7
  • 59
  • 72
  • i put all text views and buttons in the scrollView after each other and then i tried to do what u said in this code. but its not working – Piscean Feb 21 '11 at 13:21
  • you're going to need to be more precise about what you mean when you say "it's not working". What's not working? The sizing of the textviews to the content, or the placement of the frames, or both? I like to set different background colors to views when I'm working on getting them placed properly, it helps to highlight where they are – Bogatyr Feb 21 '11 at 13:36
  • every textView's frame should start from where its upper textViews frame ends. but its not happening like that. if 1st textViews frame has more text then it comes over 2nd textView. and in some detailViews last button doesn't fit into scrollView's content. – Piscean Feb 21 '11 at 14:07
  • You need to stick with one question and stop creating new questions that are all essentially the same. Do some debugging, print out all the frames, and double / triple check your calculations. Start with just getting the size of the top frame correct, then move on to positioning the lower frames one by one. You should be able to point to some concrete problem immediately, like: "the frame of the top textview is not adjusting to the size of its content". Post *concrete* statements of what is happening and ask more targetted questions, not vague "it's not working" statements. – Bogatyr Feb 21 '11 at 14:10