13

I'm using NSLog to inspect a UITextView. I have the following logging statements in my code:

    NSLog(@"textView: %@",textView);
    NSLog(@"textView.frame: %@",textView.frame);
    NSLog(@"[textView frame]: %@",[textView frame]);

And in the output to the console, i get the following:

2010-11-29 22:00:38.252 MenuMaker[57602:207] textView: <UITextView: 0x3b3afe0; frame = (0 0; 320 387); text = 'blah...'; clipsToBounds = YES; autoresize = RM+BM; layer = <CALayer: 0x3b3afa0>>
2010-11-29 22:00:38.254 MenuMaker[57602:207] textView.frame: (null)
2010-11-29 22:00:38.254 MenuMaker[57602:207] [textView frame]: (null)

The first line of output, since it contains the 'frame = (0 0; 320 387)' bit, leads me to believe that the UITextView's frame variable is all set up. But why do the next two lines show null in that case? Shouldn't they dump the frame's values?

Thanks in advance

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Chris
  • 39,719
  • 45
  • 189
  • 235
  • Is this something to do with the fact that frame is a cgrect which is a structure not a class? – Chris Nov 29 '10 at 11:17

5 Answers5

50

As the others have noted, CGRect is just a struct and, therefore, lacks a -description method (which is what the +stringWithFormat static method of NSString—used by NSLog—replaces %@ with in a format string). Therefore, you cannot pass it to NSLog directly.

However, there is no need to output any of the values individually like suggested. Cocoa provides a number of built-in methods that render several Core Graphics structures to a string:

NSStringFromCGRect()
NSStringFromCGPoint()
NSStringFromCGSize()
NSStringFromCGAffineTransform()

And so on. Therefore, outputting a CGRect in the form of a string becomes super-easy:

NSLog(@"%@", NSStringFromCGRect(rect));

You can find all these helper methods here under “String Conversions.”

Count Chocula
  • 1,041
  • 8
  • 4
10

You can also do like below

NSLog(@"%@",[NSValue valueWithCGRect:textView.frame]);
Iphone_bharat
  • 1,169
  • 1
  • 11
  • 30
4

UIView.frame is just a CGRect struct (which in turns contains a CGPoint and a CGSize struct). So no classes are involved, and because only classes can have a string representation, you get displayed null. What you must do if you want the values of the frame is something like this:

NSLog(@"textView.frame origin: %f %f", textView.frame.origin.x, textView.frame..origin.y);
NSLog(@"textView.frame size: %f %f", textView.frame.size.width, textView.frame.size.height);
slup
  • 5,484
  • 4
  • 19
  • 14
3

textview. frame will return a CGRect value. So if you want to know the frame values of a textView

NSLog(@"textView frame height: %f",textView.frame.size.height);
NSLog(@"textView frame width: %f",textView.frame.size.width);
NSLog(@"textView frame x Position: %f",textView.frame.origin.x);
NSLog(@"textView frame y Position: %f",textView.frame.origin.y);

NSLog will consider that value as null when it doesnt recognize the object what the recieved.

ipraba
  • 16,485
  • 4
  • 59
  • 58
-1

In Swift it's simple:

println("\(textView.frame)")
Segev
  • 1,267
  • 16
  • 21