Merge pull request #3 from lukedixon/master

Fixed LinkTextTouchAttributes Bug
This commit is contained in:
Claus Höfele
2014-05-15 20:27:26 +02:00
3 changed files with 32 additions and 8 deletions

View File

@@ -84,6 +84,8 @@
[self.longPressGestureRecognizer requireGestureRecognizerToFail:linkTextView.linkGestureRecognizer];
linkTextView.linkDelegate = self;
linkTextView.linkTextAttributes = @{NSForegroundColorAttributeName : [UIColor blueColor]};
linkTextView.linkTextTouchAttributes = @{NSBackgroundColorAttributeName : [UIColor whiteColor], NSForegroundColorAttributeName : [UIColor orangeColor]};
}
- (void)linkTextView:(CCHLinkTextView *)linkTextView didTapLinkWithValue:(id)value

View File

@@ -38,7 +38,7 @@ extern NSString *const CCHLinkAttributeName;
@property (nonatomic, weak) id<CCHLinkTextViewDelegate> linkDelegate;
/** `NSAttributedString` attributes applied to links. */
@property(nonatomic, copy) NSDictionary *linkTextAttributes NS_AVAILABLE_IOS(7_0);
@property (nonatomic, copy) NSDictionary *linkTextAttributes NS_AVAILABLE_IOS(7_0);
/** `NSAttributedString` attributes applied to links when touched. */
@property (nonatomic, copy) NSDictionary *linkTextTouchAttributes;

View File

@@ -109,14 +109,33 @@ NSString *const CCHLinkAttributeName = @"CCHLinkAttributeName";
return image;
}
- (void)setLinkTextAttributes:(NSDictionary *)linkTextAttributes
{
super.linkTextAttributes = linkTextAttributes;
[self setAttributedText:[self attributedText] skipLinkTextAttributes:NO];
}
- (void)setLinkTextTouchAttributes:(NSDictionary *)linkTextTouchAttributes
{
_linkTextTouchAttributes = linkTextTouchAttributes;
[self setAttributedText:[self attributedText] skipLinkTextAttributes:NO];
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
[self setAttributedText:attributedText skipLinkTextAttributes:NO];
}
- (void)setAttributedText:(NSAttributedString *)attributedText skipLinkTextAttributes:(BOOL)skipLinkTextAttributes
{
NSMutableAttributedString *mutableAttributedText = [attributedText mutableCopy];
[mutableAttributedText enumerateAttribute:CCHLinkAttributeName inRange:NSMakeRange(0, attributedText.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
[mutableAttributedText addAttributes:self.linkTextAttributes range:range];
}
}];
if (!skipLinkTextAttributes) {
[mutableAttributedText enumerateAttribute:CCHLinkAttributeName inRange:NSMakeRange(0, attributedText.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
[mutableAttributedText addAttributes:self.linkTextAttributes range:range];
}
}];
}
[super setAttributedText:mutableAttributedText];
}
@@ -224,8 +243,11 @@ NSString *const CCHLinkAttributeName = @"CCHLinkAttributeName";
{
[self enumerateLinkRangesContainingLocation:location usingBlock:^(NSRange range) {
NSMutableAttributedString *attributedText = [self.attributedText mutableCopy];
for (NSString *attribute in self.linkTextAttributes) {
[attributedText removeAttribute:attribute range:range];
}
[attributedText addAttributes:self.linkTextTouchAttributes range:range];
self.attributedText = attributedText;
[self setAttributedText:attributedText skipLinkTextAttributes:YES];
}];
}
@@ -237,7 +259,7 @@ NSString *const CCHLinkAttributeName = @"CCHLinkAttributeName";
[attributedText removeAttribute:attribute range:range];
}
[attributedText addAttributes:self.linkTextAttributes range:range];
self.attributedText = attributedText;
[self setAttributedText:attributedText skipLinkTextAttributes:YES];
}];
}