Skip to content
This repository has been archived by the owner on Feb 7, 2020. It is now read-only.

determine emails #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion STTweetLabel/STTweetLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
typedef NS_ENUM(NSInteger, STTweetHotWord) {
STTweetHandle = 0,
STTweetHashtag,
STTweetLink
STTweetLink,
STTweetEmail,
};

@interface STTweetLabel : UILabel
Expand Down
28 changes: 23 additions & 5 deletions STTweetLabel/STTweetLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#import "STTweetLabel.h"

#define STURLRegex @"(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))"
#define STEmailRegex @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"

@interface STTweetLabel () <UITextViewDelegate>

@property (nonatomic, strong) NSRegularExpression *urlRegex;
@property (nonatomic, strong) NSRegularExpression *emailRegex;

@property (strong) NSTextStorage *textStorage;
@property (strong) NSLayoutManager *layoutManager;
Expand All @@ -27,6 +29,7 @@ @interface STTweetLabel () <UITextViewDelegate>
@property (nonatomic, strong) NSDictionary *attributesHandle;
@property (nonatomic, strong) NSDictionary *attributesHashtag;
@property (nonatomic, strong) NSDictionary *attributesLink;
@property (nonatomic, strong) NSDictionary *attributesEmail;

@property (strong) UITextView *textView;

Expand All @@ -48,6 +51,7 @@ - (id)initWithFrame:(CGRect)frame {
[self setupLabel];
[self setupTextView];
[self setupURLRegularExpression];
[self setupEmailRegularExpression];
}

return self;
Expand All @@ -60,6 +64,7 @@ - (id)initWithCoder:(NSCoder *)coder {
[self setupLabel];
[self setupTextView];
[self setupURLRegularExpression];
[self setupEmailRegularExpression];
}

return self;
Expand All @@ -86,11 +91,15 @@ - (void)setupTextView {
}

- (void)setupURLRegularExpression {

NSError *regexError = nil;
self.urlRegex = [NSRegularExpression regularExpressionWithPattern:STURLRegex options:0 error:&regexError];
}

- (void)setupEmailRegularExpression {
NSError *regexError = nil;
self.emailRegex = [NSRegularExpression regularExpressionWithPattern:STEmailRegex options:0 error:&regexError];
}

#pragma mark - Responder

- (BOOL)canBecomeFirstResponder {
Expand Down Expand Up @@ -129,6 +138,7 @@ - (void)setupLabel {
_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]};
_attributesEmail = @{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]};

self.validProtocols = @[@"http", @"https"];
}
Expand Down Expand Up @@ -203,11 +213,9 @@ - (void)determineHotWords {
}

- (void)determineLinks {
NSMutableString *tmpText = [[NSMutableString alloc] initWithString:_cleanText];

[self.urlRegex enumerateMatchesInString:tmpText options:0 range:NSMakeRange(0, tmpText.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
[self.urlRegex enumerateMatchesInString:_cleanText options:0 range:NSMakeRange(0, _cleanText.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSString *protocol = @"http";
NSString *link = [tmpText substringWithRange:result.range];
NSString *link = [_cleanText substringWithRange:result.range];
NSRange protocolRange = [link rangeOfString:@":"];
if (protocolRange.location != NSNotFound) {
protocol = [link substringToIndex:protocolRange.location];
Expand All @@ -220,6 +228,10 @@ - (void)determineLinks {
}];
}
}];

[self.emailRegex enumerateMatchesInString:_cleanText options:0 range:NSMakeRange(0, _cleanText.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
[_rangesOfHotWords addObject:@{@"hotWord": @(STTweetEmail), @"range": [NSValue valueWithRange:result.range]}];
}];
}

- (void)updateText {
Expand Down Expand Up @@ -319,6 +331,9 @@ - (void)setAttributes:(NSDictionary *)attributes hotWord:(STTweetHotWord)hotWord
case STTweetLink:
_attributesLink = attributes;
break;
case STTweetEmail:
_attributesEmail = attributes;
break;
default:
break;
}
Expand Down Expand Up @@ -373,6 +388,9 @@ - (NSDictionary *)attributesForHotWord:(STTweetHotWord)hotWord {
case STTweetLink:
return _attributesLink;

case STTweetEmail:
return _attributesEmail;

default:
break;
}
Expand Down