From 8c353b18882e5414eebeb484868cb68f2cf2db95 Mon Sep 17 00:00:00 2001 From: griff Date: Thu, 8 Oct 2015 22:20:13 +0100 Subject: [PATCH 1/3] customizable word character set --- STTweetLabel/STTweetLabel.m | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/STTweetLabel/STTweetLabel.m b/STTweetLabel/STTweetLabel.m index 58be61d..22ecba7 100644 --- a/STTweetLabel/STTweetLabel.m +++ b/STTweetLabel/STTweetLabel.m @@ -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]]; @@ -153,11 +158,8 @@ - (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) { @@ -178,14 +180,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++; From bca79704af12a1303cc096acb69374d490e1a68e Mon Sep 17 00:00:00 2001 From: griff Date: Thu, 8 Oct 2015 22:36:54 +0100 Subject: [PATCH 2/3] call determineHotWords on wordCharacterSet update --- STTweetLabel/STTweetLabel.m | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/STTweetLabel/STTweetLabel.m b/STTweetLabel/STTweetLabel.m index 22ecba7..4651d11 100644 --- a/STTweetLabel/STTweetLabel.m +++ b/STTweetLabel/STTweetLabel.m @@ -158,8 +158,6 @@ - (void)determineHotWords { NSString *hotCharacters = @"@#"; NSCharacterSet *hotCharactersSet = [NSCharacterSet characterSetWithCharactersInString:hotCharacters]; - - _rangesOfHotWords = [[NSMutableArray alloc] init]; while ([tmpText rangeOfCharacterFromSet:hotCharactersSet].location < tmpText.length) { @@ -354,6 +352,11 @@ - (void)setAttributedText:(NSAttributedString *)attributedText { self.text = _cleanAttributedText.string; } +- (void)setWordCharacterSet:(NSCharacterSet *)characterSet { + _wordCharacterSet = characterSet; + [self determineHotWords]; +} + #pragma mark - Getters - (NSString *)text { From 966cae6c8547c0f81e5e9e27adf5a0cd7f90fc9c Mon Sep 17 00:00:00 2001 From: griff Date: Thu, 8 Oct 2015 22:45:38 +0100 Subject: [PATCH 3/3] added wordCharacterSet to header file --- STTweetLabel/STTweetLabel.h | 1 + 1 file changed, 1 insertion(+) diff --git a/STTweetLabel/STTweetLabel.h b/STTweetLabel/STTweetLabel.h index 6bc1be8..2abe949 100644 --- a/STTweetLabel/STTweetLabel.h +++ b/STTweetLabel/STTweetLabel.h @@ -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;