Skip to content
This repository was archived by the owner on Feb 7, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions STTweetLabel/STTweetLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef NS_ENUM(NSInteger, STTweetHotWord) {
@property (nonatomic, assign) BOOL textSelectable;
@property (nonatomic, strong) UIColor *selectionColor;
@property (nonatomic, copy) void (^detectionBlock)(STTweetHotWord hotWord, NSString *string, NSString *protocol, NSRange range);
@property (nonatomic, copy) NSCharacterSet *wordCharacterSet;

- (void)setAttributes:(NSDictionary *)attributes;
- (void)setAttributes:(NSDictionary *)attributes hotWord:(STTweetHotWord)hotWord;
Expand Down
19 changes: 12 additions & 7 deletions STTweetLabel/STTweetLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ - (void)copy:(id)sender {
#pragma mark - Setup

- (void)setupLabel {
// Define a character set for the complete world (determine the end of the hot word)
NSMutableCharacterSet *wordCharactersSet = [NSMutableCharacterSet alphanumericCharacterSet];
[wordCharactersSet removeCharactersInString:@"!@#$%^&*()-={[]}|;:',<>.?/"];
[wordCharactersSet addCharactersInString:@"_"];
_wordCharacterSet = wordCharactersSet;

// Set the basic properties
[self setBackgroundColor:[UIColor clearColor]];
Expand Down Expand Up @@ -153,11 +158,6 @@ - (void)determineHotWords {
NSString *hotCharacters = @"@#";
NSCharacterSet *hotCharactersSet = [NSCharacterSet characterSetWithCharactersInString:hotCharacters];

// Define a character set for the complete world (determine the end of the hot word)
NSMutableCharacterSet *validCharactersSet = [NSMutableCharacterSet alphanumericCharacterSet];
[validCharactersSet removeCharactersInString:@"!@#$%^&*()-={[]}|;:',<>.?/"];
[validCharactersSet addCharactersInString:@"_"];

_rangesOfHotWords = [[NSMutableArray alloc] init];

while ([tmpText rangeOfCharacterFromSet:hotCharactersSet].location < tmpText.length) {
Expand All @@ -178,14 +178,14 @@ - (void)determineHotWords {

[tmpText replaceCharactersInRange:range withString:@"%"];
// If the hot character is not preceded by a alphanumeric characater, ie email (sebastien@world.com)
if (range.location > 0 && [validCharactersSet characterIsMember:[tmpText characterAtIndex:range.location - 1]])
if (range.location > 0 && [self.wordCharacterSet characterIsMember:[tmpText characterAtIndex:range.location - 1]])
continue;

// Determine the length of the hot word
int length = (int)range.length;

while (range.location + length < tmpText.length) {
BOOL charIsMember = [validCharactersSet characterIsMember:[tmpText characterAtIndex:range.location + length]];
BOOL charIsMember = [self.wordCharacterSet characterIsMember:[tmpText characterAtIndex:range.location + length]];

if (charIsMember)
length++;
Expand Down Expand Up @@ -352,6 +352,11 @@ - (void)setAttributedText:(NSAttributedString *)attributedText {
self.text = _cleanAttributedText.string;
}

- (void)setWordCharacterSet:(NSCharacterSet *)characterSet {
_wordCharacterSet = characterSet;
[self determineHotWords];
}

#pragma mark - Getters

- (NSString *)text {
Expand Down