/
TiUIProgressBar.m
170 lines (135 loc) Β· 3.49 KB
/
TiUIProgressBar.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
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#ifdef USE_TI_UIPROGRESSBAR
#import "TiUIProgressBar.h"
#import "TiUtils.h"
#import "WebFont.h"
@implementation TiUIProgressBar
-(id)initWithStyle:(UIProgressViewStyle)style_
{
if (self = [super initWithFrame:CGRectZero])
{
style = style_;
min = 0;
max = 1;
[self setHidden:YES];
}
return self;
}
-(void)dealloc
{
RELEASE_TO_NIL(progress);
RELEASE_TO_NIL(messageLabel);
[super dealloc];
}
-(CGSize)sizeForFont:(CGFloat)suggestedWidth
{
NSString *value = [messageLabel text];
UIFont *font = [messageLabel font];
CGSize maxSize = CGSizeMake(suggestedWidth<=0 ? 480 : suggestedWidth, 1000);
return [value sizeWithFont:font constrainedToSize:maxSize lineBreakMode:UILineBreakModeTailTruncation];
}
-(CGFloat)contentWidthForWidth:(CGFloat)suggestedWidth
{
return [self sizeForFont:suggestedWidth].width;
}
-(CGFloat)contentHeightForWidth:(CGFloat)width
{
CGSize fontSize = [self sizeForFont:width];
CGSize progressSize = [progress sizeThatFits:fontSize];
return fontSize.height + progressSize.height;
}
#pragma mark Accessors
-(UIProgressView*)progress
{
if (progress==nil)
{
progress = [[UIProgressView alloc] initWithProgressViewStyle:style];
[self addSubview:progress];
}
return progress;
}
-(UILabel *)messageLabel
{
if (messageLabel==nil)
{
messageLabel=[[UILabel alloc] init];
[messageLabel setBackgroundColor:[UIColor clearColor]];
[self setNeedsLayout];
[self addSubview:messageLabel];
}
return messageLabel;
}
#pragma mark Repositioning
-(void)layoutSubviews
{
if(progress == nil)
{
return;
}
CGRect boundsRect = [self bounds];
CGSize barSize = [progress sizeThatFits:boundsRect.size];
CGPoint centerPoint = CGPointMake(boundsRect.origin.x + (boundsRect.size.width/2),
boundsRect.origin.y + (boundsRect.size.height/2));
[progress setBounds:CGRectMake(0, 0, barSize.width, barSize.height)];
if (messageLabel == nil)
{
[progress setCenter:centerPoint];
return;
}
CGSize messageSize = [messageLabel sizeThatFits:boundsRect.size];
float fittingHeight = barSize.height + messageSize.height + 5;
[progress setCenter:CGPointMake(centerPoint.x,
centerPoint.y + (fittingHeight - barSize.height)/2)];
[messageLabel setBounds:CGRectMake(0, 0, messageSize.width, messageSize.height)];
[messageLabel setCenter:CGPointMake(centerPoint.x,
centerPoint.y - (fittingHeight - messageSize.height)/2)];
}
#pragma mark Properties
-(void)setMin_:(id)value
{
min = [TiUtils floatValue:value];
}
-(void)setMax_:(id)value
{
max = [TiUtils floatValue:value];
}
-(void)setValue_:(id)value
{
CGFloat newValue = ([TiUtils floatValue:value] - min) / (max-min);
[[self progress] setProgress:newValue];
}
-(void)setFont_:(id)value
{
WebFont * newFont = [TiUtils fontValue:value def:[WebFont defaultFont]];
[[self messageLabel] setFont:[newFont font]];
[self setNeedsLayout];
}
-(void)setColor_:(id)value
{
UIColor * newColor = [[TiUtils colorValue:value] _color];
if (newColor == nil) {
newColor = [UIColor blackColor];
}
[[self messageLabel] setTextColor:newColor];
}
-(void)setMessage_:(id)value
{
NSString * text = [TiUtils stringValue:value];
if ([text length]>0)
{
[[self messageLabel] setText:text];
}
else
{
[messageLabel removeFromSuperview];
RELEASE_TO_NIL(messageLabel);
}
[self setNeedsLayout];
}
@end
#endif