Search code examples
iosios5ios6uilabelline-breaks

NSLineBreakMode enum and backwards compatibility


I need my app to support iOS 5+. Since prior iOS 6 the enum lineBreakMode for line break mode in UILabel is of type UILineBreakMode, and it is of type NSLineBreakMode for iOS 6+, what should be the best (or more correct) way to check the iOS version currently running to determine the type to be used? Is it correct to directly do something like [[UIDevice currentDevice] systemVersion], or is there a better way?

Thanks!


Solution

  • You do not need to check the iOS version at runtime, the enum values are the same, the compiled code will not be changed when moving from UILineBreakMode to NSLineBreakMode

    enum {
    
    NSLineBreakByWordWrapping = 0,
       NSLineBreakByCharWrapping,
       NSLineBreakByClipping,
       NSLineBreakByTruncatingHead,
       NSLineBreakByTruncatingTail,
       NSLineBreakByTruncatingMiddle
    };
    typedef NSUInteger NSLineBreakMode
    
    
    typedef enum {
       UILineBreakModeWordWrap = 0,
       UILineBreakModeCharacterWrap,
       UILineBreakModeClip,
       UILineBreakModeHeadTruncation,
       UILineBreakModeTailTruncation,
       UILineBreakModeMiddleTruncation,
    } UILineBreakMode;