Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for deprecated UITextAttributes in iOS 7 #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions NUI/Core/NUIUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
84 changes: 77 additions & 7 deletions NUI/Core/NUIUtilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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