EnkiUtils
Small library of classes that Enki Labs uses internally.
 All Classes Functions Properties Pages
EnkiUtilities.m
1 //
2 // EnkiUtilities.m
3 //
4 // Created by Paul Cezanne on 12/13/11.
5 // Copyright (c) 2011-2103 Enki Labs. All rights reserved.
6 //
7 
8 #import "EnkiUtilities.h"
9 
10 @implementation EnkiUtilities
11 
12 
13 + (float) pixelsForRetina:(float) pixels
14 {
15  if ([[UIScreen mainScreen] Enki_isRetina]) {
16  return pixels * 2;
17  } else {
18  return pixels;
19  }
20 }
21 
22 #pragma mark - numeric methods
23 
24 +(BOOL) AlmostEqualRelative:(float) A and:(float)B;
25 {
26  float maxRelDiEnki = FLT_EPSILON;
27 
28  // Calculate the diEnkierence.
29  float diEnki = fabs(A - B);
30  A = fabs(A);
31  B = fabs(B);
32  // Find the largest
33  float largest = (B > A) ? B : A;
34 
35  if (diEnki <= largest * maxRelDiEnki)
36  return true;
37  return false;
38 }
39 
40 
41 #pragma mark - Grouped TableCell methods
42 static UIColor *EnkiUtiltiesBackgroundCellColor;
43 
45 + (UIColor *)defaultBackgroundColor:(UITableViewCell *)inCell
46 {
47 
48  if (NULL == EnkiUtiltiesBackgroundCellColor) {
49  // iOS 4 and iOS5 have diEnkierent color backgrounds for tabbed group dialogs
50  // hence this gross hack. Must put it in a utility class someday.
51  // see http://stackoverflow.com/questions/8405644/what-is-the-default-background-color-of-a-uitableviewstylegrouped-styled-uitable
52 
53  NSString *version = [[UIDevice currentDevice] systemVersion];
54  NSString *firstLetter = [version substringToIndex:1];
55 
56  if ([firstLetter isEqual: @"4"]) {
57  EnkiUtiltiesBackgroundCellColor = [UIColor whiteColor];
58  } else {
59  EnkiUtiltiesBackgroundCellColor = [UIColor colorWithRed:(0xF7/255.0)green:(0xF7/255.0) blue:(0xF7/255.0) alpha:1.0];
60  }
61  }
62 
63  return EnkiUtiltiesBackgroundCellColor;
64 }
65 
66 
67 #pragma mark - keyboard methods
68 
69 
70 + (void)keyboardWasShown:(NSNotification*)aNotification
71  view:(UIViewController *) view
72  scrollView:(UIScrollView *)scrollView
73  activeField:(UIView *) activeField
74  activeCell:(UITableViewCell *) activeCell
75 {
76  NSDictionary* info = [aNotification userInfo];
77  CGRect rawKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
78 
79  CGRect properlyRotatedCoords = [view.view.window convertRect:rawKeyboardRect toView:view.view.window.rootViewController.view];
80 
81  CGSize kbSize = properlyRotatedCoords.size;
82 
83 
84  UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
85  scrollView.contentInset = contentInsets;
86  scrollView.scrollIndicatorInsets = contentInsets;
87 
88  CGRect viewRect = view.view.frame;
89 
90  if (nil != activeCell) {
91  // If active text field is hidden by keyboard, scroll it so it's visible
92  // Your application might not need or want this behavior.
93  viewRect.size.height -= kbSize.height;
94 
95  CGPoint origin = activeCell.frame.origin;
96  origin.x += activeField.frame.origin.x;
97  origin.y += activeField.frame.origin.y;
98  origin.y -= scrollView.contentOffset.y;
99 
100  if (!CGRectContainsPoint(viewRect, origin) ) {
101  CGFloat scrollAmount = activeCell.frame.origin.y + activeField.frame.origin.y - 30; // the -30 is just to make it look better
102  CGPoint scrollPoint = CGPointMake(0.0, scrollAmount);
103 
104  [scrollView setContentOffset:scrollPoint animated:YES];
105  }
106  } else {
107 
108  CGRect fieldRect = activeField.frame;
109 
110  float distanceFromBottom = (viewRect.origin.y + viewRect.size.height) - (fieldRect.origin.y + fieldRect.size.height);
111  float jumpNeeded = kbSize.height - distanceFromBottom;
112 
113  // the -30 is just to make it look better
114  [scrollView setContentOffset:CGPointMake(0,jumpNeeded +30) animated:YES];
115  }
116 
117 }
118 
119 
120 
121 @end
122