forked from erichoracek/MSCollectionViewCalendarLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSEventCell.m
More file actions
174 lines (136 loc) · 6.07 KB
/
Copy pathMSEventCell.m
File metadata and controls
174 lines (136 loc) · 6.07 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
//
// MSEventCell.m
// Example
//
// Created by Eric Horacek on 2/26/13.
// Copyright (c) 2013 Monospace Ltd. All rights reserved.
//
#import "MSEventCell.h"
#import "MSEvent.h"
@interface MSEventCell ()
- (UIColor *)cellBackgroundColorSelected:(BOOL)selected;
- (UIColor *)cellTextColorSelected:(BOOL)selected;
- (UIColor *)cellBorderColorSelected:(BOOL)selected;
- (UIColor *)cellTextShadowColorSelected:(BOOL)selected;
- (CGSize)cellTextShadowOffsetSelected:(BOOL)selected;
- (void)updateColors;
@end
@implementation MSEventCell
#pragma mark - UIView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
self.layer.shouldRasterize = YES;
self.contentView.layer.borderWidth = 1.0;
self.contentView.layer.cornerRadius = 4.0;
self.contentView.layer.masksToBounds = YES;
self.time = [UILabel new];
self.time.backgroundColor = [UIColor clearColor];
self.time.numberOfLines = 0;
self.time.font = [UIFont systemFontOfSize:12.0];
[self.contentView addSubview:self.time];
self.title = [UILabel new];
self.title.backgroundColor = [UIColor clearColor];
self.title.numberOfLines = 0;
self.title.font = [UIFont boldSystemFontOfSize:12.0];
[self.contentView addSubview:self.title];
self.location = [UILabel new];
self.location.backgroundColor = [UIColor clearColor];
self.location.numberOfLines = 0;
self.location.font = [UIFont systemFontOfSize:12.0];
[self.contentView addSubview:self.location];
[self updateColors];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
UIEdgeInsets padding = UIEdgeInsetsMake(4.0, 5.0, 4.0, 5.0);
CGFloat contentMargin = 2.0;
CGFloat contentWidth = (CGRectGetWidth(self.contentView.frame) - padding.left - padding.right);
CGSize maxTimeSize = CGSizeMake(contentWidth, CGRectGetHeight(self.contentView.frame) - padding.top - padding.bottom);
CGSize timeSize = [self.time.text sizeWithFont:self.time.font constrainedToSize:maxTimeSize lineBreakMode:self.time.lineBreakMode];
CGRect timeFrame = self.time.frame;
timeFrame.size = timeSize;
timeFrame.origin.x = padding.left;
timeFrame.origin.y = padding.top;
self.time.frame = timeFrame;
CGSize maxTitleSize = CGSizeMake(contentWidth, CGRectGetHeight(self.contentView.frame) - (CGRectGetMaxY(timeFrame) + contentMargin) - padding.bottom);
CGSize titleSize = [self.title.text sizeWithFont:self.title.font constrainedToSize:maxTitleSize lineBreakMode:self.title.lineBreakMode];
CGRect titleFrame = self.title.frame;
titleFrame.size = titleSize;
titleFrame.origin.x = padding.left;
titleFrame.origin.y = (CGRectGetMaxY(timeFrame) + contentMargin);
self.title.frame = titleFrame;
CGSize maxLocationSize = CGSizeMake(contentWidth, CGRectGetHeight(self.contentView.frame) - (CGRectGetMaxY(titleFrame) + contentMargin) - padding.bottom);
CGSize locationSize = [self.location.text sizeWithFont:self.location.font constrainedToSize:maxLocationSize lineBreakMode:self.location.lineBreakMode];
CGRect locationFrame = self.location.frame;
locationFrame.size = locationSize;
locationFrame.origin.x = padding.left;
locationFrame.origin.y = (CGRectGetMaxY(titleFrame) + contentMargin);
self.location.frame = locationFrame;
}
#pragma mark - UICollectionViewCell
- (void)setSelected:(BOOL)selected
{
if (selected && self.selected != selected) {
[UIView animateWithDuration:0.1 animations:^{
self.transform = CGAffineTransformMakeScale(1.05, 1.05);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 animations:^{
self.transform = CGAffineTransformIdentity;
}];
}];
}
[super setSelected:selected];
[self updateColors];
}
#pragma mark - MSEventCell
- (void)setEvent:(MSEvent *)event
{
_event = event;
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"h:mm a";
self.time.text = [dateFormatter stringFromDate:event.start];
self.title.text = event.title;
self.location.text = event.location;
[self setNeedsLayout];
}
- (void)updateColors
{
self.contentView.backgroundColor = [self cellBackgroundColorSelected:self.selected];
self.contentView.layer.borderColor = [[self cellBorderColorSelected:self.selected] CGColor];
self.time.textColor = [self cellTextColorSelected:self.selected];
self.time.shadowColor = [self cellTextShadowColorSelected:self.selected];
self.time.shadowOffset = [self cellTextShadowOffsetSelected:self.selected];
self.title.textColor = [self cellTextColorSelected:self.selected];
self.title.shadowColor = [self cellTextShadowColorSelected:self.selected];
self.title.shadowOffset = [self cellTextShadowOffsetSelected:self.selected];
self.location.textColor = [self cellTextColorSelected:self.selected];
self.location.shadowColor = [self cellTextShadowColorSelected:self.selected];
self.location.shadowOffset = [self cellTextShadowOffsetSelected:self.selected];
}
- (UIColor *)cellBackgroundColorSelected:(BOOL)selected
{
return selected ? [[UIColor colorWithHexString:@"165b9b"] colorWithAlphaComponent:0.8] : [[UIColor colorWithHexString:@"b4d0ea"] colorWithAlphaComponent:0.8];
}
- (UIColor *)cellTextColorSelected:(BOOL)selected
{
return selected ? [UIColor whiteColor] : [UIColor colorWithHexString:@"2b77ad"];
}
- (UIColor *)cellBorderColorSelected:(BOOL)selected
{
return selected ? [UIColor colorWithHexString:@"0c2e4d"] : [UIColor colorWithHexString:@"2b77ad"];
}
- (UIColor *)cellTextShadowColorSelected:(BOOL)selected
{
return selected ? [[UIColor blackColor] colorWithAlphaComponent:0.5] : [[UIColor whiteColor] colorWithAlphaComponent:0.5];
}
- (CGSize)cellTextShadowOffsetSelected:(BOOL)selected
{
return selected ? CGSizeMake(0.0, -1.0) : CGSizeMake(0.0, 1.0);
}
@end