Skip to content

Instantly share code, notes, and snippets.

@MikeSilvis
Created December 11, 2014 04:31
Show Gist options
  • Save MikeSilvis/534dc284dd9bb6ac24ea to your computer and use it in GitHub Desktop.
Save MikeSilvis/534dc284dd9bb6ac24ea to your computer and use it in GitHub Desktop.
//
// SCSocialTextView.m
// Socialcast
//
// Created by Mike Silvis on 12/10/14.
// Copyright (c) 2014 Socialcast. All rights reserved.
//
#import "SCSocialTextView.h"
#import "SCColorScheme.h"
#import "SCUser.h"
#import "SCGroup.h"
#import "SCHashtag.h"
#import "SCUIPartEventTable.h"
#import "SCHyperLink.h"
@interface SCSocialTextView ()
@property (nonatomic, strong) NSMutableArray *rangesOfHotWords;
@end
@implementation SCSocialTextView
-(void)setMessage:(SCStreamMessage *)message {
_message = message;
self.recipients = message.recipients;
self.mentions = message.mentions;
self.hashtags = message.hashtags;
}
-(void)setText:(NSString *)text {
self.rangesOfHotWords = [NSMutableArray array];
text = [self updatedTextForMentionName:text objects:self.recipients];
text = [self updatedTextForMentionName:text objects:self.mentions];
[super setText:text];
self.attributedText = [self socialAttributedText];
}
-(NSString *)updatedTextForMentionName:(NSString *)text objects:(NSArray *)objects {
for (SCMentionable *object in objects) {
if (object.mentionName && object.username) {
text = [text stringByReplacingOccurrencesOfString:object.mentionName
withString:object.username
options:NSCaseInsensitiveSearch
range:NSMakeRange(0, text.length)];
}
}
return text;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSUInteger charIndex = [self charIndexAtLocation:[[touches anyObject] locationInView:self]];
for (id obj in self.rangesOfHotWords) {
NSRange range = [obj[@"range"] rangeValue];
if (charIndex >= range.location && charIndex < (range.location + range.length)) {
if (self.socialDelegate && [self.socialDelegate respondsToSelector:@selector(scSocialTextView:linkDidTouch:)]) {
[self.socialDelegate scSocialTextView:self linkDidTouch:obj[@"link"]];
}
return;
}
}
if (self.socialDelegate && [self.socialDelegate respondsToSelector:@selector(scSocialTextViewBackgroundDidTouch:)]) {
[self.socialDelegate scSocialTextViewBackgroundDidTouch:self];
}
[super touchesEnded:touches withEvent:event];
}
-(NSUInteger)charIndexAtLocation:(CGPoint)touchLocation {
NSUInteger glyphIndex = [self.layoutManager glyphIndexForPoint:touchLocation inTextContainer:self.textContainer];
CGRect boundingRect = [self.layoutManager boundingRectForGlyphRange:NSMakeRange(glyphIndex, 1) inTextContainer:self.textContainer];
if (CGRectContainsPoint(boundingRect, touchLocation)) {
return [self.layoutManager characterIndexForGlyphAtIndex:glyphIndex];
}
else {
return -1;
}
}
-(NSMutableAttributedString *)socialAttributedText {
NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:self.text attributes:[SCColorScheme defaultScheme].defaultTextAttributes];
// Users
attrText = [self colorSocialElements:attrText objects:self.mentions keyword:@"@" color:[SCColorScheme defaultScheme].urlColor linkType:kSCHyperLinkMentionType];
// Groups
attrText = [self colorSocialElements:attrText objects:self.recipients keyword:@"@" color:[SCColorScheme defaultScheme].urlColor linkType:kSCHyperLinkMentionType];
// Hashtags
attrText = [self colorSocialElements:attrText objects:self.hashtags keyword:@"#" color:[SCColorScheme defaultScheme].hashtagColor linkType:kSCHyperLinkHashtagType];
// Read more link
attrText = [self colorReadMore:attrText];
return attrText;
}
-(NSMutableAttributedString *)colorSocialElements:(NSMutableAttributedString *)attrString objects:(NSArray *)objects keyword:(NSString *)keyword color:(UIColor *)color linkType:(SCHyperLinkTypes)linkType {
NSError *error;
for (SCMentionable *object in objects) {
NSString *stringToFind = [NSString stringWithFormat:@"%@%@", keyword, object.username];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:stringToFind options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regex matchesInString:self.text options:0 range:NSMakeRange(0, self.text.length)];
for (NSTextCheckingResult *match in matches) {
SCHyperLink *link = [[SCHyperLink alloc] init];
link.type = linkType;
link.text = object.username;
[self.rangesOfHotWords addObject:@{
@"range": [NSValue valueWithRange:match.range],
@"link": link
}
];
[attrString addAttribute:NSForegroundColorAttributeName value:color range:match.range];
}
}
return attrString;
}
-(NSMutableAttributedString *)colorReadMore:(NSMutableAttributedString *)attrString {
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"read more" options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regex matchesInString:self.text options:0 range:NSMakeRange(0, self.text.length)];
for (NSTextCheckingResult *match in matches) {
[attrString addAttribute:NSForegroundColorAttributeName value:[SCColorScheme defaultScheme].urlColor range:match.range];
}
return attrString;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment