-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.m
164 lines (127 loc) · 4.45 KB
/
Utility.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
//
// Utility.m
// daily Expenses
//
// Created by Renan Veloso Silva on 29/07/13.
// Copyright (c) 2013 renan veloso silva. All rights reserved.
//
#import "Utility.h"
#import "ItemFilter.h"
@implementation Utility
static id _instance;
+ (Utility*) sharedInstance{
@synchronized(self){
if (!_instance) {
_instance = [[self alloc] init];
}
}
return _instance;
}
-(id)init{
self = [super init];
if (self) {
monthList = [[ItemFilter sharedInstance] monthList];
}
return self;
}
#pragma mark - validate
- (BOOL)isEmptyString:(NSString *) aString{
if ((NSNull *) aString == [NSNull null]) {
return YES;
}
if (aString == nil) {
return YES;
} else if ([aString length] == 0) {
return YES;
} else {
aString = [aString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([aString length] == 0) {
return YES;
}
}
return NO;
}
-(BOOL)stringToBool:(NSString*)value{
if ([value isEqualToString:@"1"]) {
return YES;
}else {
return NO;
}
}
-(NSString*)boolToString:(BOOL)value{
if (value) {
return @"1";
}else {
return @"0";
}
}
#pragma mark - get methods
-(NSString*)getCurrentDate{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy"];
return [formatter stringFromDate:[NSDate date]];
}
-(NSString*)getDayBefore:(NSString*)currentDate{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDateComponents *component = [[NSDateComponents alloc] init];
[component setDay:-1];
[formatter setDateFormat:@"dd/MM/yyyy"];
NSDate *currentDay= [formatter dateFromString:currentDate];
NSDate *dayBefore = [[NSCalendar currentCalendar] dateByAddingComponents:component toDate:currentDay options:0];
return [formatter stringFromDate:dayBefore];
}
-(NSString*)getDayAfter:(NSString*)currentDate{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDateComponents *component = [[NSDateComponents alloc] init];
[component setDay:+1];
[formatter setDateFormat:@"dd/MM/yyyy"];
NSDate *currentDay= [formatter dateFromString:currentDate];
NSDate *dayAfter = [[NSCalendar currentCalendar] dateByAddingComponents:component toDate:currentDay options:0];
return [formatter stringFromDate:dayAfter];
}
-(NSString*)getMonthByDate:(NSString*)dateS{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSDate* dateFromString = [dateFormatter dateFromString:dateS];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:(NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit) fromDate:dateFromString];
NSInteger monthIndex = [components month];
NSString* monthStr = [monthList objectAtIndex:(monthIndex-1)];
return monthStr;
}
#pragma mark - remove
-(void)removeElementsFromView:(UIView*)viewR{
for (id object in [viewR subviews]) {
[object removeFromSuperview];
}
}
#pragma mark - setItem position
- (void)setItem:(UIView*)subView inCenterView:(UIView*)viewR padLeft:(CGFloat)l padTop:(CGFloat)t padRight:(CGFloat)r padBottom:(CGFloat)b{
CGRect CGItem = subView.frame;
CGRect CGView = viewR.frame;
CGFloat w = CGItem.size.width;
CGFloat h = CGItem.size.height;
CGFloat x = (CGView.size.width/2) - (CGItem.size.width/2) + l - r;
CGFloat y = (CGView.size.height/2) - (CGItem.size.height/2) + t - b;
CGRect newRect = CGRectMake(x, y, w, h);
subView.frame = newRect;
}
- (void)setItem:(UIView*)subView inView:(UIView*)viewR side:(NSString*)side UpDown:(NSString*)updown padLeft:(CGFloat)l padTop:(CGFloat)t padRight:(CGFloat)r padBottom:(CGFloat)b{
CGRect CGItem = subView.frame;
CGRect CGView = viewR.frame;
CGFloat newX;
CGFloat newY;
if([updown isEqualToString:@"up"]){
newY = CGView.origin.y - CGItem.size.height + t - b;
}else{
newY = CGView.origin.y + CGView.size.height + t - b;
}
if([side isEqualToString:@"left"]){
newX = CGView.origin.x +l - r;
}else{
newX = CGView.origin.x + CGView.size.width - CGItem.size.width + l - r;
}
CGRect newRect = CGRectMake(newX, newY, CGItem.size.width, CGItem.size.height);
subView.frame = newRect;
}
@end