-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+iOsUtils.m
242 lines (205 loc) · 4.6 KB
/
UIView+iOsUtils.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
//
// MiscUtilsUI.m
// Home Credit
//
// Created by Konstantin Gontarenko on 11/4/11.
// Copyright (c) 2011 iDa Mobile. All rights reserved.
//
#import "UIView+iOsUtils.h"
#import "UIDevice+iOsUtils.h"
#import "CustomFonts.h"
@implementation UIView(iOsUtils)
-(CGFloat)x
{
return self.frame.origin.x;
}
-(void)setX:(CGFloat)x
{
CGRect seflFrame = self.frame;
seflFrame.origin.x = x;
self.frame = seflFrame;
}
-(CGFloat)y
{
return self.frame.origin.y;
}
-(void)setY:(CGFloat)y
{
CGRect seflFrame = self.frame;
seflFrame.origin.y = y;
self.frame = seflFrame;
}
-(CGFloat)width
{
return self.frame.size.width;
}
-(void)setWidth:(CGFloat)width
{
CGRect seflFrame = self.frame;
seflFrame.size.width = width;
self.frame = seflFrame;
}
-(CGFloat)height
{
return self.frame.size.height;
}
-(void)setHeight:(CGFloat) height
{
CGRect seflFrame = self.frame;
seflFrame.size.height = height;
self.frame = seflFrame;
}
-(CGFloat)leftEdge
{
return self.x;
}
-(void)setLeftEdge:(CGFloat)newLeft
{
CGRect rc = self.frame;
CGFloat right = rc.size.width + rc.origin.x;
rc.origin.x = newLeft;
rc.size.width = ( right - newLeft );
rc = CGRectStandardize( rc );
self.frame = rc;
}
-(CGFloat)rightEdge
{
CGRect rc = self.frame;
return rc.size.width + rc.origin.x;
}
-(void)setRightEdge:(CGFloat)newRight
{
CGRect rc = self.frame;
rc.size.width = newRight - rc.origin.x;
rc = CGRectStandardize( rc );
self.frame = rc;
}
-(CGFloat)topEdge
{
return self.frame.origin.y;
}
-(void)setTopEdge:(CGFloat)newTop
{
CGRect rc = self.frame;
CGFloat bottom = rc.size.height + rc.origin.y;
rc.origin.y = newTop;
rc.size.height = ( bottom - newTop );
rc = CGRectStandardize( rc );
self.frame = rc;
}
-(CGFloat)bottomEdge
{
CGRect rc = self.frame;
return rc.size.height + rc.origin.y;
}
-(void)setBottomEdge:(CGFloat)newBottom
{
CGRect rc = self.frame;
rc.size.height = newBottom - rc.origin.y;
rc = CGRectStandardize( rc );
self.frame = rc;
}
-(CGSize)size
{
return self.frame.size;
}
-(void)setSize:(CGSize)sz
{
CGRect rc = self.frame;
rc.size = sz;
self.frame = rc;
}
-(CGRect)convertBoundsToView:(UIView*)view
{
return [self convertRect:self.bounds toView:view];
}
-(void)centerHorizontallyInContainer
{
CGRect rcContainer = self.superview.bounds;
self.x = floorf( ( rcContainer.size.width - self.width ) / 2 );
}
-(void)centerVerticallyInContainer
{
CGRect rcContainer = self.superview.bounds;
self.y = floorf( ( rcContainer.size.height - self.height ) / 2 );
}
-(UIView*)findSubviewRecursivelyOfClass:(Class)subviewClass
{
if( [self isKindOfClass:subviewClass] ) {
return self;
} else {
for( UIView* child in self.subviews ) {
UIView* result = [child findSubviewRecursivelyOfClass:subviewClass];
if( result ) {
return result;
}
}
return nil;
}
}
-(UIResponder*)findFirstResponderRecursively
{
if( self.isFirstResponder ) {
return self;
} else {
for( UIView* child in self.subviews ) {
UIResponder* responder = [child findFirstResponderRecursively];
if( responder ) {
return responder;
}
}
return nil;
}
}
-(void)resignFirstResponderRecursively
{
[[self findFirstResponderRecursively] resignFirstResponder];
}
-(void)heightToFit
{
self.height = [self heightThatFits];
}
-(CGFloat)heightThatFits
{
return [self sizeThatFits:self.bounds.size].height;
}
-(CGFloat)widthThatFits
{
return [self sizeThatFits:self.bounds.size].width;
}
-(void)widthToFit
{
self.width = [self widthThatFits];
}
+(instancetype)loadFromNibNamed:(NSString*)nibName owner:(id)owner options:(NSDictionary*)opts
{
NSString* deviceSpecific = nil;
if( [UIDevice isPad] ) {
deviceSpecific = [nibName stringByAppendingString:@"~iPad"];
} else {
deviceSpecific = [nibName stringByAppendingString:@"~iPhone"];
}
NSString* chosenPath = nil;
if( [[NSBundle mainBundle] pathForResource:deviceSpecific ofType:@"nib"] ) {
chosenPath = deviceSpecific;
} else if( [[NSBundle mainBundle] pathForResource:nibName ofType:@"nib"] ) {
chosenPath = nibName;
} else {
DLog( @"Nib named %@ not found", nibName );
return nil;
}
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:chosenPath owner:owner options:opts];
for( NSObject* currentObject in nibViews ) {
if( [currentObject isKindOfClass:self] ) {
[CustomFonts replaceFontsOnView:(UIView*)currentObject];
return (UIView*)currentObject;
}
}
DLog( @"Nib named %@ doesn't contain a view of class %@", nibName, NSStringFromClass( self ) );
return nil;
}
+(instancetype)loadFromNib
{
return [self loadFromNibNamed:NSStringFromClass(self) owner:nil options:nil];
}
@end