Skip to content

Instantly share code, notes, and snippets.

@neilinglis
Created December 11, 2012 17:50
Show Gist options
  • Save neilinglis/4260598 to your computer and use it in GitHub Desktop.
Save neilinglis/4260598 to your computer and use it in GitHub Desktop.
NSTextAlignment Naming Differences
Am I out of order being annoyed at things like this? The naming has changed subtly, the direction is at the end in UIKit but at the start on AppKit. If I'm writing cross platform code then I need to either use the int values directly, which is bad practice, or use a define.
UIKit:
enum {
NSTextAlignmentLeft = 0,
NSTextAlignmentCenter = 1,
NSTextAlignmentRight = 2,
NSTextAlignmentJustified = 3,
NSTextAlignmentNatural = 4,
};
typedef NSInteger NSTextAlignment;
AppKit:
typedef enum _NSTextAlignment {
NSLeftTextAlignment = 0,
NSRightTextAlignment = 1,
NSCenterTextAlignment = 2,
NSJustifiedTextAlignment = 3,
NSNaturalTextAlignment = 4
} NSTextAlignment;
@neilinglis
Copy link
Author

So basically I had to do this,

if TARGET_OS_IPHONE

    self.justification = @(NSTextAlignmentCenter);

else

    self.justification = @(NSCenterTextAlignment);

endif

@neilinglis
Copy link
Author

It's worse, I didn't notice at the time that the values go Left/Center/Right on UIKit and Left/Right/Center on AppKit. I was storing the int value in a cross platform file. So we had a subtle bug that sometimes text alignment was off.

What I should have done is created our own enum and convert between them on both platforms. I think that's a fair bit of developer inconvenience just because someone on the UIKit team presumably thought that they'd be better redefining the values to be more consistent.

I imagine it's too late to radar this now, I just have to grin and bear it.

@randomsequence
Copy link

I think they differ in UIKit so that the new NSTextAlignment values were the same as their deprecated UITextAlignment counterparts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment