forked from 2359media/STXDynamicTableView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTXCommentCell.m
More file actions
236 lines (183 loc) · 9.45 KB
/
STXCommentCell.m
File metadata and controls
236 lines (183 loc) · 9.45 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//
// STXCommentCell.m
// STXDynamicTableViewExample
//
// Created by Jesse Armand on 10/4/14.
// Copyright (c) 2014 2359 Media Pte Ltd. All rights reserved.
//
#import "STXCommentCell.h"
#import "STXAttributedLabel.h"
static CGFloat STXCommentViewLeadingEdgeInset = 10.f;
static CGFloat STXCommentViewTrailingEdgeInset = 10.f;
static NSString *HashTagAndMentionRegex = @"(#|@)(\\w+)";
@interface STXCommentCell () <TTTAttributedLabelDelegate>
@property (nonatomic) STXCommentCellStyle cellStyle;
@property (strong, nonatomic) STXAttributedLabel *commentLabel;
@property (nonatomic) BOOL didSetupConstraints;
@end
@implementation STXCommentCell
- (id)initWithStyle:(STXCommentCellStyle)style comment:(id<STXCommentItem>)comment totalComments:(NSInteger)totalComments reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (self) {
_cellStyle = style;
if (style == STXCommentCellStyleShowAllComments) {
NSString *title = [NSString stringWithFormat:NSLocalizedString(@"Show %d comments", nil), totalComments];
_commentLabel = [self allCommentsLabelWithTitle:title];
} else {
id<STXUserItem> commenter = [comment from];
_commentLabel = [self commentLabelWithText:[comment text] commenter:[commenter username]];
}
[self.contentView addSubview:_commentLabel];
_commentLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
- (instancetype)initWithStyle:(STXCommentCellStyle)style comment:(id<STXCommentItem>)comment reuseIdentifier:(NSString *)reuseIdentifier
{
return [self initWithStyle:style comment:comment totalComments:0 reuseIdentifier:reuseIdentifier];
}
- (instancetype)initWithStyle:(STXCommentCellStyle)style totalComments:(NSInteger)totalComments reuseIdentifier:(NSString *)reuseIdentifier
{
return [self initWithStyle:style comment:nil totalComments:totalComments reuseIdentifier:reuseIdentifier];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self.contentView setNeedsLayout];
[self.contentView layoutIfNeeded];
self.commentLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.frame) - (STXCommentViewLeadingEdgeInset + STXCommentViewTrailingEdgeInset);
[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.commentLabel autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, STXCommentViewLeadingEdgeInset, 0, STXCommentViewTrailingEdgeInset)];
self.didSetupConstraints = YES;
}
[super updateConstraints];
}
- (void)setComment:(id<STXCommentItem>)comment
{
if (_comment != comment) {
_comment = comment;
id <STXUserItem> commenter = [comment from];
[self setCommentLabel:self.commentLabel text:[comment text] commenter:[commenter username]];
}
}
- (void)setTotalComments:(NSInteger)totalComments
{
if (_totalComments != totalComments) {
NSString *title = [NSString stringWithFormat:NSLocalizedString(@"Show %d comments", nil), totalComments];
[self setAllCommentsLabel:self.commentLabel title:title];
}
}
#pragma mark - Attributed Label
- (void)setAllCommentsLabel:(STXAttributedLabel *)commentLabel title:(NSString *)title
{
[commentLabel setText:title];
}
- (void)setCommentLabel:(STXAttributedLabel *)commentLabel text:(NSString *)text commenter:(NSString *)commenter
{
NSMutableArray *textCheckingResults = [NSMutableArray array];
[commentLabel setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSRange searchRange = NSMakeRange(0, [mutableAttributedString length]);
NSRange currentRange = [[mutableAttributedString string] rangeOfString:commenter options:NSLiteralSearch range:searchRange];
NSTextCheckingResult *textCheckingResult = [NSTextCheckingResult linkCheckingResultWithRange:currentRange URL:nil];
[textCheckingResults addObject:textCheckingResult];
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) {
[commentLabel addLinkWithTextCheckingResult:result];
}
}
- (STXAttributedLabel *)allCommentsLabelWithTitle:(NSString *)title
{
STXAttributedLabel *allCommentsLabel = [STXAttributedLabel newAutoLayoutView];
allCommentsLabel.delegate = self;
allCommentsLabel.textColor = [UIColor lightGrayColor];
allCommentsLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
allCommentsLabel.linkAttributes = @{ (NSString *)kCTFontAttributeName: allCommentsLabel.font,
(NSString *)kCTForegroundColorAttributeName: allCommentsLabel.textColor};
allCommentsLabel.activeLinkAttributes = allCommentsLabel.linkAttributes;
allCommentsLabel.inactiveLinkAttributes = allCommentsLabel.linkAttributes;
[allCommentsLabel setText:title];
NSTextCheckingResult *textCheckingResult = [NSTextCheckingResult linkCheckingResultWithRange:NSMakeRange(0, [title length]) URL:nil];
[allCommentsLabel addLinkWithTextCheckingResult:textCheckingResult];
return allCommentsLabel;
}
- (STXAttributedLabel *)commentLabelWithText:(NSString *)text commenter:(NSString *)commenter
{
NSString *trimmedText = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *commentText = [[commenter stringByAppendingString:@" "] stringByAppendingString:trimmedText];
STXAttributedLabel *commentLabel = [[STXAttributedLabel alloc] initForParagraphStyleWithText:commentText];
commentLabel.delegate = self;
commentLabel.numberOfLines = 0;
commentLabel.lineBreakMode = NSLineBreakByWordWrapping;
commentLabel.linkAttributes = @{ (NSString *)kCTForegroundColorAttributeName: [UIColor blueColor] };
commentLabel.activeLinkAttributes = commentLabel.linkAttributes;
commentLabel.inactiveLinkAttributes = commentLabel.linkAttributes;
[self setCommentLabel:commentLabel text:text commenter:commenter];
return commentLabel;
}
- (void)showAllComments:(id)sender
{
if ([self.delegate respondsToSelector:@selector(commentCellWillShowAllComments:)])
[self.delegate commentCellWillShowAllComments:self];
}
#pragma mark - Attributed Label
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTextCheckingResult:(NSTextCheckingResult *)result
{
NSString *selectedText = [[label.attributedText string] substringWithRange:result.range];
UALog(@"%@", selectedText);
if ([selectedText hasPrefix:@"http://"] || [selectedText hasPrefix:@"https://"]) {
NSURL *selectedURL = [NSURL URLWithString:selectedText];
if (selectedURL) {
if ([self.delegate respondsToSelector:@selector(commentCell:didSelectURL:)]) {
[self.delegate commentCell:self didSelectURL:selectedURL];
}
return;
}
} else if ([selectedText hasPrefix:@"#"]) {
NSString *hashtag = [selectedText substringFromIndex:1];
if ([self.delegate respondsToSelector:@selector(commentCell:didSelectHashtag:)]) {
[self.delegate commentCell:self didSelectHashtag:hashtag];
}
} else if ([selectedText hasPrefix:@"@"]) {
NSString *mention = [selectedText substringFromIndex:1];
if ([self.delegate respondsToSelector:@selector(commentCell:didSelectMention:)]) {
[self.delegate commentCell:self didSelectMention:mention];
}
}
if (self.cellStyle == STXCommentCellStyleShowAllComments) {
[self showAllComments:label];
} else {
if ([self.delegate respondsToSelector:@selector(commentCell:willShowCommenter:)]) {
[self.delegate commentCell:self willShowCommenter:[self.comment from]];
}
}
}
@end