Skip to content

Commit

Permalink
Merge pull request #855 from adamschlag/develop
Browse files Browse the repository at this point in the history
Use correct attribute name for foreground color
  • Loading branch information
odrobnik committed May 22, 2015
2 parents 80654ad + 84a8603 commit 34f527d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 8 deletions.
30 changes: 22 additions & 8 deletions Core/Source/NSMutableAttributedString+HTML.m
Expand Up @@ -171,19 +171,33 @@ - (void)appendEndOfParagraph
}

// transfer foreground color
id foregroundColor = [attributes objectForKey:(id)kCTForegroundColorAttributeName];

if (foregroundColor)
#if DTCORETEXT_SUPPORT_NS_ATTRIBUTES
if (___useiOS6Attributes)
{
#if TARGET_OS_IPHONE
if ([foregroundColor isKindOfClass:[UIColor class]])
id foregroundColor = [attributes objectForKey:NSForegroundColorAttributeName];

if (foregroundColor)
{
[appendAttributes setObject:(id)[foregroundColor CGColor] forKey:(id)kCTForegroundColorAttributeName];
[appendAttributes setObject:foregroundColor forKey:NSForegroundColorAttributeName];
}
else
}
else
#endif
{
id foregroundColor = [attributes objectForKey:(id)kCTForegroundColorAttributeName];

if (foregroundColor)
{
[appendAttributes setObject:foregroundColor forKey:(id)kCTForegroundColorAttributeName];
#if TARGET_OS_IPHONE
if ([foregroundColor isKindOfClass:[UIColor class]])
{
[appendAttributes setObject:(id)[foregroundColor CGColor] forKey:(id)kCTForegroundColorAttributeName];
}
else
#endif
{
[appendAttributes setObject:foregroundColor forKey:(id)kCTForegroundColorAttributeName];
}
}
}

Expand Down
55 changes: 55 additions & 0 deletions Test/Source/NSMutableAttributedStringHTMLTest.m
Expand Up @@ -180,4 +180,59 @@ - (void)testRangeOfCustomHTMLAttribute
XCTAssertTrue(NSEqualRanges(queriedRange, expectedRange), @"Range is incorrect");
}

- (void)testForegroundColorAttributeNameAtEndOfParagraph
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"1234567890"];
NSMutableAttributedString *testingString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];

NSRange entireString = NSMakeRange(0, [attributedString length]);

BOOL defaultValueForUseiOS6Attributes = ___useiOS6Attributes;
___useiOS6Attributes = NO;

// add a foreground color using the Core Text attribute name
CGFloat components[4] = {1.0, 0.0, 0.0, 1.0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef redColorRef = CGColorCreate(colorSpace, components);
[testingString addAttribute:(id)kCTForegroundColorAttributeName value:(id)CFBridgingRelease(redColorRef) range:entireString];

// append the end of a paragraph tag
[testingString appendEndOfParagraph];

// check the foreground color
id stringColorRef = [testingString attribute:(id)kCTForegroundColorAttributeName atIndex:([testingString length] - 1) effectiveRange:nil];
#if TARGET_OS_IPHONE
if ([stringColorRef isKindOfClass:[UIColor class]]) {
stringColorRef = (id)[(UIColor *)stringColorRef CGColor];
}
#endif

XCTAssertTrue(CGColorEqualToColor(redColorRef, (CGColorRef)stringColorRef), @"Foreground color should be red");

CGColorSpaceRelease(colorSpace);

#if TARGET_OS_IPHONE && DTCORETEXT_SUPPORT_NS_ATTRIBUTES
[testingString setAttributedString:attributedString];

___useiOS6Attributes = YES;

// add a foreground color using the NSAttributedString attribute name
[testingString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:entireString];

// append the end of a paragraph tag
[testingString appendEndOfParagraph];

// check the foreground color
id nsAttributesStringColor = [testingString attribute:NSForegroundColorAttributeName atIndex:([testingString length] - 1) effectiveRange:nil];

if (![nsAttributesStringColor isKindOfClass:[UIColor class]]) {
XCTFail(@"Color for NSForegroundColorAttributeName should be set as a valid UIColor" );
} else {
XCTAssertTrue(CGColorEqualToColor([[UIColor redColor] CGColor], [nsAttributesStringColor CGColor]), @"Foreground color should be red");
}
#endif

___useiOS6Attributes = defaultValueForUseiOS6Attributes;
}

@end

0 comments on commit 34f527d

Please sign in to comment.