├── .gitignore ├── CAAnimation ├── CAAnimationEGOHelper.h └── CAAnimationEGOHelper.m ├── Environment ├── EnvironmentHelper.h └── EnvironmentHelper.m ├── ExceptionHelper ├── ExceptionHelper.h └── ExceptionHelper.m ├── NSApplication ├── NSApplicationHelper.h └── NSApplicationHelper.m ├── NSArray ├── NSArrayHelper.h └── NSArrayHelper.m ├── NSData ├── NSDataHelper.h └── NSDataHelper.m ├── NSDate ├── NSDateHelper.h └── NSDateHelper.m ├── NSDictionary ├── NSDictionaryHelper.h └── NSDictionaryHelper.m ├── NSGradient ├── NSGradientHelper.h └── NSGradientHelper.m ├── NSObject ├── NSObjectHelper.h └── NSObjectHelper.m ├── NSString ├── NSStringHelper.h └── NSStringHelper.m ├── NSTask ├── NSTaskHelper.h └── NSTaskHelper.m ├── NSURL ├── NSURLHelper.h └── NSURLHelper.m ├── NSWorkspace ├── NSWorkspaceHelper.h └── NSWorkspaceHelper.m ├── NSXML ├── NSXMLElement.h └── NSXMLElement.m ├── README.textile ├── UIAlertView ├── UIAlertViewHelper.h └── UIAlertViewHelper.m ├── UIApplication ├── UIApplicationHelper.h └── UIApplicationHelper.m ├── UIColor ├── UIColorHelper.h └── UIColorHelper.m ├── UIDevice ├── UIDeviceHelper.h └── UIDeviceHelper.m ├── UIImage ├── UIImageHelper.h └── UIImageHelper.m ├── UITableView ├── UITableViewContentUnavailableView.h ├── UITableViewContentUnavailableView.m ├── UITableViewHelper.h ├── UITableViewHelper.m ├── UITableViewUpdatingView.h └── UITableViewUpdatingView.m ├── UITableViewController ├── UITableViewControllerHelper.h └── UITableViewControllerHelper.m ├── UIToolbar ├── UIToolbarEGOHelper.h └── UIToolbarEGOHelper.m ├── UIView ├── UIViewHelper.h └── UIViewHelper.m └── UIViewController ├── UIViewControllerHelper.h └── UIViewControllerHelper.m /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /CAAnimation/CAAnimationEGOHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // CAAnimationEGOHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Devin Doty on 5/16/10. 6 | // Copyright 2010 enormego. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | 30 | @interface CAAnimation (EGOHelper) 31 | + (CAKeyframeAnimation*)popInAnimation; 32 | @end 33 | 34 | @interface UIView (CAAnimationEGOHelper) 35 | - (void)popInAnimated; 36 | @end 37 | 38 | @interface CALayer (CAAnimationEGOHelper) 39 | - (void)popInAnimated; 40 | @end -------------------------------------------------------------------------------- /CAAnimation/CAAnimationEGOHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIToolbarEGOHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Devin Doty on 5/16/10. 6 | // Copyright 2010 enormego. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "CAAnimationEGOHelper.h" 28 | 29 | @implementation CAAnimation (EGOHelper) 30 | 31 | + (CAKeyframeAnimation*)popInAnimation { 32 | CAKeyframeAnimation* animation = [CAKeyframeAnimation animation]; 33 | 34 | animation.values = [NSArray arrayWithObjects: 35 | [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)], 36 | [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.05, 1.05, 1.0)], 37 | [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)], 38 | nil]; 39 | 40 | animation.duration = 0.3f; 41 | return animation; 42 | } 43 | 44 | @end 45 | 46 | @implementation UIView (CAAnimationEGOHelper) 47 | 48 | - (void)popInAnimated { 49 | [self.layer popInAnimated]; 50 | } 51 | 52 | @end 53 | 54 | @implementation CALayer (CAAnimationEGOHelper) 55 | 56 | - (void)popInAnimated { 57 | [self addAnimation:[CAAnimation popInAnimation] forKey:@"transform"]; 58 | } 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /Environment/EnvironmentHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // EnvironmentHelper.h 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 10/9/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | @interface EnvironmentHelper : NSObject { 30 | 31 | } 32 | 33 | 34 | #if TARGET_OS_IPHONE 35 | + (BOOL)isOS2; 36 | + (BOOL)isOS3; 37 | #else 38 | + (BOOL)isTiger; 39 | 40 | + (BOOL)isLeopard; 41 | + (BOOL)isAtLeastLeopard; 42 | 43 | + (BOOL)isSnowLeopard; 44 | + (BOOL)isAtLeastSnowLeopard; 45 | #endif 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /Environment/EnvironmentHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // EnvironmentHelper.m 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 10/9/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "EnvironmentHelper.h" 28 | 29 | 30 | @implementation EnvironmentHelper 31 | 32 | #if TARGET_OS_IPHONE 33 | 34 | + (BOOL)isOS2 { 35 | return [[UIDevice currentDevice].systemVersion hasPrefix:@"2."]; 36 | } 37 | 38 | + (BOOL)isOS3 { 39 | return [[UIDevice currentDevice].systemVersion hasPrefix:@"3."]; 40 | } 41 | 42 | #else 43 | 44 | + (void)systemVersionMajor:(SInt32*)systemVersionMajor minor:(SInt32*)systemVersionMinor bugFix:(SInt32*)systemVersionBugFix { 45 | Gestalt(gestaltSystemVersionMajor, &*systemVersionMajor); 46 | Gestalt(gestaltSystemVersionMinor, &*systemVersionMinor); 47 | Gestalt(gestaltSystemVersionBugFix, &*systemVersionBugFix); 48 | } 49 | 50 | + (NSString*)systemVersion { 51 | SInt32 systemVersionMajor; 52 | SInt32 systemVersionMinor; 53 | SInt32 systemVersionBugFix; 54 | 55 | [self systemVersionMajor:&systemVersionMajor minor:&systemVersionMinor bugFix:&systemVersionBugFix]; 56 | 57 | return [NSString stringWithFormat:@"%u.%u.%u", systemVersionMajor, systemVersionMinor, systemVersionBugFix]; 58 | } 59 | 60 | + (BOOL)isTiger { 61 | return [[self systemVersion] hasPrefix:@"10.4."]; 62 | } 63 | 64 | + (BOOL)isLeopard { 65 | return [[self systemVersion] hasPrefix:@"10.5."]; 66 | } 67 | 68 | + (BOOL)isSnowLeopard { 69 | return [[self systemVersion] hasPrefix:@"10.6."]; 70 | } 71 | 72 | + (BOOL)isAtLeastLeopard { 73 | SInt32 systemVersionMajor; 74 | SInt32 systemVersionMinor; 75 | SInt32 systemVersionBugFix; 76 | 77 | [self systemVersionMajor:&systemVersionMajor minor:&systemVersionMinor bugFix:&systemVersionBugFix]; 78 | 79 | return systemVersionMajor >= 10 && systemVersionMinor >= 5; 80 | } 81 | 82 | + (BOOL)isAtLeastSnowLeopard { 83 | SInt32 systemVersionMajor; 84 | SInt32 systemVersionMinor; 85 | SInt32 systemVersionBugFix; 86 | 87 | [self systemVersionMajor:&systemVersionMajor minor:&systemVersionMinor bugFix:&systemVersionBugFix]; 88 | 89 | return systemVersionMajor >= 10 && systemVersionMinor >= 6; 90 | } 91 | 92 | #endif 93 | 94 | @end 95 | -------------------------------------------------------------------------------- /ExceptionHelper/ExceptionHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // ExceptionHelper.h 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 3/13/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #ifdef ENABLE_EXCEPTIONS 28 | #import 29 | 30 | @interface ExceptionHelper : NSObject { 31 | 32 | } 33 | 34 | // This will output a command to console to run in atos to get a full stacktrace 35 | // It will also display any available information with the exception 36 | + (void)generateStackTraceForException:(NSException*)exception; 37 | 38 | @end 39 | #endif -------------------------------------------------------------------------------- /ExceptionHelper/ExceptionHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // ExceptionHelper.m 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 3/13/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "ExceptionHelper.h" 28 | 29 | #ifdef ENABLE_EXCEPTIONS 30 | 31 | @implementation ExceptionHelper 32 | + (void)generateStackTraceForException:(NSException*)exception { 33 | NSString* processIdentifier = [[NSNumber numberWithInt:[[NSProcessInfo processInfo] processIdentifier]] stringValue]; 34 | NSString* stackAddresses = [[exception callStackReturnAddresses] componentsJoinedByString:@" "]; 35 | NSMutableString* debugInfo = [NSMutableString string]; 36 | [debugInfo appendString:@"\n"]; 37 | [debugInfo appendString:@"================================\n"]; 38 | [debugInfo appendString:@"Exception Caught:\n"]; 39 | [debugInfo appendString:@"--------------------------------\n"]; 40 | [debugInfo appendFormat:@"Name: %@\n", exception.name]; 41 | [debugInfo appendFormat:@"Reason: %@\n", exception.reason]; 42 | if(exception.userInfo) { 43 | [debugInfo appendFormat:@"Additional Info: %@\n", exception.userInfo]; 44 | } else { 45 | [debugInfo appendString:@"No additional information available..\n"]; 46 | } 47 | [debugInfo appendString:@"--------------------------------\n"]; 48 | [debugInfo appendFormat:@"Run the following command in GDB to view a full stack trace:\n\nshell atos -p %@ %@\n\n", processIdentifier, stackAddresses]; 49 | [debugInfo appendString:@"================================\n"]; 50 | NSLog(@"%@", debugInfo); 51 | 52 | while(1) { 53 | // Keeps the process alive while you run the command. 54 | } 55 | } 56 | @end 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /NSApplication/NSApplicationHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSApplicationHelper.h 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 4/9/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | #if MAC_OS_X_VERSION_10_5 <= MAC_OS_X_VERSION_MAX_ALLOWED 30 | 31 | NSString* AppSupportDirectory(); 32 | 33 | @interface NSApplication (Helper) 34 | 35 | @end 36 | 37 | #endif -------------------------------------------------------------------------------- /NSApplication/NSApplicationHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSApplicationHelper.m 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 4/9/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "NSApplicationHelper.h" 28 | 29 | #if MAC_OS_X_VERSION_10_5 <= MAC_OS_X_VERSION_MAX_ALLOWED 30 | 31 | NSString* AppSupportDirectory() { 32 | NSString* appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 33 | appSupportDir = [appSupportDir stringByAppendingPathComponent:[[NSProcessInfo processInfo] processName]]; 34 | 35 | if(![[NSFileManager defaultManager] fileExistsAtPath:appSupportDir]) { 36 | [[NSFileManager defaultManager] createDirectoryAtPath:appSupportDir withIntermediateDirectories:YES attributes:nil error:NULL]; 37 | } 38 | 39 | return appSupportDir; 40 | } 41 | 42 | @implementation NSApplication (Helper) 43 | 44 | @end 45 | 46 | #endif -------------------------------------------------------------------------------- /NSArray/NSArrayHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArrayHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 10/28/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | 30 | @interface NSArray (Helper) 31 | 32 | /* 33 | * Returns an array with A-Z and # to be used as section titles 34 | */ 35 | + (NSArray*)arrayWithAlphaNumericTitles; 36 | 37 | /* 38 | * Returns an array with the Search icon, A-Z and # to be used as section titles 39 | */ 40 | + (NSArray*)arrayWithAlphaNumericTitlesWithSearch:(BOOL)search; 41 | 42 | /* 43 | * Checks to see if the array is empty 44 | */ 45 | @property(nonatomic,readonly,getter=isEmpty) BOOL empty; 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /NSArray/NSArrayHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSArrayHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 10/28/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "NSArrayHelper.h" 28 | 29 | 30 | @implementation NSArray (Helper) 31 | 32 | + (NSArray*)arrayWithAlphaNumericTitles { 33 | return [self arrayWithAlphaNumericTitlesWithSearch:NO]; 34 | } 35 | 36 | + (NSArray*)arrayWithAlphaNumericTitlesWithSearch:(BOOL)search { 37 | if(search) { 38 | return [NSArray arrayWithObjects: @"{search}", 39 | @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", 40 | @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", 41 | @"W", @"X", @"Y", @"Z", @"#", nil]; 42 | } else { 43 | return [NSArray arrayWithObjects: 44 | @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", 45 | @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", 46 | @"W", @"X", @"Y", @"Z", @"#", nil]; 47 | } 48 | } 49 | 50 | - (BOOL)isEmpty { 51 | return [self count] == 0 ? YES : NO; 52 | } 53 | 54 | @end 55 | -------------------------------------------------------------------------------- /NSData/NSDataHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSDataHelper.h 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 4/9/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | 28 | #import 29 | 30 | @interface NSData (Helper) 31 | 32 | @property(nonatomic,readonly,getter=isEmpty) BOOL empty; 33 | 34 | @end 35 | 36 | /* 37 | Base64 Methods 38 | 39 | Created by Matt Gallagher on 2009/06/03. 40 | Copyright 2009 Matt Gallagher. All rights reserved. 41 | 42 | Permission is given to use this source code file, free of charge, in any 43 | project, commercial or otherwise, entirely at your risk, with the condition 44 | appreciated but not required. 45 | 46 | */ 47 | 48 | @interface NSData (Base64Helper) 49 | 50 | + (NSData *)dataFromBase64EncodedString:(NSString *)aString; 51 | - (NSString *)base64EncodedString; 52 | 53 | @end -------------------------------------------------------------------------------- /NSData/NSDataHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSDataHelper.m 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 4/9/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "NSDataHelper.h" 28 | 29 | @implementation NSData (Helper) 30 | 31 | - (BOOL)isEmpty { 32 | return [self length] == 0; 33 | } 34 | 35 | @end 36 | 37 | 38 | #pragma mark - 39 | #pragma mark Base64 Methods 40 | /* 41 | 42 | Created by Matt Gallagher on 2009/06/03. 43 | Copyright 2009 Matt Gallagher. All rights reserved. 44 | 45 | Permission is given to use this source code file, free of charge, in any 46 | project, commercial or otherwise, entirely at your risk, with the condition 47 | appreciated but not required. 48 | 49 | */ 50 | 51 | // 52 | // Mapping from 6 bit pattern to ASCII character. 53 | // 54 | static unsigned char base64EncodeLookup[65] = 55 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 56 | 57 | // 58 | // Definition for "masked-out" areas of the base64DecodeLookup mapping 59 | // 60 | #define xx 65 61 | 62 | // 63 | // Mapping from ASCII character to 6 bit pattern. 64 | // 65 | static unsigned char base64DecodeLookup[256] = 66 | { 67 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 68 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 69 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 62, xx, xx, xx, 63, 70 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, xx, xx, xx, xx, xx, xx, 71 | xx, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 72 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, xx, xx, xx, xx, xx, 73 | xx, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 74 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, xx, xx, xx, xx, xx, 75 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 76 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 77 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 78 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 79 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 80 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 81 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 82 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 83 | }; 84 | 85 | // 86 | // Fundamental sizes of the binary and base64 encode/decode units in bytes 87 | // 88 | #define BINARY_UNIT_SIZE 3 89 | #define BASE64_UNIT_SIZE 4 90 | 91 | // 92 | // NewBase64Decode 93 | // 94 | // Decodes the base64 ASCII string in the inputBuffer to a newly malloced 95 | // output buffer. 96 | // 97 | // inputBuffer - the source ASCII string for the decode 98 | // length - the length of the string or -1 (to specify strlen should be used) 99 | // outputLength - if not-NULL, on output will contain the decoded length 100 | // 101 | // returns the decoded buffer. Must be free'd by caller. Length is given by 102 | // outputLength. 103 | // 104 | void *NewBase64Decode(const char *inputBuffer, size_t length, size_t *outputLength) { 105 | if (length == -1) 106 | { 107 | length = strlen(inputBuffer); 108 | } 109 | 110 | size_t outputBufferSize = (length / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE; 111 | unsigned char *outputBuffer = (unsigned char *)malloc(outputBufferSize); 112 | 113 | size_t i = 0; 114 | size_t j = 0; 115 | while (i < length) 116 | { 117 | // 118 | // Accumulate 4 valid characters (ignore everything else) 119 | // 120 | unsigned char accumulated[BASE64_UNIT_SIZE]; 121 | size_t accumulateIndex = 0; 122 | while (i < length) 123 | { 124 | unsigned char decode = base64DecodeLookup[inputBuffer[i++]]; 125 | if (decode != xx) 126 | { 127 | accumulated[accumulateIndex] = decode; 128 | accumulateIndex++; 129 | 130 | if (accumulateIndex == BASE64_UNIT_SIZE) 131 | { 132 | break; 133 | } 134 | } 135 | } 136 | 137 | // 138 | // Store the 6 bits from each of the 4 characters as 3 bytes 139 | // 140 | outputBuffer[j] = (accumulated[0] << 2) | (accumulated[1] >> 4); 141 | outputBuffer[j + 1] = (accumulated[1] << 4) | (accumulated[2] >> 2); 142 | outputBuffer[j + 2] = (accumulated[2] << 6) | accumulated[3]; 143 | j += accumulateIndex - 1; 144 | } 145 | 146 | if (outputLength) 147 | { 148 | *outputLength = j; 149 | } 150 | return outputBuffer; 151 | } 152 | 153 | // 154 | // NewBase64Decode 155 | // 156 | // Encodes the arbitrary data in the inputBuffer as base64 into a newly malloced 157 | // output buffer. 158 | // 159 | // inputBuffer - the source data for the encode 160 | // length - the length of the input in bytes 161 | // separateLines - if zero, no CR/LF characters will be added. Otherwise 162 | // a CR/LF pair will be added every 64 encoded chars. 163 | // outputLength - if not-NULL, on output will contain the encoded length 164 | // (not including terminating 0 char) 165 | // 166 | // returns the encoded buffer. Must be free'd by caller. Length is given by 167 | // outputLength. 168 | // 169 | 170 | char *NewBase64Encode(const void *buffer, size_t length, bool separateLines, size_t *outputLength) { 171 | const unsigned char *inputBuffer = (const unsigned char *)buffer; 172 | 173 | #define MAX_NUM_PADDING_CHARS 2 174 | #define OUTPUT_LINE_LENGTH 64 175 | #define INPUT_LINE_LENGTH ((OUTPUT_LINE_LENGTH / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE) 176 | #define CR_LF_SIZE 2 177 | 178 | // 179 | // Byte accurate calculation of final buffer size 180 | // 181 | size_t outputBufferSize = 182 | ((length / BINARY_UNIT_SIZE) 183 | + ((length % BINARY_UNIT_SIZE) ? 1 : 0)) 184 | * BASE64_UNIT_SIZE; 185 | if (separateLines) 186 | { 187 | outputBufferSize += 188 | (outputBufferSize / OUTPUT_LINE_LENGTH) * CR_LF_SIZE; 189 | } 190 | 191 | // 192 | // Include space for a terminating zero 193 | // 194 | outputBufferSize += 1; 195 | 196 | // 197 | // Allocate the output buffer 198 | // 199 | char *outputBuffer = (char *)malloc(outputBufferSize); 200 | if (!outputBuffer) 201 | { 202 | return NULL; 203 | } 204 | 205 | size_t i = 0; 206 | size_t j = 0; 207 | const size_t lineLength = separateLines ? INPUT_LINE_LENGTH : length; 208 | size_t lineEnd = lineLength; 209 | 210 | while (true) 211 | { 212 | if (lineEnd > length) 213 | { 214 | lineEnd = length; 215 | } 216 | 217 | for (; i + BINARY_UNIT_SIZE - 1 < lineEnd; i += BINARY_UNIT_SIZE) 218 | { 219 | // 220 | // Inner loop: turn 48 bytes into 64 base64 characters 221 | // 222 | outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2]; 223 | outputBuffer[j++] = base64EncodeLookup[((inputBuffer[i] & 0x03) << 4) 224 | | ((inputBuffer[i + 1] & 0xF0) >> 4)]; 225 | outputBuffer[j++] = base64EncodeLookup[((inputBuffer[i + 1] & 0x0F) << 2) 226 | | ((inputBuffer[i + 2] & 0xC0) >> 6)]; 227 | outputBuffer[j++] = base64EncodeLookup[inputBuffer[i + 2] & 0x3F]; 228 | } 229 | 230 | if (lineEnd == length) 231 | { 232 | break; 233 | } 234 | 235 | // 236 | // Add the newline 237 | // 238 | outputBuffer[j++] = '\r'; 239 | outputBuffer[j++] = '\n'; 240 | lineEnd += lineLength; 241 | } 242 | 243 | if (i + 1 < length) 244 | { 245 | // 246 | // Handle the single '=' case 247 | // 248 | outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2]; 249 | outputBuffer[j++] = base64EncodeLookup[((inputBuffer[i] & 0x03) << 4) 250 | | ((inputBuffer[i + 1] & 0xF0) >> 4)]; 251 | outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i + 1] & 0x0F) << 2]; 252 | outputBuffer[j++] = '='; 253 | } 254 | else if (i < length) 255 | { 256 | // 257 | // Handle the double '=' case 258 | // 259 | outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2]; 260 | outputBuffer[j++] = base64EncodeLookup[(inputBuffer[i] & 0x03) << 4]; 261 | outputBuffer[j++] = '='; 262 | outputBuffer[j++] = '='; 263 | } 264 | outputBuffer[j] = 0; 265 | 266 | // 267 | // Set the output length and return the buffer 268 | // 269 | if (outputLength) 270 | { 271 | *outputLength = j; 272 | } 273 | return outputBuffer; 274 | } 275 | 276 | @implementation NSData (Base64) 277 | 278 | // 279 | // dataFromBase64String: 280 | // 281 | // Creates an NSData object containing the base64 decoded representation of 282 | // the base64 string 'aString' 283 | // 284 | // Parameters: 285 | // aString - the base64 string to decode 286 | // 287 | // returns the autoreleased NSData representation of the base64 string 288 | // 289 | + (NSData *)dataFromBase64String:(NSString *)aString 290 | { 291 | NSData *data = [aString dataUsingEncoding:NSASCIIStringEncoding]; 292 | size_t outputLength; 293 | void *outputBuffer = NewBase64Decode([data bytes], [data length], &outputLength); 294 | NSData *result = [NSData dataWithBytes:outputBuffer length:outputLength]; 295 | free(outputBuffer); 296 | return result; 297 | } 298 | 299 | // 300 | // base64EncodedString 301 | // 302 | // Creates an NSString object that contains the base 64 encoding of the 303 | // receiver's data. Lines are broken at 64 characters long. 304 | // 305 | // returns an autoreleased NSString being the base 64 representation of the 306 | // receiver. 307 | // 308 | - (NSString *)base64EncodedString 309 | { 310 | size_t outputLength; 311 | char *outputBuffer = 312 | NewBase64Encode([self bytes], [self length], true, &outputLength); 313 | 314 | NSString *result = 315 | [[[NSString alloc] 316 | initWithBytes:outputBuffer 317 | length:outputLength 318 | encoding:NSASCIIStringEncoding] 319 | autorelease]; 320 | free(outputBuffer); 321 | return result; 322 | } 323 | 324 | @end -------------------------------------------------------------------------------- /NSDate/NSDateHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSDateHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 10/15/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | 30 | @interface NSDate (Helper) 31 | 32 | // Returns an NSDate based on a string with formatting options passed to NSDateFormatter 33 | + (NSDate*)dateWithString:(NSString*)dateString formatString:(NSString*)dateFormatterString; 34 | 35 | // Returns an NSDate with an ISO8610 format, aka ATOM: yyyy-MM-dd'T'HH:mm:ssZZZ 36 | + (NSDate*)dateWithISO8601String:(NSString*)str; 37 | 38 | // Returns an NSDate with a 'yyyy-MM-dd' string 39 | + (NSDate*)dateWithDateString:(NSString*)str; 40 | 41 | // Returns an NSDate with a 'yyyy-MM-dd HH:mm:ss' string 42 | + (NSDate*)dateWithDateTimeString:(NSString*)str; 43 | 44 | // Returns an NSDate with a 'dd MMM yyyy HH:mm:ss' string 45 | + (NSDate*)dateWithLongDateTimeString:(NSString*)str; 46 | 47 | // Returns an NSDate with an RSS formatted string: 'EEE, d MMM yyyy HH:mm:ss ZZZ' string 48 | + (NSDate*)dateWithRSSDateString:(NSString*)str; 49 | 50 | // Returns an NSDate with an alternative RSS formatted string: 'd MMM yyyy HH:mm:ss ZZZ' string 51 | + (NSDate*)dateWithAltRSSDateString:(NSString*)str; 52 | 53 | // just now, 2 minutes ago, 2 hours ago, 2 days ago, etc. 54 | - (NSString*)formattedExactRelativeDate; 55 | 56 | // Pass in an string compatible with NSDateFormatter 57 | - (NSString*)formattedDateWithFormatString:(NSString*)dateFormatterString; 58 | 59 | // Returns date formatted to: EEE, d MMM 'at' h:mma 60 | - (NSString*)formattedDate; 61 | 62 | // Returns date formatted to: NSDateFormatterShortStyle 63 | - (NSString*)formattedTime; 64 | 65 | // Returns date formatted to: Weekday if within last 7 days, Yesterday/Tomorrow, or NSDateFormatterShortStyle for everything else 66 | - (NSString*)relativeFormattedDate; 67 | 68 | // Returns date formatted to: Weekday if within last 7 days, Yesterday/Today/Tomorrow, or NSDateFormatterShortStyle for everything else 69 | // If date is today, returns no Date, instead returns NSDateFormatterShortStyle for time 70 | - (NSString*)relativeFormattedDateOnly; 71 | 72 | // Returns date formatted to: Weekday if within last 7 days, Yesterday/Today/Tomorrow, or NSDateFormatterFullStyle for everything else 73 | // Also returns NSDateFormatterShortStyle for time 74 | - (NSString*)relativeFormattedDateTime; 75 | 76 | // Returns date formatted to: Weekday if within last 7 days, Yesterday/Today/Tomorrow, or NSDateFormatterFullStyle for everything else 77 | - (NSString*)relativeLongFormattedDate; 78 | 79 | // Returns date formatted for ISO8601/ATOM: yyyy-MM-dd'T'HH:mm:ssZZZ 80 | - (NSString*)iso8601Formatted; 81 | 82 | // Checks whether current date is past date 83 | - (BOOL)isPastDate; 84 | 85 | // Checks whether the current date occured today 86 | - (BOOL)isDateToday; 87 | 88 | // Checks whether the current date occured yesterday 89 | - (BOOL)isDateYesterday; 90 | 91 | // Returns the current date, at midnight 92 | - (NSDate*)midnightDate; 93 | 94 | @end 95 | -------------------------------------------------------------------------------- /NSDate/NSDateHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSDateHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 10/15/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "NSDateHelper.h" 28 | 29 | @implementation NSDate (Helper) 30 | 31 | + (NSDate*)dateWithString:(NSString*)dateString formatString:(NSString*)dateFormatterString { 32 | if(!dateString) return nil; 33 | 34 | NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; 35 | [formatter setDateFormat:dateFormatterString]; 36 | 37 | NSDate *theDate = [formatter dateFromString:dateString]; 38 | [formatter release]; 39 | return theDate; 40 | } 41 | 42 | + (NSDate*)dateWithISO8601String:(NSString*)dateString { 43 | if(!dateString) return nil; 44 | 45 | if([dateString hasSuffix:@" 00:00"]) { 46 | dateString = [[dateString substringToIndex:(dateString.length-6)] stringByAppendingString:@"GMT"]; 47 | } else if ([dateString hasSuffix:@"Z"]) { 48 | dateString = [[dateString substringToIndex:(dateString.length-1)] stringByAppendingString:@"GMT"]; 49 | } 50 | 51 | return [[self class] dateWithString:dateString formatString:@"yyyy-MM-dd'T'HH:mm:ssZZZ"]; 52 | } 53 | 54 | + (NSDate*)dateWithDateString:(NSString*)dateString { 55 | return [[self class] dateWithString:dateString formatString:@"yyyy-MM-dd"]; 56 | } 57 | 58 | + (NSDate*)dateWithDateTimeString:(NSString*)dateString { 59 | return [[self class] dateWithString:dateString formatString:@"yyyy-MM-dd HH:mm:ss"]; 60 | } 61 | 62 | + (NSDate*)dateWithLongDateTimeString:(NSString*)dateString { 63 | return [[self class] dateWithString:dateString formatString:@"dd MMM yyyy HH:mm:ss"]; 64 | } 65 | 66 | + (NSDate*)dateWithRSSDateString:(NSString*)dateString { 67 | if ([dateString hasSuffix:@"Z"]) { 68 | dateString = [[dateString substringToIndex:(dateString.length-1)] stringByAppendingString:@"GMT"]; 69 | } 70 | 71 | return [[self class] dateWithString:dateString formatString:@"EEE, d MMM yyyy HH:mm:ss ZZZ"]; 72 | } 73 | 74 | + (NSDate*)dateWithAltRSSDateString:(NSString*)dateString { 75 | if ([dateString hasSuffix:@"Z"]) { 76 | dateString = [[dateString substringToIndex:(dateString.length-1)] stringByAppendingString:@"GMT"]; 77 | } 78 | 79 | return [[self class] dateWithString:dateString formatString:@"d MMM yyyy HH:mm:ss ZZZ"]; 80 | } 81 | 82 | - (NSString*)formattedExactRelativeDate { 83 | NSTimeInterval time = [self timeIntervalSince1970]; 84 | NSTimeInterval now = [[NSDate date] timeIntervalSince1970]; 85 | NSTimeInterval diff = now - time; 86 | 87 | if(diff < 10) { 88 | return LocalizedString(@"just now"); 89 | } else if(diff < 60) { 90 | return LocalizedStringWithFormat(@"%d seconds ago", (int)diff); 91 | } 92 | 93 | diff = round(diff/60); 94 | if(diff < 60) { 95 | if(diff == 1) { 96 | return LocalizedStringWithFormat(@"%d minute ago", (int)diff); 97 | } else { 98 | return LocalizedStringWithFormat(@"%d minutes ago", (int)diff); 99 | } 100 | } 101 | 102 | diff = round(diff/60); 103 | if(diff < 24) { 104 | if(diff == 1) { 105 | return LocalizedStringWithFormat(@"%d hour ago", (int)diff); 106 | } else { 107 | return LocalizedStringWithFormat(@"%d hours ago", (int)diff); 108 | } 109 | } 110 | 111 | if(diff < 7) { 112 | if(diff == 1) { 113 | return LocalizedString(@"yesterday"); 114 | } else { 115 | return LocalizedStringWithFormat(@"%d days ago", (int)diff); 116 | } 117 | } 118 | 119 | return [self formattedDateWithFormatString:LocalizedString(@"MM/dd/yy")]; 120 | } 121 | 122 | - (NSString*)formattedDateWithFormatString:(NSString*)dateFormatterString { 123 | if(!dateFormatterString) return nil; 124 | 125 | NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; 126 | [formatter setDateFormat:dateFormatterString]; 127 | [formatter setAMSymbol:@"am"]; 128 | [formatter setPMSymbol:@"pm"]; 129 | return [formatter stringFromDate:self]; 130 | } 131 | 132 | - (NSString*)formattedDate { 133 | return [self formattedDateWithFormatString:@"EEE, d MMM 'at' h:mma"]; 134 | } 135 | 136 | - (NSString*)relativeFormattedDate { 137 | // Initialize the formatter. 138 | NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; 139 | [formatter setDateStyle:NSDateFormatterShortStyle]; 140 | [formatter setTimeStyle:NSDateFormatterNoStyle]; 141 | 142 | // Initialize the calendar and flags. 143 | unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit; 144 | NSCalendar* calendar = [NSCalendar currentCalendar]; 145 | 146 | // Create reference date for supplied date. 147 | NSDateComponents *comps = [calendar components:unitFlags fromDate:self]; 148 | [comps setHour:0]; 149 | [comps setMinute:0]; 150 | [comps setSecond:0]; 151 | 152 | NSDate* suppliedDate = [calendar dateFromComponents:comps]; 153 | 154 | // Iterate through the eight days (tomorrow, today, and the last six). 155 | int i; 156 | for (i = -1; i < 7; i++) { 157 | // Initialize reference date. 158 | comps = [calendar components:unitFlags fromDate:[NSDate date]]; 159 | [comps setHour:0]; 160 | [comps setMinute:0]; 161 | [comps setSecond:0]; 162 | [comps setDay:[comps day] - i]; 163 | NSDate* referenceDate = [calendar dateFromComponents:comps]; 164 | // Get week day (starts at 1). 165 | int weekday = [[calendar components:unitFlags fromDate:referenceDate] weekday] - 1; 166 | 167 | if ([suppliedDate compare:referenceDate] == NSOrderedSame && i == 0) { 168 | // Today 169 | [formatter setDateStyle:NSDateFormatterNoStyle]; 170 | [formatter setTimeStyle:NSDateFormatterShortStyle]; 171 | break; 172 | } else if ([suppliedDate compare:referenceDate] == NSOrderedSame && i == 1) { 173 | // Yesterday 174 | [formatter setDateStyle:NSDateFormatterNoStyle]; 175 | return [NSString stringWithString:LocalizedString(@"Yesterday")]; 176 | } else if ([suppliedDate compare:referenceDate] == NSOrderedSame) { 177 | // Day of the week 178 | return [[formatter weekdaySymbols] objectAtIndex:weekday]; 179 | } 180 | } 181 | 182 | // It's not in those eight days. 183 | return [formatter stringFromDate:self]; 184 | } 185 | 186 | - (NSString*)relativeFormattedDateOnly { 187 | // Initialize the formatter. 188 | NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; 189 | [formatter setDateStyle:NSDateFormatterShortStyle]; 190 | [formatter setTimeStyle:NSDateFormatterNoStyle]; 191 | 192 | // Initialize the calendar and flags. 193 | unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit; 194 | NSCalendar* calendar = [NSCalendar currentCalendar]; 195 | 196 | // Create reference date for supplied date. 197 | NSDateComponents *comps = [calendar components:unitFlags fromDate:self]; 198 | [comps setHour:0]; 199 | [comps setMinute:0]; 200 | [comps setSecond:0]; 201 | 202 | NSDate* suppliedDate = [calendar dateFromComponents:comps]; 203 | 204 | // Iterate through the eight days (tomorrow, today, and the last six). 205 | int i; 206 | for (i = -1; i < 7; i++) { 207 | // Initialize reference date. 208 | comps = [calendar components:unitFlags fromDate:[NSDate date]]; 209 | [comps setHour:0]; 210 | [comps setMinute:0]; 211 | [comps setSecond:0]; 212 | [comps setDay:[comps day] - i]; 213 | NSDate* referenceDate = [calendar dateFromComponents:comps]; 214 | // Get week day (starts at 1). 215 | int weekday = [[calendar components:unitFlags fromDate:referenceDate] weekday] - 1; 216 | 217 | if ([suppliedDate compare:referenceDate] == NSOrderedSame && i == 0) { 218 | // Today 219 | [formatter setDateStyle:NSDateFormatterNoStyle]; 220 | return [NSString stringWithString:LocalizedString(@"Today")]; 221 | } else if ([suppliedDate compare:referenceDate] == NSOrderedSame && i == 1) { 222 | // Yesterday 223 | [formatter setDateStyle:NSDateFormatterNoStyle]; 224 | return [NSString stringWithString:LocalizedString(@"Yesterday")]; 225 | } else if ([suppliedDate compare:referenceDate] == NSOrderedSame && i == -1) { 226 | // Yesterday 227 | [formatter setDateStyle:NSDateFormatterNoStyle]; 228 | return [NSString stringWithString:LocalizedString(@"Tomorrow")]; 229 | } else if ([suppliedDate compare:referenceDate] == NSOrderedSame) { 230 | // Day of the week 231 | return [[formatter weekdaySymbols] objectAtIndex:weekday]; 232 | } 233 | } 234 | 235 | // It's not in those eight days. 236 | return [formatter stringFromDate:self]; 237 | } 238 | 239 | - (NSString*)relativeFormattedDateTime { 240 | // Initialize the formatter. 241 | NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; 242 | [formatter setDateStyle:NSDateFormatterShortStyle]; 243 | [formatter setTimeStyle:NSDateFormatterShortStyle]; 244 | [formatter setAMSymbol:@"am"]; 245 | [formatter setPMSymbol:@"pm"]; 246 | 247 | // Initialize the calendar and flags. 248 | unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit; 249 | NSCalendar* calendar = [NSCalendar currentCalendar]; 250 | 251 | // Create reference date for supplied date. 252 | NSDateComponents *comps = [calendar components:unitFlags fromDate:self]; 253 | [comps setHour:0]; 254 | [comps setMinute:0]; 255 | [comps setSecond:0]; 256 | 257 | NSDate* suppliedDate = [calendar dateFromComponents:comps]; 258 | 259 | // Iterate through the eight days (tomorrow, today, and the last six). 260 | int i; 261 | for (i = -1; i < 7; i++) { 262 | // Initialize reference date. 263 | comps = [calendar components:unitFlags fromDate:[NSDate date]]; 264 | [comps setHour:0]; 265 | [comps setMinute:0]; 266 | [comps setSecond:0]; 267 | [comps setDay:[comps day] - i]; 268 | NSDate* referenceDate = [calendar dateFromComponents:comps]; 269 | // Get week day (starts at 1). 270 | int weekday = [[calendar components:unitFlags fromDate:referenceDate] weekday] - 1; 271 | 272 | if ([suppliedDate compare:referenceDate] == NSOrderedSame && i == 0) { 273 | // Today 274 | [formatter setDateStyle:NSDateFormatterNoStyle]; 275 | return [NSString stringWithFormat:@"Today, %@", [formatter stringFromDate:self]]; 276 | } else if ([suppliedDate compare:referenceDate] == NSOrderedSame && i == 1) { 277 | // Yesterday 278 | [formatter setDateStyle:NSDateFormatterNoStyle]; 279 | return [NSString stringWithFormat:@"Yesterday, %@", [formatter stringFromDate:self]]; 280 | } else if ([suppliedDate compare:referenceDate] == NSOrderedSame) { 281 | // Day of the week 282 | NSString* day = [[formatter weekdaySymbols] objectAtIndex:weekday]; 283 | [formatter setDateStyle:NSDateFormatterNoStyle]; 284 | return [NSString stringWithFormat:@"%@, %@", day, [formatter stringFromDate:self]]; 285 | } 286 | } 287 | 288 | // It's not in those eight days. 289 | [formatter setDateStyle:NSDateFormatterShortStyle]; 290 | [formatter setTimeStyle:NSDateFormatterNoStyle]; 291 | NSString* date = [formatter stringFromDate:self]; 292 | 293 | [formatter setDateStyle:NSDateFormatterNoStyle]; 294 | [formatter setTimeStyle:NSDateFormatterShortStyle]; 295 | NSString* time = [formatter stringFromDate:self]; 296 | 297 | return [NSString stringWithFormat:@"%@, %@", date, time]; 298 | } 299 | 300 | - (NSString*)relativeLongFormattedDate { 301 | // Initialize the formatter. 302 | NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; 303 | [formatter setDateStyle:NSDateFormatterFullStyle]; 304 | [formatter setTimeStyle:NSDateFormatterNoStyle]; 305 | 306 | // Initialize the calendar and flags. 307 | unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit; 308 | NSCalendar* calendar = [NSCalendar currentCalendar]; 309 | 310 | // Create reference date for supplied date. 311 | NSDateComponents *comps = [calendar components:unitFlags fromDate:self]; 312 | [comps setHour:0]; 313 | [comps setMinute:0]; 314 | [comps setSecond:0]; 315 | 316 | NSDate* suppliedDate = [calendar dateFromComponents:comps]; 317 | 318 | // Iterate through the eight days (tomorrow, today, and the last six). 319 | int i; 320 | for (i = -1; i < 7; i++) { 321 | // Initialize reference date. 322 | comps = [calendar components:unitFlags fromDate:[NSDate date]]; 323 | [comps setHour:0]; 324 | [comps setMinute:0]; 325 | [comps setSecond:0]; 326 | [comps setDay:[comps day] - i]; 327 | NSDate* referenceDate = [calendar dateFromComponents:comps]; 328 | // Get week day (starts at 1). 329 | int weekday = [[calendar components:unitFlags fromDate:referenceDate] weekday] - 1; 330 | 331 | if ([suppliedDate compare:referenceDate] == NSOrderedSame && i == 0) { 332 | // Today 333 | [formatter setDateStyle:NSDateFormatterNoStyle]; 334 | return [NSString stringWithString:LocalizedString(@"Today")]; 335 | } else if ([suppliedDate compare:referenceDate] == NSOrderedSame && i == 1) { 336 | // Yesterday 337 | [formatter setDateStyle:NSDateFormatterNoStyle]; 338 | return [NSString stringWithString:LocalizedString(@"Yesterday")]; 339 | } else if ([suppliedDate compare:referenceDate] == NSOrderedSame && i == -1) { 340 | // Tomorrow 341 | [formatter setDateStyle:NSDateFormatterNoStyle]; 342 | return [NSString stringWithString:LocalizedString(@"Tomorrow")]; 343 | } else if ([suppliedDate compare:referenceDate] == NSOrderedSame) { 344 | // Day of the week 345 | return [[formatter weekdaySymbols] objectAtIndex:weekday]; 346 | } 347 | } 348 | 349 | // It's not in those eight days. 350 | return [formatter stringFromDate:self]; 351 | } 352 | 353 | - (NSString*)formattedTime { 354 | // Initialize the formatter. 355 | NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; 356 | [formatter setDateStyle:NSDateFormatterNoStyle]; 357 | [formatter setTimeStyle:NSDateFormatterShortStyle]; 358 | 359 | return [formatter stringFromDate:self]; 360 | } 361 | 362 | - (NSString*)iso8601Formatted { 363 | return [self formattedDateWithFormatString:@"yyyy-MM-dd'T'HH:mm:ssZ"]; 364 | } 365 | 366 | - (BOOL)isPastDate { 367 | NSDate* now = [NSDate date]; 368 | if([[now earlierDate:self] isEqualToDate:self]) { 369 | return YES; 370 | } else { 371 | return NO; 372 | } 373 | } 374 | 375 | - (BOOL)isDateToday { 376 | return [[[NSDate date] midnightDate] isEqual:[self midnightDate]]; 377 | } 378 | 379 | - (BOOL)isDateYesterday { 380 | return [[[NSDate dateWithTimeIntervalSinceNow:-86400] midnightDate] isEqual:[self midnightDate]]; 381 | } 382 | 383 | - (NSDate*)midnightDate { 384 | return [[NSCalendar currentCalendar] dateFromComponents:[[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:self]]; 385 | } 386 | 387 | @end 388 | -------------------------------------------------------------------------------- /NSDictionary/NSDictionaryHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSDictionaryHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 10/29/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | 30 | @interface NSDictionary (Helper) 31 | 32 | /* 33 | * Checks to see if the dictionary contains the given key 34 | */ 35 | - (BOOL)containsObjectForKey:(id)key; 36 | 37 | /* 38 | * Checks to see if the dictionary is empty 39 | */ 40 | @property(nonatomic,readonly,getter=isEmpty) BOOL empty; 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /NSDictionary/NSDictionaryHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSDictionaryHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 10/29/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "NSDictionaryHelper.h" 28 | 29 | 30 | @implementation NSDictionary (Helper) 31 | 32 | - (BOOL)containsObjectForKey:(id)key { 33 | return [[self allKeys] containsObject:key]; 34 | } 35 | 36 | - (BOOL)isEmpty { 37 | return [self count] == 0 ? YES : NO; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /NSGradient/NSGradientHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSGradientHelper.h 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 10/12/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | #if MAC_OS_X_VERSION_10_5 <= MAC_OS_X_VERSION_MAX_ALLOWED 30 | 31 | @interface NSGradient (Helper) 32 | 33 | - (CGImageRef)createImageRefForSize:(NSSize)size angle:(CGFloat)angle; 34 | - (NSImage*)imageForSize:(NSSize)size angle:(CGFloat)angle; 35 | 36 | @end 37 | 38 | #endif -------------------------------------------------------------------------------- /NSGradient/NSGradientHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSGradientHelper.m 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 10/12/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "NSGradientHelper.h" 28 | 29 | #if MAC_OS_X_VERSION_10_5 <= MAC_OS_X_VERSION_MAX_ALLOWED 30 | 31 | @implementation NSGradient (Helper) 32 | 33 | - (CGImageRef)createImageRefForSize:(NSSize)size angle:(CGFloat)angle { 34 | CGContextRef bitmapCtx = CGBitmapContextCreate(NULL, size.width, size.height, 8, size.width*4, [[NSColorSpace genericRGBColorSpace] CGColorSpace], kCGBitmapByteOrder32Host|kCGImageAlphaPremultipliedFirst); 35 | 36 | [NSGraphicsContext saveGraphicsState]; 37 | [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:bitmapCtx flipped:NO]]; 38 | [self drawInRect:NSMakeRect(0.0f, 0.0f, size.width, size.height) angle:angle]; 39 | [NSGraphicsContext restoreGraphicsState]; 40 | 41 | CGImageRef cgImage = CGBitmapContextCreateImage(bitmapCtx); 42 | CGContextRelease(bitmapCtx); 43 | 44 | return cgImage; 45 | } 46 | 47 | - (NSImage*)imageForSize:(NSSize)size angle:(CGFloat)angle { 48 | CGImageRef cgImage = [self createImageRefForSize:size angle:angle]; 49 | 50 | NSBitmapImageRep* bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage]; 51 | NSImage* image = [[NSImage alloc] init]; 52 | [image addRepresentation:bitmapRep]; 53 | [bitmapRep release]; 54 | 55 | CGImageRelease(cgImage); 56 | 57 | return [image autorelease]; 58 | } 59 | 60 | @end 61 | 62 | #endif -------------------------------------------------------------------------------- /NSObject/NSObjectHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObjectHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 5/7/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | @interface NSObject (Helper) 30 | 31 | // Reroutes all calls to the first method, to the second method in the current class 32 | + (void)swizzleMethod:(SEL)originalMethod withMethod:(SEL)newMethod; 33 | 34 | // Adds the method to this class, from the indicated class 35 | + (void)appendMethod:(SEL)newMethod fromClass:(Class)aClass; 36 | 37 | // Replaces calls to this classes method, with the same method from another class 38 | + (void)replaceMethod:(SEL)aMethod fromClass:(Class)aClass; 39 | 40 | // Returns a NSArray containing just this object 41 | - (NSArray*)arrayValue; 42 | @property(nonatomic,readonly,getter=arrayValue) NSArray* NSArray; 43 | 44 | // Returns a NSMutableArray containing just this object 45 | - (NSMutableArray*)mutableArrayValue; 46 | @property(nonatomic,readonly,getter=mutableArrayValue) NSMutableArray* NSMutableArray; 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /NSObject/NSObjectHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObjectHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 5/7/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "NSObjectHelper.h" 28 | #import 29 | 30 | // MethodSwizzle used from here: http://www.cocoadev.com/index.pl?MethodSwizzle 31 | BOOL MethodSwizzle(Class klass, SEL origSel, SEL altSel, BOOL forInstance) { 32 | 33 | // Make sure the class isn't nil 34 | if (klass == nil) 35 | return NO; 36 | 37 | // Look for the methods in the implementation of the immediate class 38 | Class iterKlass = (forInstance ? klass : klass->isa); 39 | Method origMethod = NULL, altMethod = NULL; 40 | unsigned int methodCount = 0; 41 | Method *mlist = class_copyMethodList(iterKlass, &methodCount); 42 | if(mlist != NULL) { 43 | int i; 44 | for (i = 0; i < methodCount; ++i) { 45 | if(method_getName(mlist[i]) == origSel ) 46 | origMethod = mlist[i]; 47 | if (method_getName(mlist[i]) == altSel) 48 | altMethod = mlist[i]; 49 | } 50 | } 51 | 52 | // if origMethod was not found, that means it is not in the immediate class 53 | // try searching the entire class hierarchy with class_getInstanceMethod 54 | // if not found or not added, bail out 55 | if(origMethod == NULL) { 56 | origMethod = class_getInstanceMethod(iterKlass, origSel); 57 | if(origMethod == NULL) { 58 | return NO; 59 | } 60 | 61 | if(class_addMethod(iterKlass, method_getName(origMethod), method_getImplementation(origMethod), method_getTypeEncoding(origMethod)) == NO) { 62 | return NO; 63 | } 64 | } 65 | 66 | // same thing with altMethod 67 | if(altMethod == NULL) { 68 | altMethod = class_getInstanceMethod(iterKlass, altSel); 69 | if(altMethod == NULL ) 70 | return NO; 71 | if(class_addMethod(iterKlass, method_getName(altMethod), method_getImplementation(altMethod), method_getTypeEncoding(altMethod)) == NO ) 72 | return NO; 73 | } 74 | 75 | //clean up 76 | free(mlist); 77 | 78 | // we now have to look up again for the methods in case they were not in the class implementation, 79 | //but in one of the superclasses. In the latter, that means we added the method to the class, 80 | //but the Leopard APIs is only 'class_addMethod', in which case we need to have the pointer 81 | //to the Method objects actually stored in the Class structure (in the Tiger implementation, 82 | //a new mlist was explicitely created with the added methods and directly added to the class; 83 | //thus we were able to add a new Method AND get the pointer to it) 84 | 85 | // for simplicity, just use the same code as in the first step 86 | origMethod = NULL; 87 | altMethod = NULL; 88 | methodCount = 0; 89 | mlist = class_copyMethodList(iterKlass, &methodCount); 90 | if(mlist != NULL) { 91 | int i; 92 | for (i = 0; i < methodCount; ++i) { 93 | if(method_getName(mlist[i]) == origSel ) 94 | origMethod = mlist[i]; 95 | if (method_getName(mlist[i]) == altSel) 96 | altMethod = mlist[i]; 97 | } 98 | } 99 | 100 | // bail if one of the methods doesn't exist anywhere 101 | // with all we did, this should not happen, though 102 | if (origMethod == NULL || altMethod == NULL) 103 | return NO; 104 | 105 | // now swizzle 106 | method_exchangeImplementations(origMethod, altMethod); 107 | 108 | //clean up 109 | free(mlist); 110 | 111 | return YES; 112 | } 113 | 114 | void appendMethod(Class aClass, Class bClass, SEL bSel) { 115 | if(!aClass) return; 116 | if(!bClass) return; 117 | Method bMethod = class_getInstanceMethod(bClass, bSel); 118 | class_addMethod(aClass, method_getName(bMethod), method_getImplementation(bMethod), method_getTypeEncoding(bMethod)); 119 | } 120 | 121 | void replaceMethod(Class toClass, Class fromClass, SEL aSelector) { 122 | if(!toClass) return; 123 | if(!fromClass) return; 124 | Method aMethod = class_getInstanceMethod(fromClass, aSelector); 125 | class_replaceMethod(toClass, method_getName(aMethod), method_getImplementation(aMethod), method_getTypeEncoding(aMethod)); 126 | } 127 | 128 | @implementation NSObject (Helper) 129 | 130 | + (void)swizzleMethod:(SEL)originalMethod withMethod:(SEL)newMethod { 131 | MethodSwizzle([self class], originalMethod, newMethod, YES); 132 | } 133 | 134 | + (void)appendMethod:(SEL)newMethod fromClass:(Class)aClass { 135 | appendMethod([self class], aClass, newMethod); 136 | } 137 | 138 | + (void)replaceMethod:(SEL)aMethod fromClass:(Class)aClass { 139 | replaceMethod([self class], aClass, aMethod); 140 | } 141 | 142 | - (NSArray*)arrayValue { 143 | return [NSArray arrayWithObject:self]; 144 | } 145 | 146 | - (NSMutableArray*)mutableArrayValue { 147 | return [NSMutableArray arrayWithObject:self]; 148 | } 149 | 150 | @end 151 | -------------------------------------------------------------------------------- /NSString/NSStringHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSStringHelper.h 3 | // CocoaHelpers 4 | // 5 | // Created by Shaun Harrison on 10/14/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | /* 30 | * Short hand NSLocalizedString, doesn't need 2 parameters 31 | */ 32 | #define LocalizedString(s) NSLocalizedString(s,s) 33 | 34 | /* 35 | * LocalizedString with an additionl parameter for formatting 36 | */ 37 | #define LocalizedStringWithFormat(s,...) [NSString stringWithFormat:NSLocalizedString(s,s),##__VA_ARGS__] 38 | 39 | enum { 40 | NSTruncateStringPositionStart=0, 41 | NSTruncateStringPositionMiddle, 42 | NSTruncateStringPositionEnd 43 | }; typedef int NSTruncateStringPosition; 44 | 45 | @interface NSString (Helper) 46 | 47 | /* 48 | * Returns a comma separated NSString for an NSUInteger 49 | */ 50 | + (NSString*)stringWithFormattedUnsignedInteger:(NSUInteger)integer; 51 | 52 | /* 53 | * Checks to see if the string contains the given string, case insenstive 54 | */ 55 | - (BOOL)containsString:(NSString*)string; 56 | 57 | /* 58 | * Checks to see if the string contains the given string while allowing you to define the compare options 59 | */ 60 | - (BOOL)containsString:(NSString*)string options:(NSStringCompareOptions)options; 61 | 62 | /* 63 | * Returns the MD5 value of the string 64 | */ 65 | - (NSString*)md5; 66 | 67 | /* 68 | * Returns the long value of the string 69 | */ 70 | - (long)longValue; 71 | - (long long)longLongValue; 72 | - (unsigned long long)unsignedLongLongValue; 73 | 74 | /* 75 | * Truncate string to length 76 | */ 77 | - (NSString*)stringByTruncatingToLength:(int)length; 78 | - (NSString*)stringByTruncatingToLength:(int)length direction:(NSTruncateStringPosition)truncateFrom; 79 | - (NSString*)stringByTruncatingToLength:(int)length direction:(NSTruncateStringPosition)truncateFrom withEllipsisString:(NSString*)ellipsis; 80 | 81 | @end 82 | -------------------------------------------------------------------------------- /NSString/NSStringHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSStringHelper.m 3 | // CocoaHelpers 4 | // 5 | // Created by Shaun Harrison on 10/14/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "NSStringHelper.h" 28 | #import 29 | 30 | int const GGCharacterIsNotADigit = 10; 31 | 32 | @implementation NSString (Helper) 33 | 34 | + (NSString*)stringWithFormattedUnsignedInteger:(NSUInteger)integer { 35 | NSNumber* number = [NSNumber numberWithUnsignedInteger:integer]; 36 | NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init]; 37 | [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; 38 | NSString* formattedString = [formatter stringFromNumber:number]; 39 | [formatter release]; 40 | return formattedString; 41 | } 42 | 43 | - (BOOL)containsString:(NSString*)string { 44 | return [self containsString:string options:NSCaseInsensitiveSearch]; 45 | } 46 | 47 | - (BOOL)containsString:(NSString*)string options:(NSStringCompareOptions)options { 48 | return [self rangeOfString:string options:options].location == NSNotFound ? NO : YES; 49 | } 50 | 51 | #pragma mark - 52 | #pragma mark Long conversions 53 | 54 | - (long)longValue { 55 | return (long)[self longLongValue]; 56 | } 57 | 58 | - (long long)longLongValue { 59 | NSScanner* scanner = [NSScanner scannerWithString:self]; 60 | long long valueToGet; 61 | if([scanner scanLongLong:&valueToGet] == YES) { 62 | return valueToGet; 63 | } else { 64 | return 0; 65 | } 66 | } 67 | 68 | /* 69 | * Contact info@enormego.com if you're the author and we'll update this comment to reflect credit 70 | */ 71 | 72 | - (unsigned)digitValue:(unichar)c { 73 | 74 | if ((c>47)&&(c<58)) { 75 | return (c-48); 76 | } 77 | 78 | return GGCharacterIsNotADigit; 79 | } 80 | 81 | - (unsigned long long)unsignedLongLongValue { 82 | unsigned n = [self length]; 83 | unsigned long long v,a; 84 | unsigned small_a, j; 85 | 86 | v=0; 87 | for (j=0;j 28 | 29 | 30 | @interface NSTask (Helper) 31 | 32 | + (NSString*)executeSynchronousTaskAtLaunchPath:(NSString*)launchPath; 33 | + (NSString*)executeSynchronousTaskAtLaunchPath:(NSString*)launchPath withArguments:(NSArray*)arguments; 34 | 35 | @end 36 | -------------------------------------------------------------------------------- /NSTask/NSTaskHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSTaskHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 11/18/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "NSTaskHelper.h" 28 | 29 | 30 | @implementation NSTask (Helper) 31 | 32 | + (NSString*)executeSynchronousTaskAtLaunchPath:(NSString*)launchPath { 33 | return [self executeSynchronousTaskAtLaunchPath:launchPath withArguments:nil]; 34 | } 35 | 36 | 37 | + (NSString*)executeSynchronousTaskAtLaunchPath:(NSString*)launchPath withArguments:(NSArray*)arguments { 38 | if(launchPath.length == 0) return nil; 39 | 40 | NSTask* task = [[NSTask alloc] init]; 41 | 42 | [task setLaunchPath:launchPath]; 43 | [task setArguments:arguments]; 44 | [task setStandardOutput:[NSPipe pipe]]; 45 | 46 | NSFileHandle* outputHandle = [[task standardOutput] fileHandleForReading]; 47 | 48 | [task launch]; 49 | 50 | NSData* outputData = [outputHandle readDataToEndOfFile]; 51 | 52 | [task release]; 53 | 54 | return [[[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding] autorelease]; 55 | } 56 | 57 | @end 58 | -------------------------------------------------------------------------------- /NSURL/NSURLHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSURLHelper.h 3 | // CocoaHelpers 4 | // 5 | // Created by Shaun Harrison on 12/19/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | 30 | @interface NSURL (Helper) 31 | 32 | /* 33 | * Returns a string of the base of the URL, will contain a trailing slash 34 | * 35 | * Example: 36 | * NSURL is http://www.cnn.com/full/path?query=string&key=value 37 | * baseString will return: http://www.cnn.com/ 38 | */ 39 | - (NSString*)baseString; 40 | 41 | @end 42 | -------------------------------------------------------------------------------- /NSURL/NSURLHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSURLHelper.m 3 | // CocoaHelpers 4 | // 5 | // Created by Shaun Harrison on 12/19/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "NSURLHelper.h" 28 | 29 | 30 | @implementation NSURL (Helper) 31 | 32 | - (NSString*)baseString { 33 | // Let's see if we can build it, it'll be the most accurate 34 | if([self scheme] && [self host]) { 35 | NSMutableString* baseString = [[NSMutableString alloc] initWithString:@""]; 36 | 37 | [baseString appendFormat:@"%@://", [self scheme]]; 38 | 39 | if([self user]) { 40 | if([self password]) { 41 | [baseString appendFormat:@"%@:%@@", [self user], [self password]]; 42 | } else { 43 | [baseString appendFormat:@"%@@", [self user]]; 44 | } 45 | } 46 | 47 | [baseString appendString:[self host]]; 48 | 49 | if([self port]) { 50 | [baseString appendFormat:@":%@", [[self port] integerValue]]; 51 | } 52 | 53 | [baseString appendString:@"/"]; 54 | 55 | return [baseString autorelease]; 56 | } 57 | 58 | // Oh Well, time to strip it down 59 | else { 60 | NSString* baseString = [self absoluteString]; 61 | 62 | if(![[self path] isEqualToString:@"/"]) { 63 | baseString = [baseString stringByReplacingOccurrencesOfString:[self path] withString:@""]; 64 | } 65 | 66 | if(self.query) { 67 | baseString = [baseString stringByReplacingOccurrencesOfString:[self query] withString:@""]; 68 | } 69 | 70 | baseString = [baseString stringByReplacingOccurrencesOfString:@"?" withString:@""]; 71 | 72 | if(![baseString hasSuffix:@"/"]) { 73 | baseString = [baseString stringByAppendingString:@"/"]; 74 | } 75 | 76 | return baseString; 77 | } 78 | } 79 | 80 | @end 81 | -------------------------------------------------------------------------------- /NSWorkspace/NSWorkspaceHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSWorkspaceHelper.h 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 11/18/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | 30 | @interface NSWorkspace (Helper) 31 | 32 | - (void)registerLoginLaunchBundle:(NSBundle*)bundle; 33 | - (void)unregisterLoginLaunchBundle:(NSBundle*)bundle; 34 | 35 | - (void)unregisterLoginLaunchApplication:(NSString*)appName; 36 | 37 | @end -------------------------------------------------------------------------------- /NSWorkspace/NSWorkspaceHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSWorkspaceHelper.m 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 11/18/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "NSWorkspaceHelper.h" 28 | 29 | @implementation NSWorkspace (Helper) 30 | 31 | - (void)registerLoginLaunchBundle:(NSBundle*)bundle { 32 | [self unregisterLoginLaunchBundle:bundle]; // Removes the old bundle, incase the application location changed 33 | 34 | NSURL* bundleURL = [NSURL fileURLWithPath:[bundle bundlePath] isDirectory:YES]; 35 | if(!bundleURL) return; 36 | 37 | LSSharedFileListRef loginList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); 38 | if(!loginList) return; 39 | 40 | LSSharedFileListItemRef loginItem; 41 | 42 | if((loginItem = LSSharedFileListInsertItemURL(loginList, kLSSharedFileListItemLast, NULL, NULL, (CFURLRef)bundleURL, NULL, NULL))) { 43 | CFRelease(loginItem); 44 | } 45 | 46 | CFRelease(loginList); 47 | } 48 | 49 | - (void)unregisterLoginLaunchBundle:(NSBundle*)bundle { 50 | NSString* bundleIdentifier = [bundle bundleIdentifier]; 51 | 52 | LSSharedFileListRef loginList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); 53 | if(!loginList) return; 54 | 55 | UInt32 seedValue; 56 | NSArray* loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(loginList, &seedValue); 57 | if(!loginItemsArray) return; 58 | 59 | for (id item in loginItemsArray) { 60 | LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item; 61 | CFURLRef bundleURL; 62 | 63 | if (LSSharedFileListItemResolve(itemRef, 0, &bundleURL, NULL) == noErr) { 64 | if([[[NSBundle bundleWithPath:(NSString*)CFURLGetString(bundleURL)] bundleIdentifier] isEqualToString:bundleIdentifier]) { 65 | LSSharedFileListItemRemove(loginList, itemRef); 66 | } 67 | } 68 | } 69 | 70 | [loginItemsArray release]; 71 | 72 | CFRelease(loginList); 73 | } 74 | 75 | - (void)unregisterLoginLaunchApplication:(NSString*)appName { 76 | LSSharedFileListRef loginList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); 77 | if(!loginList) return; 78 | 79 | UInt32 seedValue; 80 | NSArray* loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(loginList, &seedValue); 81 | if(!loginItemsArray) return; 82 | 83 | for (id item in loginItemsArray) { 84 | LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item; 85 | NSString* itemName = (NSString*)LSSharedFileListItemCopyDisplayName(itemRef); 86 | 87 | if (itemName) { 88 | if([itemName isEqualToString:appName]) { 89 | LSSharedFileListItemRemove(loginList, itemRef); 90 | } 91 | 92 | [itemName release]; 93 | } 94 | } 95 | 96 | [loginItemsArray release]; 97 | 98 | CFRelease(loginList); 99 | } 100 | 101 | @end 102 | -------------------------------------------------------------------------------- /NSXML/NSXMLElement.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSXMLElement.h 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 10/10/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | #if MAC_OS_X_VERSION_10_4 <= MAC_OS_X_VERSION_MAX_ALLOWED 30 | @interface NSXMLElement (Helper) 31 | 32 | - (NSXMLElement*)elementForName:(NSString*)name; 33 | 34 | @end 35 | #endif -------------------------------------------------------------------------------- /NSXML/NSXMLElement.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSXMLElement.m 3 | // Enormego Helpers 4 | // 5 | // Created by Shaun Harrison on 10/10/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "NSXMLElement.h" 28 | 29 | #if MAC_OS_X_VERSION_10_4 <= MAC_OS_X_VERSION_MAX_ALLOWED 30 | @implementation NSXMLElement (Helper) 31 | 32 | - (NSXMLElement*)elementForName:(NSString*)name { 33 | return [[self elementsForName:name] lastObject]; 34 | } 35 | 36 | @end 37 | #endif -------------------------------------------------------------------------------- /README.textile: -------------------------------------------------------------------------------- 1 | h1. Cocoa Helpers 2 | 3 | Created by enormego 4 | 5 | Cocoa Helpers are a collection of objective-c categories we've written for the standard Apple Cocoa/UIKit classes to make them more useful. 6 | 7 | h1. Documentation 8 | 9 | We're going to build our the wiki part as soon as we get some time, in the mean time, you can check out the header files where most methods are documented in line. 10 | 11 | h1. Questions 12 | 13 | Feel free to contact info@enormego.com if you need any help with any of these methods or wish to contribute to Cocoa Helpers. 14 | 15 | h1. License 16 | 17 | Cocoa-Helpers are available under the MIT license: 18 | 19 | _Copyright (c) 2009 enormego_ 20 | 21 | _Permission is hereby granted, free of charge, to any person obtaining a copy_ 22 | _of this software and associated documentation files (the "Software"), to deal_ 23 | _in the Software without restriction, including without limitation the rights_ 24 | _to use, copy, modify, merge, publish, distribute, sublicense, and/or sell_ 25 | _copies of the Software, and to permit persons to whom the Software is_ 26 | _furnished to do so, subject to the following conditions:_ 27 | 28 | _The above copyright notice and this permission notice shall be included in_ 29 | _all copies or substantial portions of the Software._ 30 | 31 | _THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR_ 32 | _IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,_ 33 | _FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE_ 34 | _AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER_ 35 | _LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,_ 36 | _OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN_ 37 | _THE SOFTWARE._ -------------------------------------------------------------------------------- /UIAlertView/UIAlertViewHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIAlertViewHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 10/16/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import 29 | 30 | /* 31 | * Convenience method to throw a quick alert to the user 32 | * Runs LocalizedString() on all strings 33 | */ 34 | void UIAlertViewQuick(NSString* title, NSString* message, NSString* dismissButtonTitle); 35 | 36 | @interface UIAlertView (Helper) 37 | 38 | @end 39 | #endif -------------------------------------------------------------------------------- /UIAlertView/UIAlertViewHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIAlertViewHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 10/16/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import "UIAlertViewHelper.h" 29 | #import "NSStringHelper.h" 30 | 31 | void UIAlertViewQuick(NSString* title, NSString* message, NSString* dismissButtonTitle) { 32 | UIAlertView* alert = [[UIAlertView alloc] initWithTitle:LocalizedString(title) 33 | message:LocalizedString(message) 34 | delegate:nil 35 | cancelButtonTitle:LocalizedString(dismissButtonTitle) 36 | otherButtonTitles:nil 37 | ]; 38 | [alert show]; 39 | [alert autorelease]; 40 | } 41 | 42 | 43 | @implementation UIAlertView (Helper) 44 | 45 | @end 46 | #endif -------------------------------------------------------------------------------- /UIApplication/UIApplicationHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIApplicationHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 8/25/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import 29 | 30 | 31 | @interface UIApplication (Helper) 32 | 33 | /** 34 | * Returns the path to the application's Documents directory. 35 | */ 36 | - (NSString *)documentsDirectory; 37 | 38 | /** 39 | * Sets the status bar style as well as the key window background color 40 | * UIStatusBarStyleDefault will result in a white background color 41 | * UIStatusBarStyleBlackTranslucent/Opaque will result in a black background color 42 | */ 43 | - (void)setApplicationStyle:(UIStatusBarStyle)style animated:(BOOL)animated; 44 | 45 | /** 46 | * Same as the above, however, you can specify the default/original/starting backgroundColor, instead of white. 47 | */ 48 | - (void)setApplicationStyle:(UIStatusBarStyle)style animated:(BOOL)animated defaultBackgroundColor:(UIColor*)defaultBackgroundColor; 49 | 50 | @end 51 | #endif -------------------------------------------------------------------------------- /UIApplication/UIApplicationHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIApplicationHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 8/25/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import "UIApplicationHelper.h" 29 | #import 30 | 31 | @implementation UIApplication (Helper) 32 | 33 | - (NSString *)documentsDirectory { 34 | return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 35 | } 36 | 37 | - (void)setApplicationStyle:(UIStatusBarStyle)style animated:(BOOL)animated { 38 | [self setApplicationStyle:style animated:animated defaultBackgroundColor:[UIColor whiteColor]]; 39 | } 40 | 41 | - (void)setApplicationStyle:(UIStatusBarStyle)style animated:(BOOL)animated defaultBackgroundColor:(UIColor*)defaultBackgroundColor { 42 | [self setStatusBarStyle:style animated:animated]; 43 | 44 | UIColor* newBackgroundColor = style == UIStatusBarStyleDefault ? defaultBackgroundColor : [UIColor blackColor]; 45 | UIColor* oldBackgroundColor = style == UIStatusBarStyleDefault ? [UIColor blackColor] : defaultBackgroundColor; 46 | 47 | if(animated) { 48 | [CATransaction setValue:[NSNumber numberWithFloat:0.3] forKey:kCATransactionAnimationDuration]; 49 | 50 | CABasicAnimation* fadeAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"]; 51 | fadeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 52 | fadeAnimation.fromValue = (id)oldBackgroundColor.CGColor; 53 | fadeAnimation.toValue = (id)newBackgroundColor.CGColor; 54 | fadeAnimation.fillMode = kCAFillModeForwards; 55 | fadeAnimation.removedOnCompletion = NO; 56 | [self.keyWindow.layer addAnimation:fadeAnimation forKey:@"fadeAnimation"]; 57 | [CATransaction commit]; 58 | } else { 59 | self.keyWindow.backgroundColor = newBackgroundColor; 60 | } 61 | } 62 | 63 | @end 64 | #endif -------------------------------------------------------------------------------- /UIColor/UIColorHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIColorHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 11/20/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import 29 | 30 | /* 31 | * Convenience method to return a UIColor with RGB values based on 255 32 | */ 33 | 34 | UIColor* UIColorMakeRGB(CGFloat red, CGFloat green, CGFloat blue); 35 | 36 | @interface UIColor (Helper) 37 | 38 | @end 39 | #endif -------------------------------------------------------------------------------- /UIColor/UIColorHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIColorHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 11/20/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import "UIColorHelper.h" 29 | 30 | UIColor* UIColorMakeRGB(CGFloat red, CGFloat green, CGFloat blue) { 31 | return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1.0f]; 32 | } 33 | 34 | @implementation UIColor (Helper) 35 | 36 | @end 37 | #endif -------------------------------------------------------------------------------- /UIDevice/UIDeviceHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIDeviceHelper.h 3 | // CocoaHelpers 4 | // 5 | // Created by Shaun Harrison on 12/11/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | 30 | @interface UIDevice (Helper) 31 | 32 | /* 33 | * Available device memory in MB 34 | */ 35 | @property(readonly) double availableMemory; 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /UIDevice/UIDeviceHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIDeviceHelper.m 3 | // CocoaHelpers 4 | // 5 | // Created by Shaun Harrison on 12/11/08. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "UIDeviceHelper.h" 28 | #include 29 | #include 30 | 31 | @implementation UIDevice (Helper) 32 | 33 | - (double)availableMemory { 34 | vm_statistics_data_t vmStats; 35 | mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT; 36 | kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount); 37 | 38 | if(kernReturn != KERN_SUCCESS) { 39 | return NSNotFound; 40 | } 41 | 42 | return ((vm_page_size * vmStats.free_count) / 1024.0) / 1024.0; 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /UIImage/UIImageHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIImageHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Devin Doty on 1/13/2010. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import 29 | 30 | 31 | @interface UIImage (Helper) 32 | 33 | /* 34 | * Creates an image from the contents of a URL 35 | */ 36 | + (UIImage*)imageWithContentsOfURL:(NSURL*)url; 37 | 38 | /* 39 | * Creates an image with a path compontent relative to 40 | * the main bundle's resource path 41 | */ 42 | + (UIImage*)imageWithResourcesPathCompontent:(NSString*)pathCompontent; 43 | 44 | /* 45 | * Scales the image to the given size, NOT aspect 46 | */ 47 | - (UIImage*)scaleToSize:(CGSize)size; 48 | 49 | /* 50 | * Aspect scale with border color, and corner radius, and shadow 51 | */ 52 | - (UIImage*)aspectScaleToMaxSize:(CGFloat)size withBorderSize:(CGFloat)borderSize borderColor:(UIColor*)aColor cornerRadius:(CGFloat)aRadius shadowOffset:(CGSize)aOffset shadowBlurRadius:(CGFloat)aBlurRadius shadowColor:(UIColor*)aShadowColor; 53 | 54 | /* 55 | * Aspect scale with a shadow 56 | */ 57 | - (UIImage*)aspectScaleToMaxSize:(CGFloat)size withShadowOffset:(CGSize)aOffset blurRadius:(CGFloat)aRadius color:(UIColor*)aColor; 58 | 59 | /* 60 | * Aspect scale with corner radius 61 | */ 62 | - (UIImage*)aspectScaleToMaxSize:(CGFloat)size withCornerRadius:(CGFloat)aRadius; 63 | 64 | /* 65 | * Aspect scales the image to a max size 66 | */ 67 | - (UIImage*)aspectScaleToMaxSize:(CGFloat)size; 68 | 69 | /* 70 | * Aspect scales the image to a rect size 71 | */ 72 | - (UIImage*)aspectScaleToSize:(CGSize)size; 73 | 74 | /* 75 | * Masks the context with the image, then fills with the color 76 | */ 77 | - (void)drawInRect:(CGRect)rect withAlphaMaskColor:(UIColor*)aColor; 78 | 79 | /* 80 | * Masks the context with the image, then fills with the gradient (two colors in an array) 81 | */ 82 | - (void)drawInRect:(CGRect)rect withAlphaMaskGradient:(NSArray*)colors; 83 | 84 | 85 | @end 86 | #endif -------------------------------------------------------------------------------- /UIImage/UIImageHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIImageHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Devin Doty on 1/13/2010. 6 | // Copyright (c) 2008-2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import "UIImageHelper.h" 29 | #import 30 | 31 | @implementation UIImage (Helper) 32 | 33 | CGFloat degreesToRadiens(CGFloat degrees){ 34 | return degrees * M_PI / 180.0f; 35 | } 36 | 37 | + (UIImage*)imageWithContentsOfURL:(NSURL*)url { 38 | NSError* error; 39 | NSData* data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url] returningResponse:NULL error:NULL]; 40 | if(error || !data) { 41 | return nil; 42 | } else { 43 | return [UIImage imageWithData:data]; 44 | } 45 | } 46 | 47 | + (UIImage*)imageWithResourcesPathCompontent:(NSString*)pathCompontent { 48 | return [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathCompontent]]; 49 | } 50 | 51 | - (UIImage*)scaleToSize:(CGSize)size { 52 | 53 | #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 54 | if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 55 | UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]); 56 | } else { 57 | UIGraphicsBeginImageContext(size); 58 | } 59 | #else 60 | UIGraphicsBeginImageContext(size); 61 | #endif 62 | 63 | [self drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)]; 64 | UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 65 | UIGraphicsEndImageContext(); 66 | 67 | return scaledImage; 68 | } 69 | 70 | - (UIImage*)aspectScaleToMaxSize:(CGFloat)size withBorderSize:(CGFloat)borderSize borderColor:(UIColor*)aColor cornerRadius:(CGFloat)aRadius shadowOffset:(CGSize)aOffset shadowBlurRadius:(CGFloat)aBlurRadius shadowColor:(UIColor*)aShadowColor{ 71 | 72 | CGSize imageSize = CGSizeMake(self.size.width, self.size.height); 73 | 74 | CGFloat hScaleFactor = imageSize.width / size; 75 | CGFloat vScaleFactor = imageSize.height / size; 76 | 77 | CGFloat scaleFactor = MAX(hScaleFactor, vScaleFactor); 78 | 79 | CGFloat newWidth = imageSize.width / scaleFactor; 80 | CGFloat newHeight = imageSize.height / scaleFactor; 81 | 82 | CGRect imageRect = CGRectMake(borderSize, borderSize, newWidth, newHeight); 83 | 84 | 85 | #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 86 | if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 87 | UIGraphicsBeginImageContextWithOptions(CGSizeMake(newWidth + (borderSize*2), newHeight + (borderSize*2)), NO, [[UIScreen mainScreen] scale]); 88 | } else { 89 | UIGraphicsBeginImageContext(CGSizeMake(newWidth + (borderSize*2), newHeight + (borderSize*2))); 90 | } 91 | #else 92 | UIGraphicsBeginImageContext(CGSizeMake(newWidth + (borderSize*2), newHeight + (borderSize*2))); 93 | #endif 94 | 95 | 96 | CGContextRef imageContext = UIGraphicsGetCurrentContext(); 97 | CGContextSaveGState(imageContext); 98 | CGPathRef path = NULL; 99 | 100 | if (aRadius > 0.0f) { 101 | 102 | CGFloat radius; 103 | radius = MIN(aRadius, floorf(imageRect.size.width/2)); 104 | float x0 = CGRectGetMinX(imageRect), y0 = CGRectGetMinY(imageRect), x1 = CGRectGetMaxX(imageRect), y1 = CGRectGetMaxY(imageRect); 105 | 106 | CGContextBeginPath(imageContext); 107 | CGContextMoveToPoint(imageContext, x0+radius, y0); 108 | CGContextAddArcToPoint(imageContext, x1, y0, x1, y1, radius); 109 | CGContextAddArcToPoint(imageContext, x1, y1, x0, y1, radius); 110 | CGContextAddArcToPoint(imageContext, x0, y1, x0, y0, radius); 111 | CGContextAddArcToPoint(imageContext, x0, y0, x1, y0, radius); 112 | CGContextClosePath(imageContext); 113 | path = CGContextCopyPath(imageContext); 114 | CGContextClip(imageContext); 115 | 116 | } 117 | 118 | [self drawInRect:imageRect]; 119 | CGContextRestoreGState(imageContext); 120 | 121 | if (borderSize > 0.0f) { 122 | 123 | CGContextSetLineWidth(imageContext, borderSize); 124 | [aColor != nil ? aColor : [UIColor blackColor] setStroke]; 125 | 126 | if(path == NULL){ 127 | 128 | CGContextStrokeRect(imageContext, imageRect); 129 | 130 | } else { 131 | 132 | CGContextAddPath(imageContext, path); 133 | CGContextStrokePath(imageContext); 134 | 135 | } 136 | } 137 | 138 | if(path != NULL){ 139 | CGPathRelease(path); 140 | } 141 | 142 | UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 143 | UIGraphicsEndImageContext(); 144 | 145 | if (aBlurRadius > 0.0f) { 146 | 147 | #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 148 | if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 149 | UIGraphicsBeginImageContextWithOptions(CGSizeMake(scaledImage.size.width + (aBlurRadius*2), scaledImage.size.height + (aBlurRadius*2)), NO, [[UIScreen mainScreen] scale]); 150 | } else { 151 | UIGraphicsBeginImageContext(CGSizeMake(scaledImage.size.width + (aBlurRadius*2), scaledImage.size.height + (aBlurRadius*2))); 152 | } 153 | #else 154 | UIGraphicsBeginImageContext(CGSizeMake(scaledImage.size.width + (aBlurRadius*2), scaledImage.size.height + (aBlurRadius*2))); 155 | #endif 156 | 157 | CGContextRef imageShadowContext = UIGraphicsGetCurrentContext(); 158 | 159 | if (aShadowColor!=nil) { 160 | CGContextSetShadowWithColor(imageShadowContext, aOffset, aBlurRadius, aShadowColor.CGColor); 161 | } else { 162 | CGContextSetShadow(imageShadowContext, aOffset, aBlurRadius); 163 | } 164 | 165 | [scaledImage drawInRect:CGRectMake(aBlurRadius, aBlurRadius, scaledImage.size.width, scaledImage.size.height)]; 166 | scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 167 | UIGraphicsEndImageContext(); 168 | 169 | } 170 | 171 | return scaledImage; 172 | } 173 | 174 | - (UIImage*)aspectScaleToMaxSize:(CGFloat)size withShadowOffset:(CGSize)aOffset blurRadius:(CGFloat)aRadius color:(UIColor*)aColor{ 175 | return [self aspectScaleToMaxSize:size withBorderSize:0 borderColor:nil cornerRadius:0 shadowOffset:aOffset shadowBlurRadius:aRadius shadowColor:aColor]; 176 | } 177 | 178 | - (UIImage*)aspectScaleToMaxSize:(CGFloat)size withCornerRadius:(CGFloat)aRadius{ 179 | 180 | return [self aspectScaleToMaxSize:size withBorderSize:0 borderColor:nil cornerRadius:aRadius shadowOffset:CGSizeZero shadowBlurRadius:0.0f shadowColor:nil]; 181 | } 182 | 183 | - (UIImage*)aspectScaleToMaxSize:(CGFloat)size{ 184 | 185 | return [self aspectScaleToMaxSize:size withBorderSize:0 borderColor:nil cornerRadius:0 shadowOffset:CGSizeZero shadowBlurRadius:0.0f shadowColor:nil]; 186 | } 187 | 188 | - (UIImage*)aspectScaleToSize:(CGSize)size{ 189 | 190 | CGSize imageSize = CGSizeMake(self.size.width, self.size.height); 191 | 192 | CGFloat hScaleFactor = imageSize.width / size.width; 193 | CGFloat vScaleFactor = imageSize.height / size.height; 194 | 195 | CGFloat scaleFactor = MAX(hScaleFactor, vScaleFactor); 196 | 197 | CGFloat newWidth = imageSize.width / scaleFactor; 198 | CGFloat newHeight = imageSize.height / scaleFactor; 199 | 200 | // center vertically or horizontally in size passed 201 | CGFloat leftOffset = (size.width - newWidth) / 2; 202 | CGFloat topOffset = (size.height - newHeight) / 2; 203 | 204 | #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000 205 | if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 206 | UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), NO, [[UIScreen mainScreen] scale]); 207 | } else { 208 | UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height)); 209 | } 210 | #else 211 | UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height)); 212 | #endif 213 | 214 | [self drawInRect:CGRectMake(leftOffset, topOffset, newWidth, newHeight)]; 215 | UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 216 | UIGraphicsEndImageContext(); 217 | 218 | return scaledImage; 219 | } 220 | 221 | - (CGSize)aspectScaleSize:(CGFloat)size{ 222 | 223 | CGSize imageSize = CGSizeMake(self.size.width, self.size.height); 224 | 225 | CGFloat hScaleFactor = imageSize.width / size; 226 | CGFloat vScaleFactor = imageSize.height / size; 227 | 228 | CGFloat scaleFactor = MAX(hScaleFactor, vScaleFactor); 229 | 230 | CGFloat newWidth = imageSize.width / scaleFactor; 231 | CGFloat newHeight = imageSize.height / scaleFactor; 232 | 233 | return CGSizeMake(newWidth, newHeight); 234 | 235 | } 236 | 237 | - (void)drawInRect:(CGRect)rect withAlphaMaskColor:(UIColor*)aColor{ 238 | 239 | CGContextRef context = UIGraphicsGetCurrentContext(); 240 | 241 | CGContextSaveGState(context); 242 | 243 | CGContextTranslateCTM(context, 0.0, rect.size.height); 244 | CGContextScaleCTM(context, 1.0, -1.0); 245 | 246 | rect.origin.y = rect.origin.y * -1; 247 | const CGFloat *color = CGColorGetComponents(aColor.CGColor); 248 | CGContextClipToMask(context, rect, self.CGImage); 249 | CGContextSetRGBFillColor(context, color[0], color[1], color[2], color[3]); 250 | CGContextFillRect(context, rect); 251 | 252 | CGContextRestoreGState(context); 253 | } 254 | 255 | - (void)drawInRect:(CGRect)rect withAlphaMaskGradient:(NSArray*)colors{ 256 | 257 | NSAssert([colors count]==2, @"an array containing two UIColor variables must be passed to drawInRect:withAlphaMaskGradient:"); 258 | 259 | CGContextRef context = UIGraphicsGetCurrentContext(); 260 | 261 | CGContextSaveGState(context); 262 | 263 | CGContextTranslateCTM(context, 0.0, rect.size.height); 264 | CGContextScaleCTM(context, 1.0, -1.0); 265 | 266 | rect.origin.y = rect.origin.y * -1; 267 | 268 | CGContextClipToMask(context, rect, self.CGImage); 269 | 270 | const CGFloat *top = CGColorGetComponents(((UIColor*)[colors objectAtIndex:0]).CGColor); 271 | const CGFloat *bottom = CGColorGetComponents(((UIColor*)[colors objectAtIndex:1]).CGColor); 272 | 273 | CGColorSpaceRef _rgb = CGColorSpaceCreateDeviceRGB(); 274 | size_t _numLocations = 2; 275 | CGFloat _locations[2] = { 0.0, 1.0 }; 276 | CGFloat _colors[8] = { top[0], top[1], top[2], top[3], bottom[0], bottom[1], bottom[2], bottom[3] }; 277 | CGGradientRef gradient = CGGradientCreateWithColorComponents(_rgb, _colors, _locations, _numLocations); 278 | CGColorSpaceRelease(_rgb); 279 | 280 | CGPoint start = CGPointMake(CGRectGetMidX(rect), rect.origin.y); 281 | CGPoint end = CGPointMake(CGRectGetMidX(rect), rect.size.height); 282 | 283 | CGContextClipToRect(context, rect); 284 | CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); 285 | 286 | CGGradientRelease(gradient); 287 | 288 | CGContextRestoreGState(context); 289 | 290 | } 291 | 292 | @end 293 | #endif -------------------------------------------------------------------------------- /UITableView/UITableViewContentUnavailableView.h: -------------------------------------------------------------------------------- 1 | // 2 | // UITableViewContentUnavailableView.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 2/24/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import 29 | 30 | // Used by the UITableView helper, not intended for public use 31 | 32 | @interface UITableViewContentUnavailableView : UIView { 33 | @private 34 | UILabel* label; 35 | } 36 | 37 | @property(nonatomic,copy) NSString* text; 38 | @property(nonatomic,readonly) UILabel* label; 39 | @end 40 | #endif -------------------------------------------------------------------------------- /UITableView/UITableViewContentUnavailableView.m: -------------------------------------------------------------------------------- 1 | // 2 | // UITableViewContentUnavailableView.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 2/24/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import "UITableViewContentUnavailableView.h" 29 | 30 | 31 | @implementation UITableViewContentUnavailableView 32 | @synthesize label; 33 | 34 | - (id)initWithFrame:(CGRect)frame { 35 | if (self = [super initWithFrame:frame]) { 36 | self.backgroundColor = [UIColor whiteColor]; 37 | self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 38 | 39 | label = [[UILabel alloc] initWithFrame:CGRectMake(15.0f, 0.0f, frame.size.width-30.0f, frame.size.height)]; 40 | label.backgroundColor = self.backgroundColor; 41 | label.autoresizingMask = self.autoresizingMask; 42 | label.text = LocalizedString(@"Content Unavailable"); 43 | label.textAlignment = UITextAlignmentCenter; 44 | label.font = [UIFont boldSystemFontOfSize:13.0f]; 45 | label.textColor = UIColorMakeRGB(0, 91, 132); 46 | label.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.5f]; 47 | label.shadowOffset = CGSizeMake(0.0f, 1.0f); 48 | label.numberOfLines = 0; 49 | 50 | [self addSubview:label]; 51 | } 52 | 53 | return self; 54 | } 55 | 56 | - (void)setText:(NSString*)text { 57 | label.text = text; 58 | } 59 | 60 | - (NSString*)text { 61 | return label.text; 62 | } 63 | 64 | - (void)drawRect:(CGRect)rect { 65 | // Drawing code 66 | } 67 | 68 | 69 | - (void)dealloc { 70 | [label release]; 71 | [super dealloc]; 72 | } 73 | 74 | 75 | @end 76 | #endif -------------------------------------------------------------------------------- /UITableView/UITableViewHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // UITableViewHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 2/24/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import 29 | 30 | 31 | @interface UITableView (Helper) 32 | 33 | // If active, adds an activity view to bottom of a UITableView set to "Update…" 34 | - (void)setActivity:(BOOL)hasActivity; 35 | 36 | // If active, adds an activity view to bottom of a UITableView with desired title 37 | - (void)setActivity:(BOOL)hasActivity title:(NSString*)title; 38 | 39 | // If content is unavailable, overlays the UITableView with a message indicating there's no content 40 | // Default message is "Content Unavailable". Will check for delegate method: tableViewContentUnavailableText: 41 | - (void)setContentUnavailable:(BOOL)isUnavailable; 42 | 43 | // Allows you to set everything before y=0.0f to a different color than the tableView's backgroundColor 44 | @property(nonatomic,retain) UIColor* headerBackgroundColor; 45 | @end 46 | 47 | @protocol UITableViewContentUnavailableDataSource 48 | // Default: "Content Unavailable" 49 | - (NSString*)tableViewContentUnavailableText:(UITableView*)tableView; 50 | 51 | // Default: Dark Blue 52 | - (UIColor*)tableViewContentUnavailableTextColor:(UITableView*)tableView; 53 | 54 | // Default: Black, 0.5 Alpha 55 | - (UIColor*)tableViewContentUnavailableTextShadowColor:(UITableView*)tableView; 56 | 57 | // Default: White 58 | - (UIColor*)tableViewContentUnavailableBackgroundColor:(UITableView*)tableView; 59 | @end 60 | 61 | #endif -------------------------------------------------------------------------------- /UITableView/UITableViewHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UITableViewHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 2/24/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import "UITableViewHelper.h" 29 | #import "UITableViewContentUnavailableView.h" 30 | #import "UITableViewUpdatingView.h" 31 | 32 | #define UPDATING_OFFSET_Y (self.frame.size.height - 42.0f - 10.0f) 33 | #define BACKGROUND_HEADER_TAG 0x8577 34 | #define ACTIVITY_TAG 0x5646 35 | 36 | @interface UITableView (HelperPrivate) 37 | - (UIView*)overlayView; 38 | - (UITableViewUpdatingView*)updatingView; 39 | @end 40 | 41 | 42 | @implementation UITableView (Helper) 43 | 44 | - (UIColor*)headerBackgroundColor { 45 | return [self viewWithTag:BACKGROUND_HEADER_TAG].backgroundColor; 46 | } 47 | 48 | - (void)setHeaderBackgroundColor:(UIColor*)color { 49 | UIView* headerBackgroundView; 50 | 51 | if(!(headerBackgroundView = [self viewWithTag:BACKGROUND_HEADER_TAG])) { 52 | headerBackgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0 - self.bounds.size.height, self.bounds.size.width, self.bounds.size.height)] autorelease]; 53 | headerBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 54 | headerBackgroundView.tag = BACKGROUND_HEADER_TAG; 55 | [self insertSubview:headerBackgroundView atIndex:0]; 56 | } 57 | 58 | headerBackgroundView.backgroundColor = color; 59 | } 60 | 61 | - (void)setActivity:(BOOL)hasActivity { 62 | [self setActivity:hasActivity title:[UITableViewUpdatingView defaultTitle]]; 63 | } 64 | 65 | - (void)setActivity:(BOOL)hasActivity title:(NSString*)title { 66 | [[self updatingView] removeFromSuperview]; 67 | 68 | if(hasActivity) { 69 | CGRect updatingRect; 70 | updatingRect.size = CGSizeMake([UITableViewUpdatingView widthForTitle:title], 42.0f); 71 | updatingRect.origin.y = UPDATING_OFFSET_Y; 72 | updatingRect.origin.x = roundf((self.frame.size.width - updatingRect.size.width) / 2); 73 | 74 | UITableViewUpdatingView* updatingView = [[UITableViewUpdatingView alloc] initWithFrame:updatingRect title:title]; 75 | updatingView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin; 76 | updatingView.tag = ACTIVITY_TAG; 77 | [self addSubview:updatingView]; 78 | [updatingView release]; 79 | } 80 | } 81 | 82 | - (void)setContentUnavailable:(BOOL)isUnavailable { 83 | self.scrollEnabled = !isUnavailable; 84 | [[self overlayView] removeFromSuperview]; 85 | 86 | if(isUnavailable) { 87 | CGRect frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height); 88 | if(self.tableHeaderView) { 89 | frame.origin.y = self.tableHeaderView.frame.size.height; 90 | frame.size.height -= frame.origin.y; 91 | } 92 | 93 | frame.size.height -= self.contentInset.top; 94 | frame.size.height -= self.contentInset.bottom; 95 | 96 | frame.size.width -= self.contentInset.left; 97 | frame.size.width -= self.contentInset.left; 98 | 99 | UITableViewContentUnavailableView* view = [[UITableViewContentUnavailableView alloc] initWithFrame:frame]; 100 | if([self.dataSource respondsToSelector:@selector(tableViewContentUnavailableText:)]) { 101 | view.text = [(id)self.dataSource tableViewContentUnavailableText:self]; 102 | } 103 | 104 | if([self.dataSource respondsToSelector:@selector(tableViewContentUnavailableBackgroundColor:)]) { 105 | view.backgroundColor = [(id)self.dataSource tableViewContentUnavailableBackgroundColor:self]; 106 | view.label.backgroundColor = view.backgroundColor; 107 | } 108 | 109 | if([self.dataSource respondsToSelector:@selector(tableViewContentUnavailableTextColor:)]) { 110 | view.label.textColor = [(id)self.dataSource tableViewContentUnavailableTextColor:self]; 111 | } 112 | 113 | if([self.dataSource respondsToSelector:@selector(tableViewContentUnavailableTextShadowColor:)]) { 114 | view.label.shadowColor = [(id)self.dataSource tableViewContentUnavailableTextShadowColor:self]; 115 | } 116 | 117 | [self addSubview:view]; 118 | [view release]; 119 | } 120 | } 121 | 122 | - (void)didAddSubview:(UIView *)subview { 123 | [super didAddSubview:subview]; 124 | UIView* overlayView = [self overlayView]; 125 | if(overlayView) { 126 | [self bringSubviewToFront:overlayView]; 127 | } 128 | 129 | UIView* updatingView = [self updatingView]; 130 | if(updatingView) { 131 | [self bringSubviewToFront:updatingView]; 132 | } 133 | } 134 | 135 | - (UIView*)overlayView { 136 | for(UIView* view in self.subviews) { 137 | if([view isKindOfClass:[UITableViewContentUnavailableView class]]) { 138 | return view; 139 | } 140 | } 141 | 142 | return nil; 143 | } 144 | 145 | - (UITableViewUpdatingView*)updatingView { 146 | return (UITableViewUpdatingView*)[self viewWithTag:ACTIVITY_TAG]; 147 | } 148 | 149 | - (void)setContentOffset:(CGPoint)point { 150 | [super setContentOffset:point]; 151 | UIView* updatingView = [self updatingView]; 152 | if(updatingView) { 153 | updatingView.frame = CGRectMake(updatingView.frame.origin.x, UPDATING_OFFSET_Y + point.y, updatingView.frame.size.width, updatingView.frame.size.height); 154 | } 155 | } 156 | 157 | @end 158 | #endif -------------------------------------------------------------------------------- /UITableView/UITableViewUpdatingView.h: -------------------------------------------------------------------------------- 1 | // 2 | // UITableViewUpdatingView.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 2/24/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import 29 | 30 | // Used by the UITableView helper, not intended for public use 31 | 32 | @interface UITableViewUpdatingView : UIView { 33 | @private 34 | UIActivityIndicatorView* activitiyIndicatorView; 35 | UILabel* label; 36 | } 37 | 38 | - (id)initWithFrame:(CGRect)frame title:(NSString*)title; 39 | 40 | + (NSString*)defaultTitle; 41 | + (CGFloat)widthForTitle:(NSString*)title; 42 | 43 | @end 44 | #endif -------------------------------------------------------------------------------- /UITableView/UITableViewUpdatingView.m: -------------------------------------------------------------------------------- 1 | // 2 | // UITableViewUpdatingView.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 2/24/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "UITableViewUpdatingView.h" 28 | #import "NSStringHelper.h" 29 | #import 30 | 31 | #define LABEL_FONT [UIFont boldSystemFontOfSize:16.0f] 32 | 33 | @implementation UITableViewUpdatingView 34 | 35 | - (id)initWithFrame:(CGRect)frame { 36 | return [self initWithFrame:frame title:[[self class] defaultTitle]]; 37 | } 38 | 39 | - (id)initWithFrame:(CGRect)frame title:(NSString*)title { 40 | if (self = [super initWithFrame:frame]) { 41 | self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f]; 42 | self.layer.masksToBounds = YES; 43 | self.layer.cornerRadius = 9.0f; 44 | 45 | label = [[UILabel alloc] initWithFrame:CGRectMake(39.0f, 0.0f, frame.size.width-50.0f, frame.size.height)]; 46 | label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 47 | label.backgroundColor = [UIColor clearColor]; 48 | label.textColor = [UIColor whiteColor]; 49 | label.font = LABEL_FONT; 50 | label.text = title; 51 | label.textAlignment = UITextAlignmentCenter; 52 | [self addSubview:label]; 53 | 54 | activitiyIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 55 | activitiyIndicatorView.frame = CGRectMake(11.0f, floorf((frame.size.height-20.0f) / 2.0f), 20.0f, 20.0f); 56 | activitiyIndicatorView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin; 57 | [self addSubview:activitiyIndicatorView]; 58 | [activitiyIndicatorView startAnimating]; 59 | } 60 | 61 | return self; 62 | } 63 | 64 | + (NSString*)defaultTitle { 65 | return LocalizedString(@"Updating…"); 66 | } 67 | 68 | + (CGFloat)widthForTitle:(NSString*)title { 69 | if(!title) { 70 | title = [self defaultTitle]; 71 | } 72 | 73 | return [title sizeWithFont:LABEL_FONT].width + 50.0f; 74 | } 75 | 76 | - (void)dealloc { 77 | [activitiyIndicatorView release]; 78 | [label release]; 79 | [super dealloc]; 80 | } 81 | 82 | 83 | @end 84 | -------------------------------------------------------------------------------- /UITableViewController/UITableViewControllerHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // UITableViewControllerHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 2/16/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import 29 | 30 | @interface UITableViewController (Helper) 31 | 32 | // Returns height of a cell given the desired attributes, font-size is 14.0f (This should be change-able) 33 | - (CGFloat)cellHeightWithText:(NSString*)text padding:(float)padding; 34 | - (CGFloat)cellHeightWithText:(NSString*)text padding:(float)padding minimumHeight:(float)minimumHeight; 35 | - (CGFloat)cellHeightWithText:(NSString*)text padding:(float)padding maximumHeight:(float)maximumHeight; 36 | - (CGFloat)cellHeightWithText:(NSString*)text padding:(float)padding minimumHeight:(float)minimumHeight maximumHeight:(float)maximumHeight; 37 | 38 | // Returns an NSArray of generated NSIndexPaths based on parameters 39 | - (NSArray*)indexPathsFromRow:(NSUInteger)fromRow toRow:(NSUInteger)toRow inSection:(NSUInteger)inSection; 40 | 41 | // Returns an NSArray containing a single NSIndexPath 42 | - (NSArray*)indexPathsForRow:(NSUInteger)forRow inSection:(NSUInteger)inSection; 43 | @end 44 | #endif -------------------------------------------------------------------------------- /UITableViewController/UITableViewControllerHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UITableViewControllerHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 2/16/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import "UITableViewControllerHelper.h" 29 | 30 | @implementation UITableViewController (Helper) 31 | 32 | - (CGFloat)cellHeightWithText:(NSString*)text padding:(float)padding { 33 | return [self cellHeightWithText:text padding:padding minimumHeight:0.0f maximumHeight:0.0f]; 34 | } 35 | 36 | - (CGFloat)cellHeightWithText:(NSString*)text padding:(float)padding minimumHeight:(float)minimumHeight { 37 | return [self cellHeightWithText:text padding:padding minimumHeight:minimumHeight maximumHeight:0.0f]; 38 | } 39 | 40 | - (CGFloat)cellHeightWithText:(NSString*)text padding:(float)padding maximumHeight:(float)maximumHeight { 41 | return [self cellHeightWithText:text padding:padding minimumHeight:0.0f maximumHeight:maximumHeight]; 42 | } 43 | 44 | - (CGFloat)cellHeightWithText:(NSString*)text padding:(float)padding minimumHeight:(float)minimumHeight maximumHeight:(float)maximumHeight { 45 | CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(300.0f, 99999999.0f) lineBreakMode:UILineBreakModeWordWrap]; 46 | float height = size.height + padding; 47 | if(minimumHeight > 0.0f && height < minimumHeight) { 48 | height = minimumHeight; 49 | } 50 | 51 | if(maximumHeight > 0.0f && height > maximumHeight) { 52 | height = maximumHeight; 53 | } 54 | 55 | return height; 56 | } 57 | 58 | - (NSArray*)indexPathsFromRow:(NSUInteger)fromRow toRow:(NSUInteger)toRow inSection:(NSUInteger)inSection { 59 | NSMutableArray* indexPaths = [NSMutableArray arrayWithCapacity:toRow-fromRow]; 60 | int x; 61 | for(x=fromRow;x<=toRow;x++) { 62 | [indexPaths addObject:[NSIndexPath indexPathForRow:x inSection:inSection]]; 63 | } 64 | 65 | return indexPaths; 66 | } 67 | 68 | - (NSArray*)indexPathsForRow:(NSUInteger)forRow inSection:(NSUInteger)inSection { 69 | return [NSArray arrayWithObject:[NSIndexPath indexPathForRow:forRow inSection:inSection]]; 70 | } 71 | 72 | @end 73 | #endif -------------------------------------------------------------------------------- /UIToolbar/UIToolbarEGOHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIToolbarEGOHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 4/25/10. 6 | // Copyright 2010 enormego. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | 30 | @interface UIToolbar (EGOHelper) 31 | 32 | - (void)setItemTitle:(NSString*)title forTag:(NSInteger)tag; 33 | 34 | - (UIBarButtonItem*)itemWithTag:(NSInteger)tag; 35 | - (NSUInteger)indexOfItemWithTag:(NSInteger)tag; 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /UIToolbar/UIToolbarEGOHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIToolbarEGOHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 4/25/10. 6 | // Copyright 2010 enormego. All rights reserved. 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #import "UIToolbarEGOHelper.h" 28 | 29 | 30 | @implementation UIToolbar (EGOHelper) 31 | 32 | - (void)setItemTitle:(NSString*)title forTag:(NSInteger)tag { 33 | UIBarButtonItem* item = [self itemWithTag:tag]; 34 | if(!item) return; 35 | item.title = title ? title : @""; 36 | } 37 | 38 | - (UIBarButtonItem*)itemWithTag:(NSInteger)tag { 39 | for(UIBarButtonItem* item in self.items) { 40 | if(item.tag == tag) { 41 | return item; 42 | } 43 | } 44 | 45 | return nil; 46 | } 47 | 48 | - (NSUInteger)indexOfItemWithTag:(NSInteger)tag { 49 | return [self.items indexOfObject:[self itemWithTag:tag]]; 50 | } 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /UIView/UIViewHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 1/8/10. 6 | // Copyright (c) 2010 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import 29 | 30 | #define UIViewAutoresizingFlexibleMargins UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin 31 | #define UIViewAutoresizingFlexibleSize UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight 32 | 33 | // Device Independent Width 34 | // Calculates a width based on baseWidth, and scales it up or down depending on actualWidth 35 | CGFloat DIW(CGFloat width); // baseWidth = 320, actualWidth = device width 36 | CGFloat DIWW(CGFloat width, CGFloat baseWidth, CGFloat actualWidth); 37 | 38 | @interface UIView (EGOHelper) 39 | 40 | /* 41 | * Steps through the current views superview hiearchy until it finds a view with the given class 42 | * If strict is set to NO, it will match using isKindOfClass: 43 | * If strict is set to YES, it will match using isMemberOfClass: 44 | */ 45 | - (UIView*)superviewWithClass:(Class)svClass; // strict:NO 46 | - (UIView*)superviewWithClass:(Class)svClass strict:(BOOL)strict; 47 | - (void)setDebug:(BOOL)val; 48 | @end 49 | 50 | #endif -------------------------------------------------------------------------------- /UIView/UIViewHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 1/8/10. 6 | // Copyright (c) 2010 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | 29 | #import "UIViewHelper.h" 30 | 31 | CGFloat DIW(CGFloat width) { 32 | return DIWW(width, 320.0f, UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? [UIScreen mainScreen].bounds.size.height : [UIScreen mainScreen].bounds.size.width); 33 | } 34 | 35 | CGFloat DIWW(CGFloat width, CGFloat baseWidth, CGFloat actualWidth) { 36 | return floorf((width * actualWidth) / baseWidth); 37 | } 38 | 39 | @implementation UIView (EGOHelper) 40 | 41 | - (UIView*)superviewWithClass:(Class)svClass { 42 | return [self superviewWithClass:svClass strict:NO]; 43 | } 44 | 45 | - (UIView*)superviewWithClass:(Class)svClass strict:(BOOL)strict { 46 | UIView* view = self.superview; 47 | 48 | while(view) { 49 | if(strict && [view isMemberOfClass:svClass]) { 50 | break; 51 | } else if(!strict && [view isKindOfClass:svClass]) { 52 | break; 53 | } else { 54 | view = view.superview; 55 | } 56 | } 57 | 58 | return view; 59 | } 60 | 61 | - (void)setDebug:(BOOL)val{ 62 | 63 | self.layer.borderColor = [UIColor colorWithRed:arc4random()%1.0f green:arc4random()%1.0f blue:arc4random()%1.0f alpha:1.0f]; 64 | self.layer.borderWidth = 1.0f; 65 | 66 | } 67 | 68 | @end 69 | 70 | #endif -------------------------------------------------------------------------------- /UIViewController/UIViewControllerHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewControllerHelper.h 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 3/18/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import 29 | 30 | @protocol PopUpViewControllerDelegate 31 | @required 32 | @property(nonatomic,retain) UIViewController* poppedUpFromViewController; 33 | @end 34 | 35 | 36 | @interface UIViewController (Helper) 37 | 38 | // A "Pop Up" is intended to only take up a portion of the screen, similar to a UIAlertView 39 | 40 | // Adds a "Pop Up" view to the current view controller 41 | - (void)presentPopUpViewController:(UIViewController*)viewController; 42 | 43 | // Dismisses the "Pop Up" view 44 | - (void)dismissPopUpViewController; // Calls the method below on poppedUpFromViewController 45 | - (void)dismissPopUpViewController:(UIViewController*)viewController; 46 | 47 | @end 48 | #endif -------------------------------------------------------------------------------- /UIViewController/UIViewControllerHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewControllerHelper.m 3 | // Enormego Cocoa Helpers 4 | // 5 | // Created by Shaun Harrison on 3/18/09. 6 | // Copyright (c) 2009 enormego 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in 16 | // all copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | // THE SOFTWARE. 25 | // 26 | 27 | #if TARGET_OS_IPHONE 28 | #import "UIViewControllerHelper.h" 29 | 30 | @implementation UIViewController (Helper) 31 | 32 | - (void)presentPopUpViewController:(UIViewController*)viewController { 33 | viewController.poppedUpFromViewController = self; 34 | BOOL isLandscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]); 35 | 36 | if((self.view.bounds.size.height == [UIScreen mainScreen].bounds.size.height && !isLandscape) || (self.view.bounds.size.height == [UIScreen mainScreen].bounds.size.width && isLandscape)) { 37 | if(![viewController wantsFullScreenLayout]) { 38 | 39 | CGRect frame = self.view.bounds; 40 | if(isLandscape) { 41 | frame.origin.y = [UIApplication sharedApplication].statusBarFrame.size.width; 42 | } else { 43 | frame.origin.y = [UIApplication sharedApplication].statusBarFrame.size.height; 44 | } 45 | 46 | frame.size.height -= frame.origin.y; 47 | viewController.view.frame = frame; 48 | } else { 49 | viewController.view.frame = self.view.bounds; 50 | } 51 | } else { 52 | viewController.view.frame = self.view.bounds; 53 | } 54 | 55 | viewController.view.alpha = 0.0f; 56 | 57 | [self.view addSubview:viewController.view]; 58 | 59 | [viewController viewWillAppear:YES]; 60 | 61 | [UIView beginAnimations:@"presentPopUpViewController" context:viewController]; 62 | [UIView setAnimationDelegate:self]; 63 | [UIView setAnimationDidStopSelector:@selector(presentedPopUpViewController:finished:viewController:)]; 64 | viewController.view.alpha = 1.0f; 65 | [UIView commitAnimations]; 66 | 67 | [viewController retain]; 68 | } 69 | 70 | - (void)presentedPopUpViewController:(id)name finished:(id)finished viewController:(UIViewController*)viewController { 71 | [viewController viewDidAppear:YES]; 72 | } 73 | 74 | - (void)dismissPopUpViewController { 75 | [((UIViewController*)self).poppedUpFromViewController dismissPopUpViewController:self]; 76 | } 77 | 78 | - (void)dismissPopUpViewController:(UIViewController*)viewController { 79 | [viewController viewWillDisappear:YES]; 80 | 81 | [UIView beginAnimations:@"dismissPopUpViewController" context:viewController]; 82 | [UIView setAnimationDelegate:self]; 83 | [UIView setAnimationDidStopSelector:@selector(dismissedPopUpViewController:finished:viewController:)]; 84 | viewController.view.alpha = 0.0f; 85 | [UIView commitAnimations]; 86 | 87 | if([self isKindOfClass:[UINavigationController class]]) { 88 | if([((UINavigationController*)self).topViewController.view isKindOfClass:[UIScrollView class]]) { 89 | ((UIScrollView*)((UINavigationController*)self).topViewController.view).scrollsToTop = YES; 90 | } else if([((UINavigationController*)self).topViewController respondsToSelector:@selector(tableView)]) { 91 | ((UITableViewController*)((UINavigationController*)self).topViewController).tableView.scrollsToTop = YES; 92 | } 93 | 94 | } 95 | } 96 | 97 | - (void)dismissedPopUpViewController:(id)name finished:(id)finished viewController:(UIViewController*)viewController { 98 | [viewController.view removeFromSuperview]; 99 | [viewController viewDidDisappear:YES]; 100 | [viewController release]; 101 | } 102 | 103 | @end 104 | #endif --------------------------------------------------------------------------------