Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 2 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
3 STTweetLabel/STTweetLabel.h
@@ -16,15 +16,14 @@ typedef enum {
@property (nonatomic, strong) NSArray *validProtocols;
@property (nonatomic, assign) BOOL leftToRight;
-@property (nonatomic, assign) BOOL textSelectable;
-@property (nonatomic, strong) UIColor *selectionColor;
@property (nonatomic, copy) void (^detectionBlock)(STTweetHotWord hotWord, NSString *string, NSString *protocol, NSRange range);
- (void)setAttributes:(NSDictionary *)attributes;
- (void)setAttributes:(NSDictionary *)attributes hotWord:(STTweetHotWord)hotWord;
- (NSDictionary *)attributes;
- (NSDictionary *)attributesForHotWord:(STTweetHotWord)hotWord;
+- (void)setHighlightedAttributes:(NSDictionary *)attributes hotWord:(STTweetHotWord)hotWord;
- (CGSize)suggestedFrameSizeToFitEntireStringConstraintedToWidth:(CGFloat)width;
View
175 STTweetLabel/STTweetLabel.m
@@ -29,6 +29,11 @@ @interface STTweetLabel () <UITextViewDelegate>
@property (nonatomic, strong) NSDictionary *attributesHashtag;
@property (nonatomic, strong) NSDictionary *attributesLink;
+@property (nonatomic, strong) NSDictionary *highlightedAttributesLink;
+@property (nonatomic, strong) NSDictionary *highlightedAttributesHandle;
+@property (nonatomic, strong) NSDictionary *highlightedAttributesHashtag;
+@property (nonatomic, assign) id highlightedWord;
+
@property (strong) UITextView *textView;
- (void)setupLabel;
@@ -40,7 +45,6 @@ - (void)updateText;
@implementation STTweetLabel {
BOOL _isTouchesMoved;
- NSRange _selectableRange;
int _firstCharIndex;
CGPoint _firstTouchLocation;
}
@@ -64,26 +68,6 @@ - (void)awakeFromNib {
}
#pragma mark -
-#pragma mark Responder
-
-- (BOOL)canBecomeFirstResponder {
- return YES;
-}
-
-- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
- return (action == @selector(copy:));
-}
-
-- (void)copy:(id)sender {
- [[UIPasteboard generalPasteboard] setString:[_cleanText substringWithRange:_selectableRange]];
-
- @try {
- [_textStorage removeAttribute:NSBackgroundColorAttributeName range:_selectableRange];
- } @catch (NSException *exception) {
- }
-}
-
-#pragma mark -
#pragma mark Setup
- (void)setupLabel {
@@ -94,13 +78,14 @@ - (void)setupLabel {
[self setNumberOfLines:0];
_leftToRight = YES;
- _textSelectable = YES;
- _selectionColor = [UIColor colorWithWhite:0.9 alpha:1.0];
_attributesText = @{NSForegroundColorAttributeName: self.textColor, NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0]};
_attributesHandle = @{NSForegroundColorAttributeName: [UIColor redColor], NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0]};
_attributesHashtag = @{NSForegroundColorAttributeName: [[UIColor alloc] initWithWhite:170.0/255.0 alpha:1.0], NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0]};
_attributesLink = @{NSForegroundColorAttributeName: [[UIColor alloc] initWithRed:129.0/255.0 green:171.0/255.0 blue:193.0/255.0 alpha:1.0], NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0]};
+ _highlightedAttributesHandle = @{};
+ _highlightedAttributesHashtag = @{};
+ _highlightedAttributesLink = @{};
self.validProtocols = @[@"http", @"https"];
}
@@ -305,6 +290,32 @@ - (void)setAttributes:(NSDictionary *)attributes hotWord:(STTweetHotWord)hotWord
}
}
+- (void)setHighlightedAttributes:(NSDictionary *)attributes hotWord:(STTweetHotWord)hotWord {
+ if (!attributes[NSFontAttributeName]) {
+ NSMutableDictionary *copy = [attributes mutableCopy];
+ copy[NSFontAttributeName] = self.font;
+ attributes = [NSDictionary dictionaryWithDictionary:copy];
+ }
+
+ if (!attributes[NSForegroundColorAttributeName]) {
+ NSMutableDictionary *copy = [attributes mutableCopy];
+ copy[NSForegroundColorAttributeName] = self.textColor;
+ attributes = [NSDictionary dictionaryWithDictionary:copy];
+ }
+
+ switch (hotWord) {
+ case STTweetHandle:
+ _highlightedAttributesHandle = attributes;
+ break;
+ case STTweetHashtag:
+ _highlightedAttributesHashtag = attributes;
+ break;
+ case STTweetLink:
+ _highlightedAttributesLink = attributes;
+ break;
+ }
+}
+
- (void)setLeftToRight:(BOOL)leftToRight {
_leftToRight = leftToRight;
@@ -360,89 +371,89 @@ - (NSDictionary *)attributesForHotWord:(STTweetHotWord)hotWord {
}
}
+- (NSDictionary *)highlightedAttributesForHotWord:(STTweetHotWord)hotWord {
+ switch (hotWord) {
+ case STTweetHandle:
+ return _highlightedAttributesHandle;
+ break;
+ case STTweetHashtag:
+ return _highlightedAttributesHashtag;
+ break;
+ case STTweetLink:
+ return _highlightedAttributesLink;
+ break;
+ }
+}
+
- (BOOL)isLeftToRight {
return _leftToRight;
}
#pragma mark -
#pragma mark Retrieve word after touch event
+- (void)clearHighlightedWord {
+ if (_highlightedWord) {
+ NSRange range = [[_highlightedWord objectForKey:@"range"] rangeValue];
+ [_textStorage removeAttributes:[self highlightedAttributesForHotWord:(STTweetHotWord)[[_highlightedWord objectForKey:@"hotWord"] integerValue]]
+ range:range];
+ [_textStorage addAttributes:[self attributesForHotWord:(STTweetHotWord)[[_highlightedWord objectForKey:@"hotWord"] integerValue]]
+ range:range];
+ self.highlightedWord = nil;
+ }
+}
+
+- (void)highlightWord {
+ if (_highlightedWord) {
+ NSRange range = [[_highlightedWord objectForKey:@"range"] rangeValue];
+ [_textStorage removeAttributes:[self attributesForHotWord:(STTweetHotWord)[[_highlightedWord objectForKey:@"hotWord"] integerValue]]
+ range:range];
+ [_textStorage addAttributes:[self highlightedAttributesForHotWord:(STTweetHotWord)[[_highlightedWord objectForKey:@"hotWord"] integerValue]]
+ range:range];
+ }
+}
+
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
-
- _isTouchesMoved = NO;
-
- @try {
- [_textStorage removeAttribute:NSBackgroundColorAttributeName range:_selectableRange];
- } @catch (NSException *exception) {
- }
-
- _selectableRange = NSMakeRange(0, 0);
- _firstTouchLocation = [[touches anyObject] locationInView:_textView];
+ [self clearHighlightedWord];
+ int charIndex = (int)[self charIndexAtLocation:[[touches anyObject] locationInView:_textView]];
+ __block typeof(self) blockSelf = self;
+ [_rangesOfHotWords enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
+ NSRange range = [[obj objectForKey:@"range"] rangeValue];
+ if (NSLocationInRange(charIndex, range)) {
+ blockSelf.highlightedWord = obj;
+ [self highlightWord];
+ *stop = YES;
+ }
+ }];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
-
- if (!_textSelectable) {
- UIMenuController *menuController = [UIMenuController sharedMenuController];
- [menuController setMenuVisible:NO animated:YES];
-
- return;
- }
-
- _isTouchesMoved = YES;
-
- int charIndex = (int)[self charIndexAtLocation:[[touches anyObject] locationInView:_textView]];
-
- @try {
- [_textStorage removeAttribute:NSBackgroundColorAttributeName range:_selectableRange];
- } @catch (NSException *exception) {
- }
-
- if (_selectableRange.length == 0) {
- _selectableRange = NSMakeRange(charIndex, 1);
- _firstCharIndex = charIndex;
- } else if (charIndex > _firstCharIndex) {
- _selectableRange = NSMakeRange(_firstCharIndex, charIndex - _firstCharIndex + 1);
- } else if (charIndex < _firstCharIndex) {
- _firstTouchLocation = [[touches anyObject] locationInView:_textView];
-
- _selectableRange = NSMakeRange(charIndex, _firstCharIndex - charIndex);
- }
-
- @try {
- [_textStorage addAttribute:NSBackgroundColorAttributeName value:_selectionColor range:_selectableRange];
- } @catch (NSException *exception) {
+ if (_highlightedWord) {
+ int charIndex = (int)[self charIndexAtLocation:[[touches anyObject] locationInView:_textView]];
+ NSRange range = [[_highlightedWord objectForKey:@"range"] rangeValue];
+ if (!NSLocationInRange(charIndex, range)) {
+ [self clearHighlightedWord];
+ }
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
-
CGPoint touchLocation = [[touches anyObject] locationInView:self];
-
- if (_isTouchesMoved) {
- UIMenuController *menuController = [UIMenuController sharedMenuController];
- [menuController setTargetRect:CGRectMake(_firstTouchLocation.x, _firstTouchLocation.y, 1.0, 1.0) inView:self];
- [menuController setMenuVisible:YES animated:YES];
-
- [self becomeFirstResponder];
-
- return;
- }
-
- if (!CGRectContainsPoint(_textView.frame, touchLocation))
- return;
+ if (!CGRectContainsPoint(_textView.frame, touchLocation)) return;
int charIndex = (int)[self charIndexAtLocation:[[touches anyObject] locationInView:_textView]];
[_rangesOfHotWords enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSRange range = [[obj objectForKey:@"range"] rangeValue];
-
- if (charIndex >= range.location && charIndex < range.location + range.length) {
- _detectionBlock((STTweetHotWord)[[obj objectForKey:@"hotWord"] intValue], [_cleanText substringWithRange:range], [obj objectForKey:@"protocol"], range);
-
+ if (NSLocationInRange(charIndex, range)) {
+ [self clearHighlightedWord];
+ _detectionBlock((STTweetHotWord)[[obj objectForKey:@"hotWord"] intValue],
+ [_cleanText substringWithRange:range],
+ [obj objectForKey:@"protocol"],
+ range);
*stop = YES;
}
}];
View
1 STTweetLabel/STTweetTextStorage.h
@@ -14,5 +14,6 @@
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
+- (void)removeAttributes:(NSDictionary*)attributes range:(NSRange)range;
@end
View
7 STTweetLabel/STTweetTextStorage.m
@@ -55,6 +55,13 @@ - (void)removeAttribute:(NSString *)name range:(NSRange)range {
[self endEditing];
}
+- (void)removeAttributes:(NSDictionary*)attributes range:(NSRange)range {
+ __block typeof(self) blockSelf = self;
+ [attributes enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
+ [blockSelf removeAttribute:key range:range];
+ }];
+}
+
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)string {
[self beginEditing];
[_backingStore replaceCharactersInRange:range withString:string];
View
14 ...eetLabelExample.xcodeproj/project.xcworkspace/xcshareddata/STTweetLabelExample.xccheckout
@@ -5,34 +5,34 @@
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
- <string>07C12E3B-50A0-45B9-AA7F-923C04908480</string>
+ <string>D000AC98-092C-4EA5-A0DD-BFACE75BE8F7</string>
<key>IDESourceControlProjectName</key>
<string>STTweetLabelExample</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
- <key>D2B5469B-FFE8-44F3-9BAD-28F0412E4E1F</key>
- <string>ssh://github.com/SebastienThiebaud/STTweetLabel.git</string>
+ <key>093123D7-6A52-4172-A7A0-C2A905951119</key>
+ <string>ssh://github.com/cruffenach/STTweetLabel.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>STTweetLabelExample/STTweetLabelExample.xcodeproj/project.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
- <key>D2B5469B-FFE8-44F3-9BAD-28F0412E4E1F</key>
+ <key>093123D7-6A52-4172-A7A0-C2A905951119</key>
<string>../../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
- <string>ssh://github.com/SebastienThiebaud/STTweetLabel.git</string>
+ <string>ssh://github.com/cruffenach/STTweetLabel.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>110</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
- <string>D2B5469B-FFE8-44F3-9BAD-28F0412E4E1F</string>
+ <string>093123D7-6A52-4172-A7A0-C2A905951119</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
- <string>D2B5469B-FFE8-44F3-9BAD-28F0412E4E1F</string>
+ <string>093123D7-6A52-4172-A7A0-C2A905951119</string>
<key>IDESourceControlWCCName</key>
<string>STTweetLabel</string>
</dict>
View
2 STTweetLabelExample/STTweetLabelExample/STViewController.m
@@ -31,6 +31,8 @@ - (void)viewDidLoad {
tweetLabel.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:tweetLabel];
+ [tweetLabel setHighlightedAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} hotWord:STTweetLink];
+
CGSize size = [tweetLabel suggestedFrameSizeToFitEntireStringConstraintedToWidth:tweetLabel.frame.size.width];
CGRect frame = tweetLabel.frame;
frame.size.height = size.height;

No commit comments for this range

Something went wrong with that request. Please try again.