0

I am using the ios library UIPlaceHolderTextView in my application: https://github.com/JohnnyZeng/UIPlaceHolderTextView which is linked to the following question:

Placeholder in UITextView

The library itself is working fine, however, I need to calculate the height of the area after the user has entered text, and unfortunately, I am always getting a value of 0.00000 despite the fact that the text area contains text. Why is this? Here is my relevant code:

//self.textView is a reference to a UIPlaceHolderTextView object, and text is its attribute

self.textView.text = @"Text that the user has entered, and can be of any length";
    float height = MIN(175, [self.textView.attributedText boundingRectWithSize:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:NULL].size.height);

NSLog(@"textview height is: %f", self.textView.frame.origin.y);
NSLog(@"textview height is: %f", self.textView.frame.size.height);

The output in both lines are 0.00000. Can anyone see why this is happening? My guess is that the problem lies in the expression self.textView.attributedText, but not sure if I'm correct, and if so, what I should do instead.

halfer
  • 19,824
  • 17
  • 99
  • 186
syedfa
  • 2,801
  • 1
  • 41
  • 74
  • 1
    ...and what the `NSLog(@"%f", height);` would say...? because it seems that ivar has the calculated height only in your code fragment, the actual `self.textView` does not. – holex Mar 26 '14 at 16:59
  • I'm not sure I understand. Could you please clarify? – syedfa Mar 26 '14 at 17:02
  • My output is 0.00000. How would I rectify the situation as you've pointed out? – syedfa Mar 26 '14 at 17:06
  • Why would we expect self.textView.frame.size.height to change if you never show code like self.textView.frame.size.height = height? – stevesliva Mar 26 '14 at 17:11
  • I'm not setting the value of self.textView.frame.size.height anywhere. I'm trying to determine the value of it based on the text that the user has entered. – syedfa Mar 26 '14 at 17:19
  • Obvious first question from Mr. Obvious: is `self.textView` definitely not `nil`? – Tommy Mar 26 '14 at 17:35

2 Answers2

0

you are assigning

`self.textView.text = @"Text that the user has entered, and can be of any length";`

and calculating heightfor self.textView.attributedText which is nil i guess please do appropriate changes. It should work.

To make is work you should assign value to self.textView.attributedText

jnix
  • 480
  • 3
  • 10
  • that should be okay, because the `attributedText` will be auto-populated from the `text`, `font`, `textColor`, `textAlignment`, ect... values, and vica-versa, as the docs says here: https://developer.apple.com/library/ios/documentation/uikit/reference/uitextview_class/Reference/UITextView.html#//apple_ref/occ/instp/UITextView – holex Mar 26 '14 at 16:56
0
Use a local textview of same width as real textview and set the same text with sizeToFit property.it will be returned in correct frame.

 UITextView *tv = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];

    //whatever alignment of real text view you have set
    tv.textAlignment = NSTextAlignmentJustified;
        tv.text= @"Text that the user has entered, and can be of any length";
    [tv sizeToFit];

    float height  =  tv.frame.size.height;
Tanvi Jain
  • 917
  • 2
  • 7
  • 19