forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDITaskTableViewCell.m
182 lines (141 loc) · 5.7 KB
/
CDITaskTableViewCell.m
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
//
// CDITaskTableViewCell.m
// Cheddar for iOS
//
// Created by Sam Soffes on 4/5/12.
// Copyright (c) 2012 Nothing Magical. All rights reserved.
//
#import "CDITaskTableViewCell.h"
#import "CDIAttributedLabel.h"
#import "CDICheckboxButton.h"
#import "UIColor+CheddariOSAdditions.h"
#import "UIFont+CheddariOSAdditions.h"
#import "CDKTask+CheddariOSAdditions.h"
#import "CDISettingsTextSizePickerViewController.h"
@interface CDITaskTableViewCell ()
- (void)_updateAttributedText;
@end
@implementation CDITaskTableViewCell {
UIImageView *_checkmark;
}
@synthesize task = _task;
@synthesize attributedLabel = _attributedLabel;
@synthesize checkboxButton = _checkboxButton;
- (void)setTask:(CDKTask *)task {
_task = task;
if (_task.isCompleted) {
_attributedLabel.textColor = [UIColor cheddarLightTextColor];
_checkmark.hidden = NO;
_attributedLabel.linkAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
(id)[UIColor colorWithWhite:0.45f alpha:1.0f].CGColor, (NSString *)kCTForegroundColorAttributeName,
nil];
} else {
_attributedLabel.textColor = [UIColor cheddarTextColor];
_checkmark.hidden = YES;
_attributedLabel.linkAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
(id)[UIColor cheddarBlueColor].CGColor, (NSString *)kCTForegroundColorAttributeName,
nil];
}
[self _updateAttributedText];
}
#pragma mark - Class Methods
+ (CGFloat)cellHeightForTask:(CDKTask *)task width:(CGFloat)width {
static TTTAttributedLabel *label = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
label.numberOfLines = 0;
});
label.font = [UIFont cheddarFontOfSize:18.0f];
label.text = task.attributedDisplayText;
CGSize size = [label sizeThatFits:CGSizeMake(width - 54.0f, 2000.0f)];
label.text = nil;
CGFloat offset = ([CDISettingsTextSizePickerViewController fontSizeAdjustment] * 2.0f) - 1.0f;
return size.height + 27.0f + offset;
}
#pragma mark - UITableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])) {
self.textLabel.hidden = YES;
_checkboxButton = [[CDICheckboxButton alloc] initWithFrame:CGRectZero];
_checkboxButton.tableViewCell = self;
[self.contentView addSubview:_checkboxButton];
_checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"small-check"]];
_checkmark.hidden = YES;
[self.contentView addSubview:_checkmark];
_attributedLabel = [[CDIAttributedLabel alloc] initWithFrame:CGRectZero];
_attributedLabel.textColor = [UIColor cheddarTextColor];
_attributedLabel.backgroundColor = [UIColor clearColor];
_attributedLabel.numberOfLines = 0;
_attributedLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentTop;
[self updateFonts];
[self.contentView addSubview:_attributedLabel];
self.contentView.clipsToBounds = YES;
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGSize size = self.contentView.bounds.size;
CGFloat offset = ([CDISettingsTextSizePickerViewController fontSizeAdjustment] * 2.0f) - 1.0f;
CGFloat textYOffset = roundf(offset / 2.0f);
if (self.editing) { // TODO: Only match reordering and not swipe to delete
_checkboxButton.frame = CGRectMake(-34.0f, 13.0f + offset, 24.0f, 24.0f);
_checkmark.frame = CGRectMake(-30.0f, 16.0f + offset, 22.0f, 18.0f);
_attributedLabel.frame = CGRectMake(12.0f, 13.0f + textYOffset, size.width - 20.0f, size.height - 27.0f - offset);
} else {
_checkboxButton.frame = CGRectMake(10.0f, 13.0f + offset, 24.0f, 24.0f);
_checkmark.frame = CGRectMake(12.0f, 16.0f + offset, 22.0f, 18.0f);
_attributedLabel.frame = CGRectMake(44.0f, 13.0f + textYOffset, size.width - 54.0f, size.height - 27.0f - offset);
}
}
#pragma mark - UITableViewCell
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
void (^change)(void) = ^{
_checkboxButton.alpha = editing ? 0.0f : 1.0f;
_checkmark.alpha = _checkboxButton.alpha;
};
if (animated) {
[UIView animateWithDuration:0.18 delay:0.0 options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction animations:change completion:nil];
} else {
change();
}
}
- (void)prepareForReuse {
[super prepareForReuse];
self.task = nil;
}
#pragma mark - CDITableViewCell
- (void)updateFonts {
[super updateFonts];
_attributedLabel.font = [UIFont cheddarFontOfSize:18.0f];
[self _updateAttributedText];
}
#pragma Private
- (void)_updateAttributedText {
__weak CDKTask *task = _task;
[_attributedLabel setText:_task.displayText afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
[task addEntitiesToAttributedString:mutableAttributedString];
return mutableAttributedString;
}];
// Add links
for (NSDictionary *entity in _task.entities) {
NSArray *indices = [entity objectForKey:@"display_indices"];
NSRange range = NSMakeRange([[indices objectAtIndex:0] unsignedIntegerValue], 0);
range.length = [[indices objectAtIndex:1] unsignedIntegerValue] - range.location;
range = [_attributedLabel.text composedRangeWithRange:range];
NSString *type = [entity objectForKey:@"type"];
// Tag
if ([type isEqualToString:@"tag"]) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"x-cheddar-tag://%@", [entity objectForKey:@"tag_name"]]];
[_attributedLabel addLinkToURL:url withRange:range];
}
// Link
else if ([type isEqualToString:@"link"]) {
NSURL *url = [NSURL URLWithString:[entity objectForKey:@"url"]];
[_attributedLabel addLinkToURL:url withRange:range];
}
}
}
@end