forked from gsempe/JFCommon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJFViewPositionUtil.m
executable file
·516 lines (440 loc) · 16.2 KB
/
JFViewPositionUtil.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
//
// JFViewPositionUtil.m
// JFCommon
//
// Created by Jason Fuerstenberg on 11/07/04.
// Copyright 2011 Jason Fuerstenberg
//
// http://www.jayfuerstenberg.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "JFViewPositionUtil.h"
@implementation JFViewPositionUtil
#pragma mark - Superview centric alignment methods
/*
* Vertically centers the provided view at x within the superview.
*
* Params
* view The view to vertically center
* superview The super view within which the view will be vertically centered.
* x The horizontal offset from which the view will be vertically centered.
*/
+ (void) verticallyCenterView: (VIEW_TYPE *) view withinSuperview: (VIEW_TYPE *) superview atX: (CGFloat) x {
CGFloat superViewCenter = superview.bounds.size.height / 2.0f;
CGFloat viewCenter = view.bounds.size.height / 2.0f;
CGFloat y = superViewCenter - viewCenter;
RECT_TYPE frame = [view frame];
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
RECT_TYPE newFrame = CGRectMake(x,
y,
frame.size.width,
frame.size.height);
[view setFrame: newFrame];
#else
RECT_TYPE newFrame = NSMakeRect(x,
y,
frame.size.width,
frame.size.height);
[view setFrame: newFrame];
#endif
}
/*
* Centers the provided view at y within the superview.
*
* Params
* view The view to center
* superview The super view within which the view will be centered.
* y The vertical offset from which the view will be centered.
*/
+ (void) centerView: (VIEW_TYPE *) view withinSuperView: (VIEW_TYPE *) superView atY: (CGFloat) y {
CGFloat superViewCenter = superView.bounds.size.width / 2.0f;
CGFloat viewCenter = view.bounds.size.width / 2.0f;
CGFloat x = superViewCenter - viewCenter;
RECT_TYPE frame = [view frame];
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
RECT_TYPE newFrame = CGRectMake(x,
y,
frame.size.width,
frame.size.height);
[view setFrame: newFrame];
#else
RECT_TYPE newFrame = NSMakeRect(x,
y,
frame.size.width,
frame.size.height);
[view setFrame: newFrame];
#endif
}
/*
* Centers the provided view within the superview.
*
* Params
* view The view to center
* superview The super view within which the view will be centered.
*/
+ (void) centerView: (VIEW_TYPE *) view withinSuperView: (VIEW_TYPE *) superView {
CGFloat superViewCenterX = superView.bounds.size.width / 2.0f;
CGFloat viewCenterX = view.bounds.size.width / 2.0f;
CGFloat x = superViewCenterX - viewCenterX;
CGFloat superViewCenterY = superView.bounds.size.height / 2.0f;
CGFloat viewCenterY = view.bounds.size.height / 2.0f;
CGFloat y = superViewCenterY - viewCenterY;
RECT_TYPE frame = [view frame];
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
RECT_TYPE newFrame = CGRectMake(x,
y,
frame.size.width,
frame.size.height);
[view setFrame: newFrame];
#else
RECT_TYPE newFrame = NSMakeRect(x,
y,
frame.size.width,
frame.size.height);
[view setFrame: newFrame];
#endif
}
/*
* Centers the provided view within the super view and below a sibling distanced by the margin/offset.
*
* Params
* view The view to center.
* superview The super view within which the view will be centered.
* siblingView The sibling view under which the view will appear.
* margin The margin/offset between the view and the sibling view.
*/
+ (void) centerView: (VIEW_TYPE *) view withinSuperView: (VIEW_TYPE *) superView belowSiblingView: (VIEW_TYPE *) siblingView withMargin: (CGFloat) margin {
CGFloat superViewCenter = superView.bounds.size.width / 2.0f;
CGFloat siblingCenter = siblingView.frame.origin.x + (siblingView.frame.size.width / 2.0f);
CGPoint marginPoint = CGPointMake(superViewCenter - siblingCenter, margin);
[JFViewPositionUtil snapAnchorPoint: JFAnchorPointTop
ofView: view
withMargin: marginPoint
fromAnchorPoint: JFAnchorPointBottom
ofSiblingView: siblingView];
}
#pragma mark - Sibling view centric alignment methods
/*
* Aligns the provided view below a sibling view distanced by the margin/offset and positions it at x.
*
* Params
* view The view to left align.
* x The new x coordinate of the view.
* siblingView The sibling view under which the view will appear.
* margin The margin/offset between the view and the sibling view.
*/
+ (void) alignView: (VIEW_TYPE *) view atX: (CGFloat) x belowSiblingView: (VIEW_TYPE *) siblingView withMargin: (CGFloat) margin {
RECT_TYPE siblingFrame = [siblingView frame];
CGFloat y = siblingFrame.origin.y + siblingFrame.size.height + margin;
RECT_TYPE frame = [view frame];
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
RECT_TYPE newFrame = CGRectMake(x, y, frame.size.width, frame.size.height);
[view setFrame: newFrame];
#else
RECT_TYPE newFrame = NSMakeRect(x, y, frame.size.width, frame.size.height);
[view setFrame: newFrame];
#endif
}
/*
* Left aligns the provided view below a sibling view distanced by the margin/offset.
*
* Params
* view The view to left align.
* siblingView The sibling view under which the view will appear.
* margin The margin/offset between the view and the sibling view.
*/
+ (void) leftAlignView: (VIEW_TYPE *) view belowSiblingView: (VIEW_TYPE *) siblingView withMargin: (CGFloat) margin {
CGPoint marginPoint = CGPointMake(0.0f, margin);
[JFViewPositionUtil snapAnchorPoint: JFAnchorPointTopLeft
ofView: view
withMargin: marginPoint
fromAnchorPoint: JFAnchorPointBottomLeft
ofSiblingView: siblingView];
}
/*
* Right aligns the provided view below a sibling view distanced by the margin/offset.
*
* Params
* view The view to right align.
* siblingView The sibling view under which the view will appear.
* margin The margin/offset between the view and the sibling view.
*/
+ (void) rightAlignView: (VIEW_TYPE *) view belowSiblingView: (VIEW_TYPE *) siblingView withMargin: (CGFloat) margin {
CGPoint marginPoint = CGPointMake(0.0f, margin);
[JFViewPositionUtil snapAnchorPoint: JFAnchorPointTopRight
ofView: view
withMargin: marginPoint
fromAnchorPoint: JFAnchorPointBottomRight
ofSiblingView: siblingView];
}
/*
* Centers the provided view below a sibling view distanced by the margin/offset.
*
* Params
* view The view to center.
* siblingView The sibling view under which the view will appear.
* margin The margin/offset between the view and the sibling view.
*/
+ (void) centerView: (VIEW_TYPE *) view belowSiblingView: (VIEW_TYPE *) siblingView withMargin: (CGFloat) margin {
CGPoint marginPoint = CGPointMake(0.0f, margin);
[JFViewPositionUtil snapAnchorPoint: JFAnchorPointTop
ofView: view
withMargin: marginPoint
fromAnchorPoint: JFAnchorPointBottom
ofSiblingView: siblingView];
}
/*
* Vertically centers the provided view to the left of a sibling view distanced by the margin/offset.
*
* Params
* view The view to vertically center.
* siblingView The sibling view to the left of which the view will appear.
* margin The margin/offset between the view and the sibling view.
*/
+ (void) verticallyCenterView: (VIEW_TYPE *) view leftOfSiblingView: (VIEW_TYPE *) siblingView withMargin: (CGFloat) margin {
CGPoint marginPoint = CGPointMake(-margin, 0.0f);
[JFViewPositionUtil snapAnchorPoint: JFAnchorPointRight
ofView: view
withMargin: marginPoint
fromAnchorPoint: JFAnchorPointLeft
ofSiblingView: siblingView];
}
/*
* Vertically centers the provided view to the right of a sibling view distanced by the margin/offset.
*
* Params
* view The view to vertically center.
* siblingView The sibling view to the right of which the view will appear.
* margin The margin/offset between the view and the sibling view.
*/
+ (void) verticallyCenterView: (VIEW_TYPE *) view rightOfSiblingView: (VIEW_TYPE *) siblingView withMargin: (CGFloat) margin {
CGPoint marginPoint = CGPointMake(margin, 0.0f);
[JFViewPositionUtil snapAnchorPoint: JFAnchorPointLeft
ofView: view
withMargin: marginPoint
fromAnchorPoint: JFAnchorPointRight
ofSiblingView: siblingView];
}
#pragma mark - Lower level snapping methods
/*
* Snaps the provided view by the anchor point to the sibling view's anchor point.
* If either view is nil the method will perform no anchoring.
*
* Params
* anchorPoint The view's anchor point.
* view The view to anchor.
* siblingAnchorPoint The sibling view's anchor point.
* siblingView The sibling view.
*/
+ (void) snapAnchorPoint: (JFAnchorPoint) anchorPoint ofView: (VIEW_TYPE *) view toAnchorPoint: (JFAnchorPoint) siblingAnchorPoint ofSiblingView: (VIEW_TYPE *) siblingView {
if (view == nil || siblingView == nil) {
// At least one of the views is not usable.
return;
}
CGPoint siblingPoint = [JFViewPositionUtil pointForAnchorPoint: siblingAnchorPoint
ofView: siblingView];
CGPoint point = [JFViewPositionUtil pointForAnchorPoint: anchorPoint
ofView: view];
[JFViewPositionUtil moveView: view
bySnappingPoint: point
toOtherPoint: siblingPoint];
}
/*
* Snaps the provided view by the anchor point to the sibling view's anchor point.
* If either view is nil the method will perform no anchoring.
*
* Params
* anchorPoint The view's anchor point.
* view The view to anchor.
* margin The margin/offset from the sibling anchor point.
* siblingAnchorPoint The sibling view's anchor point.
* siblingView The sibling view.
*/
+ (void) snapAnchorPoint: (JFAnchorPoint) anchorPoint ofView: (VIEW_TYPE *) view withMargin: (CGPoint) margin fromAnchorPoint: (JFAnchorPoint) siblingAnchorPoint ofSiblingView: (VIEW_TYPE *) siblingView {
if (view == nil || siblingView == nil) {
// At least one of the views is not usable.
return;
}
CGPoint siblingPoint = [JFViewPositionUtil pointForAnchorPoint: siblingAnchorPoint
ofView: siblingView];
CGPoint point = [JFViewPositionUtil pointForAnchorPoint: anchorPoint
ofView: view];
[JFViewPositionUtil moveView: view
bySnappingPoint: point
toOtherPoint: siblingPoint
withMargin: margin];
}
/*
* Moves the provided view by snapping its point to another point.
*
* Params
* view The view to move.
* point The point being snapped.
* otherPoint The point against which the first point will be snapped.
*/
+ (void) moveView: (VIEW_TYPE *) view bySnappingPoint: (CGPoint) point toOtherPoint: (CGPoint) otherPoint {
CGFloat xDiff = otherPoint.x - point.x;
CGFloat yDiff = otherPoint.y - point.y;
RECT_TYPE frame = [view frame];
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
RECT_TYPE newFrame = CGRectMake(frame.origin.x += xDiff,
frame.origin.y += yDiff,
frame.size.width,
frame.size.height);
[view setFrame: newFrame];
#else
RECT_TYPE newFrame = NSMakeRect(frame.origin.x += xDiff,
frame.origin.y += yDiff,
frame.size.width,
frame.size.height);
[view setFrame: newFrame];
#endif
}
/*
* Moves the provided view by snapping its point to another while also considering the margin/offset.
*
* Params
* view The view to move.
* point The point being snapped.
* otherPoint The point against which the first point will be snapped.
* margin The margin/offset from the other point to consider when snapping the point of the view.
*/
+ (void) moveView: (VIEW_TYPE *) view bySnappingPoint: (CGPoint) point toOtherPoint: (CGPoint) otherPoint withMargin: (CGPoint) margin {
CGFloat xDiff = otherPoint.x - point.x;
CGFloat yDiff = otherPoint.y - point.y;
RECT_TYPE frame = [view frame];
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
RECT_TYPE newFrame = CGRectMake((frame.origin.x += xDiff) + margin.x,
(frame.origin.y += yDiff) + margin.y,
frame.size.width,
frame.size.height);
[view setFrame: newFrame];
#else
RECT_TYPE newFrame = NSMakeRect((frame.origin.x += xDiff) + margin.x,
(frame.origin.y += yDiff) + margin.y,
frame.size.width,
frame.size.height);
[view setFrame: newFrame];
#endif
}
#pragma mark - Point retrieval method
/*
* Returns the point within the view designated by the provided anchor point.
* The provided view must not be nil.
*
* Params
* anchorPoint The view's anchor point.
* view The view whose point is being returned.
*
* Return
* A CGPoint struct.
*/
+ (CGPoint) pointForAnchorPoint: (JFAnchorPoint) anchorPoint ofView: (VIEW_TYPE *) view {
RECT_TYPE frame = [view frame];
CGFloat x = 0.0f;
CGFloat y = 0.0f;
switch (anchorPoint) {
case JFAnchorPointTopLeft:
x = frame.origin.x;
y = frame.origin.y;
break;
case JFAnchorPointTop:
x = (frame.size.width / 2.0f) + frame.origin.x;
y = frame.origin.y;
break;
case JFAnchorPointTopRight:
x = frame.size.width + frame.origin.x;
y = frame.origin.y;
break;
case JFAnchorPointLeft:
x = frame.origin.x;
y = (frame.size.height / 2.0f) + frame.origin.y;
break;
case JFAnchorPointCenter:
x = (frame.size.width / 2.0f) + frame.origin.x;
y = (frame.size.height / 2.0f) + frame.origin.y;
break;
case JFAnchorPointRight:
x = frame.size.width + frame.origin.x;
y = (frame.size.height / 2.0f) + frame.origin.y;
break;
case JFAnchorPointBottomLeft:
x = frame.origin.x;
y = frame.size.height + frame.origin.y;
break;
case JFAnchorPointBottom:
x = (frame.size.width / 2.0f) + frame.origin.x;
y = frame.size.height + frame.origin.y;
break;
case JFAnchorPointBottomRight:
x = frame.size.width + frame.origin.x;
y = frame.size.height + frame.origin.y;
break;
}
return CGPointMake(x, y);
}
#pragma mark - Extra methods
/*
* Sets the height of the provided view to fit all its subviews and adding the margin.
*
* Params
* superview The view to resize.
* margin The margin to add to the eventual height.
*/
+ (void) setHeightOfSuperview: (VIEW_TYPE *) superview toFitAllSubviewsWithMargin: (CGFloat) margin {
CGFloat deepestY = 0.0f;
NSArray *subviews = [superview subviews];
for (VIEW_TYPE *subview in subviews) {
CGFloat y = subview.frame.origin.y + subview.frame.size.height;
if (y > deepestY) {
deepestY = y;
}
}
RECT_TYPE currentFrame = superview.frame;
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
RECT_TYPE newFrame = CGRectMake(currentFrame.origin.x, currentFrame.origin.y, currentFrame.size.width, deepestY + margin);
[superview setFrame: newFrame];
#else
RECT_TYPE newFrame = NSMakeRect(currentFrame.origin.x, currentFrame.origin.y, currentFrame.size.width, deepestY + margin);
[superview setFrame: newFrame];
#endif
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
if ([superview isKindOfClass: [UIScrollView class]]) {
// The superview being resized is a scroll view so also set its content size to match its actual size.
UIScrollView *scrollView = (UIScrollView *) superview;
[scrollView setContentSize: newFrame.size];
}
#endif
}
/*
* Snaps the provided view to neatly discreet coordinates.
*
* Params
* view The view whose coordinates will be snapped.
*/
+ (void) snapViewToDiscreteCoordinates: (VIEW_TYPE *) view {
if (view == nil) {
return;
}
CGPoint point = [JFViewPositionUtil pointForAnchorPoint: JFAnchorPointTopLeft
ofView: view];
point.x = round(point.x);
point.y = round(point.y);
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
[view setFrame: CGRectMake(point.x, point.y, view.frame.size.width, view.frame.size.height)];
#else
[view setFrame: NSMakeRect(point.x, point.y, view.frame.size.width, view.frame.size.height)];
#endif
}
@end