forked from 2359media/STXDynamicTableView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTXCaptionCell.m
More file actions
157 lines (120 loc) · 5.86 KB
/
STXCaptionCell.m
File metadata and controls
157 lines (120 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// STXCaptionCell.m
// STXDynamicTableViewExample
//
// Created by Jesse Armand on 10/4/14.
// Copyright (c) 2014 2359 Media Pte Ltd. All rights reserved.
//
#import "STXCaptionCell.h"
#import "STXAttributedLabel.h"
static CGFloat STXCaptionViewLeadingEdgeInset = 10.f;
static CGFloat STXCaptionViewTrailingEdgeInset = 10.f;
static NSString *HashTagAndMentionRegex = @"(#|@)(\\w+)";
@interface STXCaptionCell () <TTTAttributedLabelDelegate>
@property (strong, nonatomic) STXAttributedLabel *captionLabel;
@property (nonatomic) BOOL didSetupConstraints;
@end
@implementation STXCaptionCell
- (id)initWithCaption:(NSDictionary *)caption reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (self) {
NSString *text = [caption stringValueForComplexKeyPath:@"text"];
_captionLabel = [self captionLabelWithText:text];
[self.contentView addSubview:_captionLabel];
_captionLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self.contentView setNeedsLayout];
[self.contentView layoutIfNeeded];
self.captionLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.frame) - (STXCaptionViewLeadingEdgeInset + STXCaptionViewTrailingEdgeInset);
[super layoutSubviews];
}
- (void)updateConstraints
{
if (!self.didSetupConstraints) {
// Note: if the constraints you add below require a larger cell size
// than the current size (which is likely to be the default size {320,
// 44}), you'll get an exception. As a fix, you can temporarily
// increase the size of the cell's contentView so that this does not
// occur using code similar to the line below. See here for further
// discussion:
// https://github.com/Alex311/TableCellWithAutoLayout/commit/bde387b27e33605eeac3465475d2f2ff9775f163#commitcomment-4633188
self.contentView.bounds = CGRectMake(0, 0, 99999, 99999);
[self.captionLabel autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, STXCaptionViewLeadingEdgeInset, 0, STXCaptionViewTrailingEdgeInset)];
self.didSetupConstraints = YES;
}
[super updateConstraints];
}
- (void)setCaption:(NSDictionary *)caption
{
if (_caption != caption) {
_caption = caption;
NSString *text = [caption stringValueForComplexKeyPath:@"text"];
[self setCaptionLabel:self.captionLabel text:text];
}
}
#pragma mark - Attributed Label
- (void)setCaptionLabel:(STXAttributedLabel *)captionLabel text:(NSString *)text
{
NSString *trimmedText = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSMutableArray *textCheckingResults = [NSMutableArray array];
[captionLabel setText:trimmedText afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:HashTagAndMentionRegex
options:NSRegularExpressionCaseInsensitive
error:&error];
if (error) {
UALog(@"%@", error);
}
[regex enumerateMatchesInString:[mutableAttributedString string] options:0 range:NSMakeRange(0, [mutableAttributedString length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
[textCheckingResults addObject:result];
}];
return mutableAttributedString;
}];
for (NSTextCheckingResult *result in textCheckingResults) {
[captionLabel addLinkWithTextCheckingResult:result];
}
}
- (STXAttributedLabel *)captionLabelWithText:(NSString *)text
{
STXAttributedLabel *captionLabel = [[STXAttributedLabel alloc] initForParagraphStyleWithText:text];
captionLabel.textColor = [UIColor blackColor];
captionLabel.lineBreakMode = NSLineBreakByWordWrapping;
captionLabel.numberOfLines = 0;
captionLabel.delegate = self;
captionLabel.userInteractionEnabled = YES;
captionLabel.linkAttributes = @{ (NSString *)kCTForegroundColorAttributeName: [UIColor blueColor],
(NSString *)kCTFontAttributeName: [UIFont boldSystemFontOfSize:14],
(NSString *)kCTUnderlineStyleAttributeName : @(kCTUnderlineStyleNone) };
captionLabel.activeLinkAttributes = captionLabel.linkAttributes;
captionLabel.inactiveLinkAttributes = captionLabel.linkAttributes;
[self setCaptionLabel:captionLabel text:text];
return captionLabel;
}
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTextCheckingResult:(NSTextCheckingResult *)result
{
NSString *hashtagOrMention = [[label.attributedText string] substringWithRange:result.range];
UALog(@"%@", hashtagOrMention);
if ([hashtagOrMention hasPrefix:@"#"]) {
NSString *hashtag = [hashtagOrMention substringFromIndex:1];
if ([self.delegate respondsToSelector:@selector(captionCell:didSelectHashtag:)]) {
[self.delegate captionCell:self didSelectHashtag:hashtag];
}
} else if ([hashtagOrMention hasPrefix:@"@"]) {
NSString *mention = [hashtagOrMention substringFromIndex:1];
if ([self.delegate respondsToSelector:@selector(captionCell:didSelectMention:)]) {
[self.delegate captionCell:self didSelectMention:mention];
}
}
}
@end