Skip to content

Commit

Permalink
[Issue TTTAttributedLabel#303] Implementing tintColorDidChange by add…
Browse files Browse the repository at this point in the history
…ing inactiveLinkAttributes @Property

Signed-off-by: Mattt Thompson <[email protected]>
  • Loading branch information
getaaron authored and mattt committed Jan 10, 2014
1 parent cb60511 commit be4bdb6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions TTTAttributedLabel/TTTAttributedLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ extern NSString * const kTTTBackgroundCornerRadiusAttributeName;
*/
@property (nonatomic, strong) NSDictionary *activeLinkAttributes;

/**
A dictionary containing the `NSAttributedString` attributes to be applied to links when they are in the inactive state, which is triggered a change in `tintColor` in iOS 7. Supply `nil` or an empty dictionary to opt out of inactive link styling. The default inactive link style is gray and unadorned.
*/
@property (nonatomic, strong) NSDictionary *inactiveLinkAttributes;

///---------------------------------------
/// @name Acccessing Text Style Attributes
///---------------------------------------
Expand Down
37 changes: 36 additions & 1 deletion TTTAttributedLabel/TTTAttributedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,24 @@ - (void)commonInit {
NSMutableDictionary *mutableActiveLinkAttributes = [NSMutableDictionary dictionary];
[mutableActiveLinkAttributes setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];

NSMutableDictionary *mutableInactiveLinkAttributes = [NSMutableDictionary dictionary];
[mutableInactiveLinkAttributes setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];

if ([NSMutableParagraphStyle class]) {
[mutableLinkAttributes setObject:[UIColor blueColor] forKey:(NSString *)kCTForegroundColorAttributeName];
[mutableActiveLinkAttributes setObject:[UIColor redColor] forKey:(NSString *)kCTForegroundColorAttributeName];
[mutableInactiveLinkAttributes setObject:[UIColor grayColor] forKey:(NSString *)kCTForegroundColorAttributeName];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

[mutableLinkAttributes setObject:paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
[mutableActiveLinkAttributes setObject:paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
[mutableInactiveLinkAttributes setObject:paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
} else {
[mutableLinkAttributes setObject:(__bridge id)[[UIColor blueColor] CGColor] forKey:(NSString *)kCTForegroundColorAttributeName];
[mutableActiveLinkAttributes setObject:(__bridge id)[[UIColor redColor] CGColor] forKey:(NSString *)kCTForegroundColorAttributeName];
[mutableInactiveLinkAttributes setObject:(__bridge id)[[UIColor grayColor] CGColor] forKey:(NSString *)kCTForegroundColorAttributeName];

CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping;
CTParagraphStyleSetting paragraphStyles[1] = {
Expand All @@ -370,12 +376,14 @@ - (void)commonInit {

[mutableLinkAttributes setObject:(__bridge id)paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];
[mutableActiveLinkAttributes setObject:(__bridge id)paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];

[mutableInactiveLinkAttributes setObject:(__bridge id)paragraphStyle forKey:(NSString *)kCTParagraphStyleAttributeName];

CFRelease(paragraphStyle);
}

self.linkAttributes = [NSDictionary dictionaryWithDictionary:mutableLinkAttributes];
self.activeLinkAttributes = [NSDictionary dictionaryWithDictionary:mutableActiveLinkAttributes];
self.inactiveLinkAttributes = [NSDictionary dictionaryWithDictionary:mutableInactiveLinkAttributes];
}

- (void)dealloc {
Expand Down Expand Up @@ -516,6 +524,7 @@ - (void)addLinksWithTextCheckingResults:(NSArray *)results
for (NSTextCheckingResult *result in results) {
[mutableAttributedString addAttributes:attributes range:result.range];
}

self.attributedText = mutableAttributedString;
[self setNeedsDisplay];
}
Expand Down Expand Up @@ -1173,6 +1182,27 @@ - (CGSize)intrinsicContentSize {
return [self sizeThatFits:[super intrinsicContentSize]];
}

- (void)tintColorDidChange {
BOOL isInactive = (CGColorSpaceGetModel(CGColorGetColorSpace([self.tintColor CGColor])) == kCGColorSpaceModelMonochrome);

NSDictionary *attributesToRemove = isInactive ? self.linkAttributes : self.inactiveLinkAttributes;
NSDictionary *attributesToAdd = isInactive ? self.inactiveLinkAttributes : self.linkAttributes;

NSMutableAttributedString *mutableAttributedString = [self.attributedText mutableCopy];
for (NSTextCheckingResult *result in self.links) {
[attributesToRemove enumerateKeysAndObjectsUsingBlock:^(NSString *name, __unused id value, __unused BOOL *stop) {
[mutableAttributedString removeAttribute:name range:result.range];
}];

if (attributesToAdd) {
[mutableAttributedString addAttributes:attributesToAdd range:result.range];
}
}

self.attributedText = mutableAttributedString;
[self setNeedsDisplay];
}

#pragma mark - UIResponder

- (BOOL)canBecomeFirstResponder {
Expand Down Expand Up @@ -1290,6 +1320,7 @@ - (void)encodeWithCoder:(NSCoder *)coder {
if ([NSMutableParagraphStyle class]) {
[coder encodeObject:self.linkAttributes forKey:NSStringFromSelector(@selector(linkAttributes))];
[coder encodeObject:self.activeLinkAttributes forKey:NSStringFromSelector(@selector(activeLinkAttributes))];
[coder encodeObject:self.inactiveLinkAttributes forKey:NSStringFromSelector(@selector(inactiveLinkAttributes))];
}
[coder encodeObject:@(self.shadowRadius) forKey:NSStringFromSelector(@selector(shadowRadius))];
[coder encodeObject:@(self.highlightedShadowRadius) forKey:NSStringFromSelector(@selector(highlightedShadowRadius))];
Expand Down Expand Up @@ -1328,6 +1359,10 @@ - (id)initWithCoder:(NSCoder *)coder {
if ([coder containsValueForKey:NSStringFromSelector(@selector(activeLinkAttributes))]) {
self.activeLinkAttributes = [coder decodeObjectForKey:NSStringFromSelector(@selector(activeLinkAttributes))];
}

if ([coder containsValueForKey:NSStringFromSelector(@selector(inactiveLinkAttributes))]) {
self.activeLinkAttributes = [coder decodeObjectForKey:NSStringFromSelector(@selector(inactiveLinkAttributes))];
}
}

if ([coder containsValueForKey:NSStringFromSelector(@selector(shadowRadius))]) {
Expand Down

0 comments on commit be4bdb6

Please sign in to comment.