Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cant get the uitextview height dynamically correct

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

like image 805
mrugen munshi Avatar asked Apr 12 '11 17:04

mrugen munshi


People also ask

How do I size a UITextView to its content?

It can be done using the UITextView contentSize . This will not work if auto layout is ON. With auto layout, the general approach is to use the sizeThatFits method and update the constant value on a height constraint. CGSize sizeThatShouldFitTheContent = [_textView sizeThatFits:_textView.

How do I increase the height of a TextField in Swift?

Step 1 : Click the Attribute Inspector, Select the Border Styles which is not the rounded one. Step 2 : Now go to Size Inspector and change the size of the TextField. Step 3 : Create an @IBOutlet for TextField and then add below code inside the viewWillAppear() or viewDidAppear() .


3 Answers

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.

like image 148
CharlieMezak Avatar answered Nov 03 '22 22:11

CharlieMezak


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

like image 22
shannoga Avatar answered Nov 04 '22 00:11

shannoga


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;
like image 33
Alberto Lopez Avatar answered Nov 03 '22 22:11

Alberto Lopez