From 42f9c63165f6a43e34edb4d157b3387487cdf644 Mon Sep 17 00:00:00 2001 From: Conrad Stoll Date: Wed, 18 Sep 2013 13:26:13 -0500 Subject: [PATCH] Adding support for iOS 7's preferred attributed text keys to solve deprecation issues --- NUI/Core/NUIUtilities.h | 10 +++++ NUI/Core/NUIUtilities.m | 84 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 87 insertions(+), 7 deletions(-) diff --git a/NUI/Core/NUIUtilities.h b/NUI/Core/NUIUtilities.h index 871c9e43..ea4cd41b 100644 --- a/NUI/Core/NUIUtilities.h +++ b/NUI/Core/NUIUtilities.h @@ -14,4 +14,14 @@ + (NSDictionary*)titleTextAttributesForClass:(NSString*)className; + (NSDictionary*)titleTextAttributesForClass:(NSString*)className withSuffix:(NSString*) suffix; ++ (NSString *)textAttributeFontKey; ++ (NSString *)textAttributeTextColorKey; + +// Deprecated in iOS 7 ++ (NSString *)textAttributeTextShadowColorKey; ++ (NSString *)textAttributeTextShadowOffsetKey; + +// iOS 7 and later ++ (NSString *)textAttributeShadowKey; + @end diff --git a/NUI/Core/NUIUtilities.m b/NUI/Core/NUIUtilities.m index c1063f2b..a52424cc 100644 --- a/NUI/Core/NUIUtilities.m +++ b/NUI/Core/NUIUtilities.m @@ -27,20 +27,42 @@ + (NSDictionary*)titleTextAttributesForClass:(NSString*)className withSuffix:(NS fontSize = fontSize ? fontSize : [UIFont systemFontSize]; UIFont *font = fontName ? [UIFont fontWithName:fontName size:fontSize] : [UIFont systemFontOfSize:fontSize]; - [titleTextAttributes setObject:font forKey:UITextAttributeFont]; + [titleTextAttributes setObject:font forKey:[self textAttributeFontKey]]; } if ([NUISettings hasProperty:fontColorSelector withClass:className]) { - [titleTextAttributes setObject:[NUISettings getColor:fontColorSelector withClass:className] forKey:UITextAttributeTextColor]; + [titleTextAttributes setObject:[NUISettings getColor:fontColorSelector withClass:className] forKey:[self textAttributeTextColorKey]]; } - if ([NUISettings hasProperty:textShadowColorSelector withClass:className]) { - [titleTextAttributes setObject:[NUISettings getColor:textShadowColorSelector withClass:className] forKey:UITextAttributeTextShadowColor]; + if (OSVersionIsAtLeastiOS7()) { + NSShadow *shadow = [[NSShadow alloc] init]; + BOOL containsShadow = NO; + + if ([NUISettings hasProperty:textShadowColorSelector withClass:className]) { + containsShadow = YES; + shadow.shadowColor = [NUISettings getColor:textShadowColorSelector withClass:className]; + } + + if ([NUISettings hasProperty:textShadowOffsetSelector withClass:className]) { + containsShadow = YES; + UIOffset offset = [NUISettings getOffset:textShadowOffsetSelector withClass:className]; + CGSize shadowOffset = CGSizeMake(offset.horizontal, offset.vertical); + shadow.shadowOffset = shadowOffset; + } + + if (containsShadow) { + [titleTextAttributes setObject:shadow forKey:NSShadowAttributeName]; + } + } else { + if ([NUISettings hasProperty:textShadowColorSelector withClass:className]) { + [titleTextAttributes setObject:[NUISettings getColor:textShadowColorSelector withClass:className] forKey:UITextAttributeTextShadowColor]; + } + + if ([NUISettings hasProperty:textShadowOffsetSelector withClass:className]) { + [titleTextAttributes setObject:[NSValue valueWithUIOffset:[NUISettings getOffset:textShadowOffsetSelector withClass:className]] forKey:UITextAttributeTextShadowOffset]; + } } - if ([NUISettings hasProperty:textShadowOffsetSelector withClass:className]) { - [titleTextAttributes setObject:[NSValue valueWithUIOffset:[NUISettings getOffset:textShadowOffsetSelector withClass:className]] forKey:UITextAttributeTextShadowOffset]; - } return titleTextAttributes; } @@ -58,5 +80,53 @@ + (NSString*)selector:(NSString*)selector withSuffix:(NSString*)suffix return selector; } ++ (NSString *)textAttributeFontKey +{ + if (OSVersionIsAtLeastiOS7()) { + return NSFontAttributeName; + } else { + return UITextAttributeFont; + } +} + ++ (NSString *)textAttributeTextColorKey +{ + if (OSVersionIsAtLeastiOS7()) { + return nil; + } else { + return UITextAttributeTextColor; + } +} + ++ (NSString *)textAttributeTextShadowColorKey +{ + if (OSVersionIsAtLeastiOS7()) { + return nil; + } else { + return UITextAttributeTextShadowColor; + } +} + ++ (NSString *)textAttributeTextShadowOffsetKey +{ + if (OSVersionIsAtLeastiOS7()) { + return nil; + } else { + return UITextAttributeTextShadowOffset; + } +} + ++ (NSString *)textAttributeShadowKey +{ + if (OSVersionIsAtLeastiOS7()) { + return NSShadowAttributeName; + } else { + return nil; + } +} + +static BOOL OSVersionIsAtLeastiOS7() { + return (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1); +} @end