-
Notifications
You must be signed in to change notification settings - Fork 0
/
JUIFactory.m
673 lines (529 loc) · 21.7 KB
/
JUIFactory.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//
// NLJsonedUIFactory.m
//
// Created by snaiper on 5/24/12.
// Copyright (c) 2012 snaiper All rights reserved.
//
#import "JUIFactory.h"
#import "JSONKit.h"
#import "NSData+Base64.h"
#import "UIButton+Block.h"
// supported type
typedef enum
{
NLUITypeLabel = 0,
NLUITypeImageView,
NLUITypeButton,
NLUITypeView,
NLUITypeUnknown,
} NLUIType;
typedef enum
{
NLResourceTypeImage = 0,
NLResourceTypeUnknown,
} NLResourceType;
enum AutoCenterType
{
NLAutoCenterTypeNone = 1 << 0,
NLAutoCenterTypeHorizontal = 1 << 1,
NLAutoCenterTypeVertical = 1 << 2,
};
// define
#define kImageCacheCost (1024 * 1024 * 5)
#define kSupportedVersion (1)
#define kVersionKey @"version"
#define kViewsKey @"views"
#define kResoucesKey @"resource"
#define kUITypeStringView @"view"
#define kUITypeStringLabel @"label"
#define kUITypeStringImageView @"imageview"
#define kUITypeStringButton @"button"
#define kResourceTypeStringImage @"image"
// common
#define kCommonAttributeIDKey @"id"
#define kCommonAttributeTypeKey @"type"
#define kCommonAttributeFrameKey @"frame"
#define kCommonAttributeBackgroundColorKey @"backgroundColor"
#define kCommonAttributeChildrenKey @"children"
#define kCommonAttributeAutoCenterKey @"autocenter"
#define kAutoCenterTypeHorizontal @"h"
#define kAutoCenterTypeVertical @"v"
#define kAutoCenterTypeSeperator @"|"
// font
#define kDefaultSystemFontSymbolName @"system"
#define kDefaultBoldSystemFontSymbolName @"bold-system"
// label
#define kLabelAttributeShadowColorKey @"shadowColor"
#define kLabelAttributeShadowOffsetKey @"shadowOffset"
#define kLabelAttributeFontFamilyKey @"font"
#define kLabelAttributeFontPointSizeKey @"fontSize"
#define kLabelAttributeTextKey @"text"
#define kLabelAttributeTextColorKey @"textColor"
#define kLabelAttributeMultilineKey @"multiline"
#define kLabelAttributeAlignmentKey @"align"
// image
#define kImageAttributeImageSourceKey @"source"
// button
#define kButtonAttributeTitleKey @"title"
#define kButtonAttributeTitleFontFamilyKey @"titleFont"
#define kButtonAtrributeTitleFontPointSizeKey @"titleFontSize"
#define kButtonAttributeTitleColor @"titleColor"
#define kButtonAttributeTitleShadowColorKey @"titleShadowColor"
#define kButtonAttributeTitleShadowOffsetKey @"titleShadowOffset"
#define kButtonAttributeBackgroundImageKey @"backgroundImage"
#define kButtonAttributeActionURIKey @"action-uri"
#define kButtonAttributeStateNormalKey @"normal"
#define kButtonAttributeStateHighlightedKey @"highlighted"
// resource
#define kResourceTypeKey @"type"
#define kResourceFileNameKey @"filename"
#define kResourcePathNameKey @"path"
#define kResourceDataKey @"data"
#define kResourceImageStretchedPointKey @"stretchedPoint"
@interface JUIFactory ()
- (void)initialize;
@end
@implementation JUIFactory {
@private
NSArray *mViews;
NSDictionary *mResources;
NSString *mCurrentLanguage;
NSCache *mImageCache;
}
#pragma mark -
#pragma mark Life Cycle
- (id)init
{
self = [super init];
if (self != nil)
{
[self initialize];
}
return self;
}
- (void)initialize
{
// initialize
mCurrentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
mImageCache = [[NSCache alloc] init];
[mImageCache setTotalCostLimit:kImageCacheCost];
}
- (void)reset
{
mViews = nil;
mResources = nil;
[mImageCache removeAllObjects];
}
#pragma mark -
#pragma mark Conversion
- (NLUIType)uiTypeFromString:(NSString *)aString
{
aString = [[aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] lowercaseString];
if ([aString caseInsensitiveCompare:kUITypeStringLabel] == NSOrderedSame)
return NLUITypeLabel;
else if ([aString caseInsensitiveCompare:kUITypeStringImageView] == NSOrderedSame)
return NLUITypeImageView;
else if ([aString caseInsensitiveCompare:kUITypeStringButton] == NSOrderedSame)
return NLUITypeButton;
else if ([aString caseInsensitiveCompare:kUITypeStringView] == NSOrderedSame)
return NLUITypeView;
else
return NLUITypeUnknown;
}
- (NLResourceType)resourceTypeFromString:(NSString *)aString
{
aString = [[aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] lowercaseString];
if ([aString caseInsensitiveCompare:kResourceTypeStringImage] == NSOrderedSame)
return NLResourceTypeImage;
else
return NLResourceTypeUnknown;
}
- (UITextAlignment)textAlignmentFromString:(NSString *)aString
{
aString = [[aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] lowercaseString];
if ([aString caseInsensitiveCompare:@"center"] == NSOrderedSame)
return UITextAlignmentCenter;
else if ([aString caseInsensitiveCompare:@"right"] == NSOrderedSame)
return UITextAlignmentRight;
else
return UITextAlignmentLeft;
}
- (NSInteger)autoCenterTypeFromString:(NSString *)aString
{
if ([aString length] == 0)
return NLAutoCenterTypeNone;
aString = [[aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] lowercaseString];
NSInteger sType = 0;
NSArray *sComponents = [aString componentsSeparatedByString:kAutoCenterTypeSeperator];
for (NSString *sTypeString in sComponents)
{
if ([sTypeString isEqualToString:kAutoCenterTypeHorizontal] == YES)
{
sType |= NLAutoCenterTypeHorizontal;
}
else if ([sTypeString isEqualToString:kAutoCenterTypeVertical] == YES)
{
sType |= NLAutoCenterTypeVertical;
}
else
{
continue;
}
}
return sType;
}
#pragma mark -
#pragma mark Image Cache
- (void)setImage:(UIImage *)anImage forKey:(NSString *)aKey dataSize:(NSUInteger)aDataSize
{
if (anImage != nil && aKey != nil && aDataSize != 0)
[mImageCache setObject:anImage forKey:aKey cost:aDataSize];
}
- (void)removeImageForURL:(NSURL *)aURL
{
if (aURL != nil)
[mImageCache removeObjectForKey:[aURL absoluteString]];
}
- (void)clearImageCache
{
[mImageCache removeAllObjects];
}
- (UIImage *)imageForKey:(NSString *)aKey
{
return [mImageCache objectForKey:aKey];
}
#pragma mark -
#pragma mark Parse Logics
- (BOOL)parse:(NSDictionary *)aParsedResult
{
NSLog(@"%@", aParsedResult);
NSNumber *sVersion = [aParsedResult objectForKey:kVersionKey];
NSArray *sViews = [aParsedResult objectForKey:kViewsKey];
NSDictionary *sResources = [aParsedResult objectForKey:kResoucesKey];
if (sVersion == nil || sViews == nil || sResources == nil)
{
NSLog(@"Required attributes is not existed");
return NO;
}
if ([sVersion integerValue] > kSupportedVersion)
return NO;
mViews = sViews;
mResources = sResources;
return YES;
}
- (UIColor *)colorFromHTMLStyleString:(NSString *)aColorCode
{
NSString *sTrimmed = [[aColorCode stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
if ([sTrimmed length] < 6)
return [UIColor blackColor];
if ([sTrimmed hasPrefix:@"0X"])
sTrimmed = [sTrimmed substringFromIndex:2];
if ([sTrimmed hasPrefix:@"#"])
sTrimmed = [sTrimmed substringFromIndex:1];
if ([sTrimmed length] != 6)
return [UIColor blackColor];
NSRange sRange;
sRange.location = 0;
sRange.length = 2;
NSString *rcolorString = [sTrimmed substringWithRange:sRange];
sRange.location = 2;
NSString *gcolorString = [sTrimmed substringWithRange:sRange];
sRange.location = 4;
NSString *bcolorString = [sTrimmed substringWithRange:sRange];
unsigned int sRed = 0, sGreen = 0, sBlue = 0;
[[NSScanner scannerWithString:rcolorString] scanHexInt:&sRed];
[[NSScanner scannerWithString:gcolorString] scanHexInt:&sGreen];
[[NSScanner scannerWithString:bcolorString] scanHexInt:&sBlue];
return [UIColor colorWithRed:((float) sRed / 255.0f)
green:((float) sGreen / 255.0f)
blue:((float) sBlue / 255.0f)
alpha:1.0f];
}
- (NSInteger)tagForControlIDString:(NSString *)aIDString
{
return [aIDString hash];
}
- (UIFont *)fontWithFontFamilyString:(NSString *)aFontFamily fontSize:(NSNumber *)aFontSize
{
if ([aFontFamily caseInsensitiveCompare:kDefaultSystemFontSymbolName] == NSOrderedSame)
return [UIFont systemFontOfSize:[aFontSize floatValue]];
else if ([aFontFamily caseInsensitiveCompare:kDefaultBoldSystemFontSymbolName] == NSOrderedSame)
return [UIFont boldSystemFontOfSize:[aFontSize floatValue]];
else
return [UIFont fontWithName:aFontFamily size:[aFontSize floatValue]];
}
- (NSString *)textForCurrentLanguage:(NSDictionary *)aTextDictionary
{
if (aTextDictionary == nil)
return @"";
NSString *sCurrentLanguageText = [aTextDictionary objectForKey:mCurrentLanguage];
if (sCurrentLanguageText == nil)
sCurrentLanguageText = [aTextDictionary objectForKey:@"en"];
if (sCurrentLanguageText != nil)
return sCurrentLanguageText;
else
return @"";
}
- (UIImage *)imageForResourceIDString:(NSString *)aResourceID
{
UIImage *sCachedImage = [self imageForKey:aResourceID];
if (sCachedImage != nil)
return sCachedImage;
NSDictionary *sInfo = [mResources objectForKey:aResourceID];
if (sInfo == nil)
return nil;
NSString *sType = [sInfo objectForKey:kResourceTypeKey];
if (sType == nil)
return nil;
NLResourceType sResourceType = [self resourceTypeFromString:sType];
if (sResourceType != NLResourceTypeImage)
return nil;
NSString *sImageDataString = [sInfo objectForKey:kResourceDataKey];
NSString *sPath = [sInfo objectForKey:kResourcePathNameKey];
NSString *sFileName = [sInfo objectForKey:kResourceFileNameKey];
NSString *sStrechedPoint = [sInfo objectForKey:kResourceImageStretchedPointKey];
UIImage *sImage = nil;
if (sImageDataString != nil)
{
sImage = [UIImage imageWithData:[NSData dataWithBase64EncodedString:sImageDataString]];
}
else if (sPath != nil)
{
NSData *sImageData = nil;
// TODO: do asynchronous
if ([sPath hasPrefix:@"http://"] == YES || [sPath hasPrefix:@"https://"] == YES)
{
sImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:sPath]];
}
else
{
sImageData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:sPath]];
}
sImage = [UIImage imageWithData:sImageData];
}
else
{
if ([sFileName length] == 0)
return nil;
sImage = [UIImage imageNamed:sFileName];
}
if (sImage == nil)
return nil;
if (sStrechedPoint != nil)
{
CGPoint sPoint = CGPointFromString(sStrechedPoint);
sImage = [sImage stretchableImageWithLeftCapWidth:sPoint.x topCapHeight:sPoint.y];
}
NSData *sData = UIImagePNGRepresentation(sImage);
[self setImage:sImage forKey:aResourceID dataSize:[sData length]];
return sImage;
}
#pragma mark -
#pragma mark making UI Controls
- (UILabel *)labelWithFrame:(CGRect)aFrame attributes:(NSDictionary *)aViewInfo
{
NSString *sFontFamily = [aViewInfo objectForKey:kLabelAttributeFontFamilyKey];
NSNumber *sFontSize = [aViewInfo objectForKey:kLabelAttributeFontPointSizeKey];
NSDictionary *sText = [aViewInfo objectForKey:kLabelAttributeTextKey];
NSString *sTextColor = [aViewInfo objectForKey:kLabelAttributeTextColorKey];
NSString *sShadowColor = [aViewInfo objectForKey:kLabelAttributeShadowColorKey];
NSString *sShadowOffset = [aViewInfo objectForKey:kLabelAttributeShadowOffsetKey];
NSNumber *sMultiline = [aViewInfo objectForKey:kLabelAttributeMultilineKey];
NSString *sAlignment = [aViewInfo objectForKey:kLabelAttributeAlignmentKey];
UILabel *sLabel = [[UILabel alloc] initWithFrame:aFrame];
[sLabel setFont:[self fontWithFontFamilyString:sFontFamily fontSize:sFontSize]];
[sLabel setTextColor:[self colorFromHTMLStyleString:sTextColor]];
if (sAlignment != nil)
{
[sLabel setTextAlignment:[self textAlignmentFromString:sAlignment]];
}
if (sShadowColor != nil)
{
[sLabel setShadowColor:[self colorFromHTMLStyleString:sShadowColor]];
}
if (sShadowOffset != nil)
{
CGSize sOffsetSize = CGSizeFromString(sShadowOffset);
[sLabel setShadowOffset:sOffsetSize];
}
if (sMultiline != nil && [sMultiline boolValue] == YES)
{
[sLabel setNumberOfLines:0];
}
[sLabel setText:[self textForCurrentLanguage:sText]];
return sLabel;
}
- (UIImageView *)imageViewWithFrame:(CGRect)aFrame attributes:(NSDictionary *)aViewInfo
{
NSString *sImageID = [aViewInfo objectForKey:kImageAttributeImageSourceKey];
if ([sImageID length] == 0)
return nil;
UIImage *sImage = [self imageForResourceIDString:sImageID];
if (sImage == nil)
return nil;
UIImageView *sImageView = [[UIImageView alloc] initWithFrame:aFrame];
[sImageView setImage:sImage];
return sImageView;
}
- (UIButton *)buttonWithFrame:(CGRect)aFrame attributes:(NSDictionary *)aViewInfo
{
NSDictionary *sTitle = [aViewInfo objectForKey:kButtonAttributeTitleKey];
NSDictionary *sTitleNormal = [sTitle objectForKey:kButtonAttributeStateNormalKey];
NSDictionary *sTitleHighlighted = [sTitle objectForKey:kButtonAttributeStateHighlightedKey];
NSString *sTitleFontFamily = [aViewInfo objectForKey:kButtonAttributeTitleFontFamilyKey];
NSNumber *sTitleFontSize = [aViewInfo objectForKey:kButtonAtrributeTitleFontPointSizeKey];
NSDictionary *sTitleColor = [aViewInfo objectForKey:kButtonAttributeTitleColor];
NSString *sNormalTitleColor = [sTitleColor objectForKey:kButtonAttributeStateNormalKey];
NSString *sHighlightedTitleColor = [sTitleColor objectForKey:kButtonAttributeStateHighlightedKey];
NSString *sTitleShadowColor = [aViewInfo objectForKey:kButtonAttributeTitleShadowColorKey];
NSString *sTitleShadowOffset = [aViewInfo objectForKey:kButtonAttributeTitleShadowOffsetKey];
NSDictionary *sBackgroundImage = [aViewInfo objectForKey:kButtonAttributeBackgroundImageKey];
NSString *sNormalBackgroundImage = [sBackgroundImage objectForKey:kButtonAttributeStateNormalKey];
NSString *sHighlightedBackgroundImage = [sBackgroundImage objectForKey:kButtonAttributeStateHighlightedKey];
NSString *sActionUri = [aViewInfo objectForKey:kButtonAttributeActionURIKey];
if (sTitle == nil || sTitleNormal == nil || sTitleFontFamily == nil || sTitleFontSize == nil || sNormalBackgroundImage == nil)
return nil;
UIButton *sButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sButton setFrame:aFrame];
[sButton setAdjustsImageWhenDisabled:NO];
[sButton setAdjustsImageWhenHighlighted:NO];
[sButton setTitle:[self textForCurrentLanguage:sTitleNormal] forState:UIControlStateNormal];
if (sTitleHighlighted != nil)
[sButton setTitle:[self textForCurrentLanguage:sTitleHighlighted] forState:UIControlStateHighlighted];
[sButton.titleLabel setFont:[self fontWithFontFamilyString:sTitleFontFamily fontSize:sTitleFontSize]];
UIColor *sButtonTitleColor = [UIColor blackColor];
if (sButtonTitleColor != nil)
sButtonTitleColor = [self colorFromHTMLStyleString:sNormalTitleColor];
[sButton setTitleColor:sButtonTitleColor forState:UIControlStateNormal];
if (sHighlightedTitleColor != nil)
[sButton setBackgroundImage:[self imageForResourceIDString:sNormalBackgroundImage] forState:UIControlStateNormal];
if (sHighlightedBackgroundImage != nil)
[sButton setBackgroundImage:[self imageForResourceIDString:sHighlightedBackgroundImage] forState:UIControlStateHighlighted];
if (sTitleShadowColor != nil)
{
UIColor *sColor = [self colorFromHTMLStyleString:sTitleShadowColor];
[sButton setTitleShadowColor:sColor forState:UIControlStateNormal];
[sButton setTitleShadowColor:sColor forState:UIControlStateHighlighted];
}
if (sTitleShadowOffset != nil)
{
CGSize sSize = CGSizeFromString(sTitleShadowOffset);
[sButton.titleLabel setShadowOffset:sSize];
}
[sButton setExclusiveTouch:YES];
[sButton handleControlEvent:UIControlEventTouchUpInside withBlock:^{
NSURL *aURL = [NSURL URLWithString:sActionUri];
if ([[UIApplication sharedApplication] canOpenURL:aURL] == YES)
[[UIApplication sharedApplication] openURL:aURL];
}];
return sButton;
}
- (UIView *)generalViewWithFrame:(CGRect)aFrame attributes:(NSDictionary *)aViewInfo
{
UIView *sView = [[UIView alloc] initWithFrame:aFrame];
return sView;
}
- (UIView *)viewForUIType:(NLUIType)aUIType withFrame:(CGRect)aViewFrame attributes:(NSDictionary *)aViewInfo
{
switch (aUIType)
{
case NLUITypeLabel:
return [self labelWithFrame:aViewFrame attributes:aViewInfo];
case NLUITypeImageView:
return [self imageViewWithFrame:aViewFrame attributes:aViewInfo];
case NLUITypeButton:
return [self buttonWithFrame:aViewFrame attributes:aViewInfo];
case NLUITypeView:
return [self generalViewWithFrame:aViewFrame attributes:aViewInfo];
default:
return nil;
}
}
- (UIView *)makeChildViewFromInfo:(NSDictionary *)aViewInfo
{
UIView *sView = nil;
NSString *sViewID = [aViewInfo objectForKey:kCommonAttributeIDKey];
NSString *sViewType = [aViewInfo objectForKey:kCommonAttributeTypeKey];
NSString *sViewFrame = [aViewInfo objectForKey:kCommonAttributeFrameKey];
NSString *sViewBackgroundColor = [aViewInfo objectForKey:kCommonAttributeBackgroundColorKey];
NSString *sViewAutoCenter = [aViewInfo objectForKey:kCommonAttributeAutoCenterKey];
NSArray *sViewsChildren = [aViewInfo objectForKey:kCommonAttributeChildrenKey];
if ([sViewID length] == 0 || [sViewType length] == 0 || [sViewFrame length] == 0)
return nil;
CGRect sFrame = CGRectFromString(sViewFrame);
NLUIType sUIType = [self uiTypeFromString:sViewType];
NSInteger sTag = [self tagForControlIDString:sViewID];
sView = [self viewForUIType:sUIType withFrame:sFrame attributes:aViewInfo];
if (sView == nil)
return nil;
if ([sViewsChildren count] > 0)
{
UIView *sChildView = [self makeChildViewFromInfo:aViewInfo];
[sView addSubview:sChildView];
}
UIColor *sBackgroundColor = nil;
if (sViewBackgroundColor != nil)
sBackgroundColor = [self colorFromHTMLStyleString:sViewBackgroundColor];
else
sBackgroundColor = [UIColor clearColor];
if (sViewAutoCenter != nil)
{
NSInteger sAutoCenterType = [self autoCenterTypeFromString:sViewAutoCenter];
if (sAutoCenterType & NLAutoCenterTypeHorizontal)
[sView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
else if (sAutoCenterType & NLAutoCenterTypeVertical)
[sView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
}
[sView setBackgroundColor:sBackgroundColor];
[sView setTag:sTag];
return sView;
}
- (UIView *)makeRootView
{
CGRect sRootFrame = CGRectZero;
UIView *sRootView = [[UIView alloc] init];
for (NSDictionary *sViewInfo in mViews)
{
UIView *sInnerView = [self makeChildViewFromInfo:sViewInfo];
if (sInnerView == nil)
continue;
sRootFrame = CGRectUnion(sRootFrame, [sInnerView frame]);
[sRootView addSubview:sInnerView];
}
[sRootView setFrame:sRootFrame];
return sRootView;
}
#pragma mark -
#pragma mark Public Operation
- (BOOL)load:(NSString *)aFilePath
{
if ([aFilePath length] == 0)
return NO;
[self reset];
NSError *sError = nil;
NSString *sContents = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:aFilePath]
encoding:NSUTF8StringEncoding
error:&sError];
if (sError != nil)
{
NSLog(@"%@", sError);
return NO;
}
NSDictionary *sParsingResult = [sContents objectFromJSONString];
if (sParsingResult != nil)
{
return [self parse:sParsingResult];
}
else
{
return NO;
}
}
- (UIView *)view
{
if ([mViews count] != 0)
return [self makeRootView];
else
return nil;
}
@end