1

I'm trying to change the text shadow position of my UISegmentedControl. To do this, the documentation says I need to pass in an NSValue which wraps a UIOffset struct.

UITextAttributeTextShadowOffset
    Key to the offset used for the text shadow in a text attributes dictionary.
    The corresponding value is an instance of NSValue wrapping a UIOffset struct.
    Available in iOS 5.0 and later.
    Declared in UIStringDrawing.h.

The problem is NSValue has no such wrapper method for a UIOffset struct - just search the documentation. I saw a lot of sample code use this nonexistent function.

[NSValue valueWithUIOffset:UIOffsetMake(0, 1)]

It crashes when I try to run it. I don't understand how so many sample code can run it.

Community
  • 1
  • 1
JoJo
  • 19,587
  • 34
  • 106
  • 162

2 Answers2

5

Check out the NSValue UIKit Additions, a Category that, among other useful things, adds the -[NSValue valueWithUIOffset:] method you're after.

Kristian Glass
  • 37,325
  • 7
  • 45
  • 73
1

It may not be in the documentation, but it exists, and it works:

NSValue *uiOffsetValue = [NSValue valueWithUIOffset:UIOffsetMake(0, 1)];
NSLog(@"%@", uiOffsetValue);
// outputs "UIOffset: {0, 1}"

What do you mean, it crashes when you try to run it? If the method doesn't exist, then it shouldn't even compile. If your app compiles but crashes, the problem has to be something else.

yuji
  • 16,695
  • 4
  • 63
  • 64
  • Xcode only warns against passing unknown messages to objects. Objective-C will crash at runtime when an unknown message is passed to an object. – JoJo Mar 30 '12 at 21:38
  • Really? I have a vanilla Xcode setup, and if I try to use an unknown method I get a "No known class method for selector 'foo:'", and it doesn't even compile. – yuji Mar 30 '12 at 21:45
  • Actually, after further testing. It only crashed on my iOS 4 device. It's working on my iOS 5 device. This is understood because setting shadow position is only supported on 5. But there is still an unsolved mystery as to why valueWithUIOffset is not in the documentation. – JoJo Mar 30 '12 at 21:49
  • It is in the documentation - see here: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/NSValue_UIKit_Additions/Reference/Reference.html It also says "Available in iOS 5.0 and later", so that explains why it crashes on your iOS 4 device. – jsd Mar 30 '12 at 23:24