This repository has been archived by the owner on Aug 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
SSPersonHeaderView.m
151 lines (110 loc) · 4.21 KB
/
SSPersonHeaderView.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
//
// SSPersonHeaderView.m
// SSToolkit
//
// Created by Sam Soffes on 9/8/10.
// Copyright 2010 Sam Soffes. All rights reserved.
//
#import "SSPersonHeaderView.h"
#import <QuartzCore/QuartzCore.h>
static CGFloat kSSPersonHeaderViewImageSize = 64.0;
@interface SSPersonHeaderView (PrivateMethods)
- (void)_updateImage;
@end
@implementation SSPersonHeaderView
@synthesize organization = _organization;
@synthesize alignImageToLeft = _alignImageToLeft;
@synthesize imageView = _imageView;
@synthesize personName = _personName;
@synthesize organizationName = _organizationName;
#pragma mark NSObject
- (void)dealloc {
[_imageView removeFromSuperview];
[_imageView release];
self.personName = nil;
self.organizationName = nil;
[super dealloc];
}
#pragma mark UIView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.opaque = YES;
_organization = NO;
_alignImageToLeft = YES;
_imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
_imageView.clipsToBounds = YES;
_imageView.layer.cornerRadius = 3.0;
[self addSubview:_imageView];
[self _updateImage];
}
return self;
}
- (void)layoutSubviews {
_imageView.frame = CGRectMake(_alignImageToLeft ? 10.0 : 19.0, 15.0, kSSPersonHeaderViewImageSize, kSSPersonHeaderViewImageSize);
}
- (void)drawRect:(CGRect)rect {
CGFloat textX = _alignImageToLeft ? 87.0 : 96.0;
CGFloat width = self.frame.size.width - 105.0;
CGSize constraintSize = CGSizeMake(width, 200.0);
UILineBreakMode lineBreakMode = UILineBreakModeWordWrap;
UIColor *textColor = [UIColor blackColor];
UIColor *shadowTextColor = [UIColor whiteColor];
UIFont *personNameFont = [UIFont boldSystemFontOfSize:18.0];
UIFont *organizationNameFont = [UIFont systemFontOfSize:14.0];
// Calculate sizes
CGSize personNameSize = [_personName sizeWithFont:personNameFont constrainedToSize:constraintSize lineBreakMode:lineBreakMode];
CGSize organizationNameSize = _organizationName ? [_organizationName sizeWithFont:organizationNameFont constrainedToSize:constraintSize lineBreakMode:lineBreakMode] : CGSizeZero;
// Draw person name
CGFloat personNameY = 15.0;
if (_organizationName) {
personNameY += roundf((kSSPersonHeaderViewImageSize - personNameSize.height - organizationNameSize.height) / 2.0);
} else {
personNameY += roundf((kSSPersonHeaderViewImageSize - personNameSize.height) / 2.0);
}
CGRect personNameRect = CGRectMake(textX, personNameY, personNameSize.width, personNameSize.height);
[shadowTextColor set];
personNameRect.origin = CGPointMake(personNameRect.origin.x, personNameRect.origin.y + 1.0f);
[_personName drawInRect:personNameRect withFont:personNameFont lineBreakMode:lineBreakMode];
[textColor set];
[_personName drawInRect:personNameRect withFont:personNameFont lineBreakMode:lineBreakMode];
// Draw organization name
if (_organizationName) {
CGFloat organizationNameY = personNameRect.origin.y + personNameRect.size.height;
CGRect organizationNameRect = CGRectMake(textX, organizationNameY, organizationNameSize.width, organizationNameSize.height);
[shadowTextColor set];
organizationNameRect.origin = CGPointMake(organizationNameRect.origin.x, organizationNameRect.origin.y + 1.0f);
[_organizationName drawInRect:organizationNameRect withFont:organizationNameFont lineBreakMode:lineBreakMode];
[textColor set];
[_organizationName drawInRect:organizationNameRect withFont:organizationNameFont lineBreakMode:lineBreakMode];
}
}
#pragma mark Private Methods
- (void)_updateImage {
if (_imageView.image) {
_imageView.layer.borderColor = [UIColor colorWithWhite:0.0 alpha:0.5].CGColor;
_imageView.layer.borderWidth = 1.0;
return;
}
_imageView.image = [UIImage imageNamed:(_organization ? @"ABPictureOrganization.png" : @"ABPicturePerson.png")];
_imageView.layer.borderColor = nil;
_imageView.layer.borderWidth = 0.0;
}
#pragma mark Setters
- (void)setOrganization:(BOOL)org {
if (_organization == org) {
return;
}
_organization = org;
[self _updateImage];
[self setNeedsDisplay];
}
- (void)setImage:(UIImage *)image {
self.imageView.image = image;
[self _updateImage];
}
#pragma mark Getters
- (UIImage *)image {
return self.imageView.image;
}
@end