-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIViewSegments.m
103 lines (75 loc) · 2.54 KB
/
UIViewSegments.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
//
// UIViewSegments.m
//
// Licensed by ruralcoder.com under the
// Creative Commons Attribution-ShareAlike 3.0 Unported License
#import "UIViewSegments.h"
@implementation UIViewSegments
@synthesize segments = _segments;
- (id) init
{
self = [super init];
if (self)
_segments = [[NSMutableArray alloc] init];
return self;
}
- (void) dealloc
{
[_segments release];
[super dealloc];
}
- (NSString*) description
{
return [self.segments description];
}
+ (UILabel*) labelWithText:(id)text font:(UIFont*)font color:(UIColor*)color width:(float)width
{
return [UIViewSegments labelWithMultilineText:text font:font color:color width:width ];
}
+ (UILabel*) labelWithOneLineText:(id)text fontSize:(CGFloat)fontSize color:(UIColor*)color width:(float)width
{
CGFloat height = fontSize;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
label.text = text;
label.numberOfLines = 1;
label.textColor = color;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:fontSize];
label.lineBreakMode = UILineBreakModeTailTruncation;
label.textAlignment = UITextAlignmentLeft;
return [label autorelease];
}
+ (UILabel*) labelWithMultilineText:(id)text font:(UIFont*)font color:(UIColor*)color width:(float)width
{
CGSize maxSize = CGSizeMake(width, 9999);
CGSize size = [text sizeWithFont:font
constrainedToSize:maxSize
lineBreakMode:UILineBreakModeWordWrap];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
label.text = text;
label.numberOfLines = 0;
label.textColor = color;
label.backgroundColor = [UIColor clearColor];
label.font = font;
label.lineBreakMode = UILineBreakModeWordWrap;
label.textAlignment = UITextAlignmentLeft;
return [label autorelease];
}
- (UIView*) containerWithMarginBetweetSegments:(NSUInteger)margin frame:(CGRect)frame
{
NSUInteger calculatedHeight = 0;
CGRect updatedFrame;
UIView *container = [[UIView alloc] initWithFrame:frame];
for (UIView *segment in self.segments)
{
updatedFrame = segment.frame;
updatedFrame.origin.y = calculatedHeight;
segment.frame = updatedFrame;
[container addSubview:segment];
calculatedHeight += updatedFrame.size.height + margin;
}
frame.size.height = calculatedHeight;
container.frame = frame;
return [container autorelease];
}
@end