forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDITaskTableViewCell.m
154 lines (121 loc) · 4.99 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
//
// 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 "UIColor+CheddariOSAdditions.h"
#import "UIFont+CheddariOSAdditions.h"
#import "CDKTask+CheddariOSAdditions.h"
@implementation CDITaskTableViewCell {
UIImageView *_checkbox;
UIImageView *_checkmark;
}
@synthesize task = _task;
@synthesize attributedLabel = _attributedLabel;
- (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];
}
[_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];
}
}
}
#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.font = [UIFont cheddarFontOfSize:20.0f];
label.numberOfLines = 0;
});
label.text = task.attributedDisplayText;
CGSize size = [label sizeThatFits:CGSizeMake(width - 54.0f, 2000.0f)];
label.text = nil;
return fmaxf(size.height + 27.0f, [self cellHeight]);
}
#pragma mark - UITableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])) {
self.textLabel.hidden = YES;
_checkbox = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"checkbox.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:4]];
[self.contentView addSubview:_checkbox];
_checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"small-check.png"]];
_checkmark.hidden = YES;
[self.contentView addSubview:_checkmark];
_attributedLabel = [[CDIAttributedLabel alloc] initWithFrame:CGRectZero];
_attributedLabel.textColor = [UIColor cheddarTextColor];
_attributedLabel.font = [UIFont cheddarFontOfSize:20.0f];
_attributedLabel.backgroundColor = [UIColor clearColor];
_attributedLabel.numberOfLines = 0;
_attributedLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentTop;
[self.contentView addSubview:_attributedLabel];
self.contentView.clipsToBounds = YES;
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGSize size = self.contentView.bounds.size;
if (self.editing) { // TODO: Only match reordering and not swipe to delete
_checkbox.frame = CGRectMake(-34.0f, 13.0f, 24.0f, 24.0f);
_checkmark.frame = CGRectMake(-30.0f, 16.0f, 22.0f, 18.0f);
_attributedLabel.frame = CGRectMake(12.0f, 13.0f, size.width - 20.0f, size.height - 27.0f);
} else {
_checkbox.frame = CGRectMake(10.0f, 13.0f, 24.0f, 24.0f);
_checkmark.frame = CGRectMake(12.0f, 16.0f, 22.0f, 18.0f);
_attributedLabel.frame = CGRectMake(44.0f, 13.0f, size.width - 54.0f, size.height - 27.0f);
}
}
#pragma mark - UITableViewCell
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
void (^change)(void) = ^{
_checkbox.alpha = editing ? 0.0f : 1.0f;
_checkmark.alpha = _checkbox.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;
}
@end