9
votes

I am having issue in getting the correct size of uitextview.In my application i am fetching the data from webservice and i set the height of uitextview as per the data from webservice .But whenever there is space in first 35 letters(frame of my textview is (20,0,255,2000)). then the hjeight calculated is always one line less then correct height and even the uitext view starts to print the line after space in the new line (may be word is big thats why ??).

So i am getting the wrong text view height.

The text view apears somwethibng like this :

firstword spacesspacesspacesspaces spacesspacesspacesspaces spacesspacesspacesspaces spacesspacesspacesspaces spacesspacesspacesspaces

When i enter text which is say without spaces it gives me correct height suppose say the data is "ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt" Then in the above scenerio Cgsize will generate proper result for me .

Here i am not using UILabel is because i want link detection thats why . If anyone has anyother suggestions it is highly welcomed.

Regards Mrugen

6

6 Answers

33
votes

Using NSString's sizeWithFont methods doesn't really work for a text view. It works great for a UILabel, but text views seem to have extra padding around the text. If you use that method, you end up with a size that's a little short.

UITextView is a subclass of UIScrollView. After you set the textview's text property, its contentSize property is automatically updated to fit the text. So, if you want to set the text view's height to fit its text just do:

textView.text    = @"some text";
CGRect rect      = textView.frame;
rect.size.height = textView.contentSize.height;
textView.frame   = rect;

That will set your textView's hight so it contains the text.

9
votes

You can measure the height of the text with this function and set your frame according to the returned height.

   -(CGSize)sizeOfText:(NSString *)textToMesure widthOfTextView:(CGFloat)width withFont:(UIFont*)font
    {
        CGSize ts = [textToMesure sizeWithFont:font constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
        return ts; 
    }

you use it like that:

 CGSize size =   [self sizeOfText:@"the string you want to present" widthOfTextView:255 withFont:fontYouAreUsing];

And then you pass it to the text view:

   textView.frame = CGRectMake(20,0,255,size.height);

Good luck

shani

1
votes

As of iOS7 you should now use:

CGRect textRect = [textToMesure boundingRectWithSize:CGSizeMake(width, FLT_MAX)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:font}
                                     context:nil];

CGSize size = textRect.size;
0
votes

The answer for your question is Create an instance for UITextView,In my case I have created as txtView and then add text to it and print out the height in content height in console.

NSLog(@"TextView Height is :%d",txtView.contentSize.height)

0
votes

Get the estimated Height and Width of any textField: (Swift 4)

It take the text of TextField as argument.

private func estimateFrameForText(text: String) -> CGRect {

        let size = CGSize(width: 200.0, height: 1000)

        let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)

        return NSString(string: text).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17.0)], context: nil)
    }
0
votes

Tried in Swift 4,

textView.intrinsicContentSize.height