0

I'm using UIScrollView in which I have placed a UIImageView and a UITextView. I make the UIScrollView to scroll both the images and text and it works fine, but my UITextView contains dynamic text (i.e number of lines is different for each time). So I can't find the way to assign contentSize of UITextView. Is there any way to do this?

objc-obsessive
  • 825
  • 1
  • 12
  • 18
varth_FD
  • 137
  • 1
  • 2
  • 12

2 Answers2

1

This and this might help you.
You can put a condition that if width of the new CGSize is greater than textview width then number of lines = 2 else 1.

Community
  • 1
  • 1
Nitish
  • 13,845
  • 28
  • 135
  • 263
0

calculate the text width and define your text view width using below code

+(float) calculateHeightOfTextFromWidth:(NSString *) text: (UIFont *)withFont: (float)width
:(UILineBreakMode)lineBreakMode
{

[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(215, 1000) lineBreakMode:lineBreakMode];

[text release];
[withFont release];

return suggestedSize.height;
}

and use when you want to display dynamic text as

float titleHeight;

titleHeight = [Your view controllre ViewController calculateHeightOfTextFromWidth:[NSString stringWithFormat:@"%@",[dict objectForKey:@"title"]] :[UIFont systemFontOfSize:14]:300 :UILineBreakModeTailTruncation]; 

give this titleHeight to your textview or do calculation dividation using titleheight ,you can get the number of lines

If you get problem then reply

Nitish
  • 13,845
  • 28
  • 135
  • 263
Hiren
  • 12,720
  • 7
  • 52
  • 72