Skip to content

Commit

Permalink
Removing unnecessary -setLinkActive:withTextCheckingResult: method, i…
Browse files Browse the repository at this point in the history
…n favor of simpler override of -setActiveLink:
  • Loading branch information
mattt committed Jan 19, 2013
1 parent 250ddc1 commit 7e7fdb7
Showing 1 changed file with 25 additions and 40 deletions.
65 changes: 25 additions & 40 deletions TTTAttributedLabel/TTTAttributedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -306,33 +306,6 @@ - (NSAttributedString *)renderedAttributedText {

#pragma mark -

- (void)setLinkActive:(BOOL)active
withTextCheckingResult:(NSTextCheckingResult *)result
{
if (result && [self.activeLinkAttributes count] > 0) {
if (active) {
if (!self.inactiveAttributedText) {
self.inactiveAttributedText = self.attributedText;
}

NSMutableAttributedString *mutableAttributedString = [self.inactiveAttributedText mutableCopy];
[mutableAttributedString addAttributes:self.activeLinkAttributes range:result.range];
self.attributedText = mutableAttributedString;

[self setNeedsDisplay];
} else {
if (self.inactiveAttributedText) {
self.attributedText = self.inactiveAttributedText;
self.inactiveAttributedText = nil;

[self setNeedsDisplay];
}
}
}
}

#pragma mark -

- (void)setDataDetectorTypes:(UIDataDetectorTypes)dataDetectorTypes {
[self willChangeValueForKey:@"dataDetectorTypes"];
_dataDetectorTypes = dataDetectorTypes;
Expand Down Expand Up @@ -772,6 +745,27 @@ - (void)setText:(id)text
[self setText:mutableAttributedString];
}

- (void)setActiveLink:(NSTextCheckingResult *)activeLink {
_activeLink = activeLink;

if (_activeLink && [self.activeLinkAttributes count] > 0) {
if (!self.inactiveAttributedText) {
self.inactiveAttributedText = [self.attributedText copy];
}

NSMutableAttributedString *mutableAttributedString = [self.inactiveAttributedText mutableCopy];
[mutableAttributedString addAttributes:self.activeLinkAttributes range:_activeLink.range];
self.attributedText = mutableAttributedString;

[self setNeedsDisplay];
} else if (self.inactiveAttributedText) {
self.attributedText = self.inactiveAttributedText;
self.inactiveAttributedText = nil;

[self setNeedsDisplay];
}
}

#pragma mark - UILabel

- (void)setHighlighted:(BOOL)highlighted {
Expand Down Expand Up @@ -943,9 +937,7 @@ - (void)touchesBegan:(NSSet *)touches

self.activeLink = [self linkAtPoint:[touch locationInView:self]];

if (self.activeLink) {
[self setLinkActive:YES withTextCheckingResult:self.activeLink];
} else {
if (!self.activeLink) {
[super touchesBegan:touches withEvent:event];
}
}
Expand All @@ -957,10 +949,7 @@ - (void)touchesMoved:(NSSet *)touches
UITouch *touch = [touches anyObject];

if (self.activeLink != [self linkAtPoint:[touch locationInView:self]]) {
[self setLinkActive:NO withTextCheckingResult:self.activeLink];
self.activeLink = nil;
} else {
[self setLinkActive:YES withTextCheckingResult:self.activeLink];
}
} else {
[super touchesMoved:touches withEvent:event];
Expand All @@ -971,13 +960,9 @@ - (void)touchesEnded:(NSSet *)touches
withEvent:(UIEvent *)event
{
if (self.activeLink) {
[self setLinkActive:NO withTextCheckingResult:self.activeLink];

if (!self.delegate) {
return;
}

NSTextCheckingResult *result = self.activeLink;
self.activeLink = nil;

switch (result.resultType) {
case NSTextCheckingTypeLink:
if ([self.delegate respondsToSelector:@selector(attributedLabel:didSelectLinkWithURL:)]) {
Expand Down Expand Up @@ -1023,7 +1008,7 @@ - (void)touchesCancelled:(NSSet *)touches
withEvent:(UIEvent *)event
{
if (self.activeLink) {
[self setLinkActive:NO withTextCheckingResult:self.activeLink];
self.activeLink = nil;
} else {
[super touchesCancelled:touches withEvent:event];
}
Expand Down

0 comments on commit 7e7fdb7

Please sign in to comment.