Amogh Talpallikar
Amogh Talpallikar

Reputation: 12184

How to set an enum not present in current iOS version?

I am trying to set linebreakmode for textLabel in UIButton.

self.scheduleButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;

NSLineBreakByWordWrapping won't be available prior to iOS 6.0.

self.scheduleButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;

will work prior to iOS6 but XCode is throwing an error saying, it doesn't recognize UILineBreakModeWordWrap.

How do I make sure that UILineBreakModeWordWrap code is completely ignored prior to iOS 6.

I have made a macro to check OS version, I would have used respondsToSelector, had it been a case with a method but this is an enum type.

SYSTEM_VERSION_LESS_THAN(@"6.0")

I am using this here but in an if-else scenario but it wont still work as XCode will still tell me that UILineBreakModeWordWrap is not recognizable.

Is there a way I can know OS version at pre-processing level ?

#define SYSTEM_VERSION_LESS_THAN(v)                ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

This macro that I have made will just add Objective C code to determine OS type which means the check will happen at runtime.

but because of this XCode will not ignore UILineBreakModeWordWrap.

Upvotes: 1

Views: 526

Answers (2)

Mrunal
Mrunal

Reputation: 14128

Option:1

You cane create your own macros to handle this:

#ifdef __IPHONE_6_0
# define LINE_BREAK_MODE_WORD_WRAP NSLineBreakByWordWrapping
#else
# define LINE_BREAK_MODE_WORD_WRAP UILineBreakModeWordWrap
#endif

Now in code you can use, LINE_BREAK_MODE_WORD_WRAP

Option:2

Directly use enum value instead of reference name, both NSLineBreakByWordWrapping and UILineBreakModeWordWrap are having same value - 0

Hence you can directly write this way as well -

self.scheduleButton.titleLabel.lineBreakMode = 0;

It will automatically picks up 0th value from iOS SDK enumeration reference.

Hope this helps.

Upvotes: 1

rmaddy
rmaddy

Reputation: 318884

Both NSLineBreakByWordWrapping and UILineBreakModeWordWrap have the same value. As long as your Base SDK is 6.0 or later then using NSLineBreakByWordWrapping will make the compiler happy and it will work for any version of iOS at runtime.

Upvotes: 6

Related Questions