2

I am calculating the Correct height of my Dynamic textView . But i am not getting the correct height here. What should i do to get the correct height .I have to display some arabic text . Any tricks to get the correct size. Please help me

CGSize size = [foundationTrimmed sizeWithFont:newsSecondPart.font constrainedToSize:             CGSizeMake(newsSecondPart.frame.size.width-11,10000)];
size.height = size.height + 50;

CGRect newsSecondPartFrame = newsSecondPart.frame;
newsSecondPartFrame.size.height = size.height;
newsSecondPart.frame = newsSecondPartFrame;

CGRect CGRectnewsImageFrame = newsImageView.frame;
CGRectnewsImageFrame.origin.y = newsSecondPartFrame.size.height+40;   
newsImageView.frame = CGRectnewsImageFrame;

scrollView.contentSize = CGSizeMake(  0, newsSecondPartFrame.size.height+CGRectnewsImageFrame.size.height+ 100);

newsSecondPart.text = foundationTrimmed;
JoseK
  • 31,141
  • 14
  • 104
  • 131
proCoder
  • 81
  • 1
  • 9

1 Answers1

1

Say the text view with the two red dots is your textview (in its current state).

enter image description here

The coordinates of the red dot at the top left edge of the textview are (CGRectGetMinX(textview.frame), CGRectGetMinY(textView.frame)).

The coordinates of the red dot at the bottom right edge of the textview are (CGRectGetMaxX(textview.frame), CGRectGetMaxY(textview.frame)).

This is because the geometry of the coordinate system for UIViews causes the "x" coordinate to increase as you move right, and the "y" coordinate to increase as you move down, so the origin is at the top-leftmost corner of the view.

You want the height of the view, so you have three options:

Option 1. The first option is to subtract the bottom y coordinate and top y coordinate of the textview like this:

int heightOfTextView = (CGRectGetMaxY(textview.frame)) - (CGRectGetMinY(textview.frame));

Option 2. The second option is to simply get the max y coordinate in terms of the textview's bounds. First though, you should get an understanding of the difference between a view's frame and its bounds. This definition of frame and bounds was taken from this question:

The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).

The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

So you would do this for option two:

int heightOfTextView = CGRectGetMaxY(textview.bounds);

Option 3. This option is actually the simplest way to do it:

int heightOfTextView = textview.bounds.size.height;

With any of the three options, you end up with the correct height of the textview, and you can put this code anywhere you want to calculate the height of the textview. Just a note: the second option won't work if you change the bounds with setBoundsOrigin:, but it is unlikely you'll do that with a textview.

Hope this helps!

Community
  • 1
  • 1
pasawaya
  • 11,515
  • 7
  • 53
  • 92