├── LICENSE
├── README.md
├── RHAdditions.podspec
├── RHAdditions.xcodeproj
├── project.pbxproj
└── project.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcshareddata
│ └── RHAdditions.xccheckout
└── RHAdditions
├── NSAlert+RHBlockAdditions.h
├── NSAlert+RHBlockAdditions.m
├── NSArray+RHFirstObjectAdditions.h
├── NSArray+RHFirstObjectAdditions.m
├── NSArray+RHNumberAdditions.h
├── NSArray+RHNumberAdditions.m
├── NSArray+RHObjectAdditions.h
├── NSArray+RHObjectAdditions.m
├── NSBundle+RHLaunchAtLoginAdditions.h
├── NSBundle+RHLaunchAtLoginAdditions.m
├── NSDate+RHCalendarAdditions.h
├── NSDate+RHCalendarAdditions.m
├── NSDictionary+RHNumberAdditions.h
├── NSDictionary+RHNumberAdditions.m
├── NSImage+RHImageRepresentationAdditions.h
├── NSImage+RHImageRepresentationAdditions.m
├── NSImage+RHResizableImageAdditions.h
├── NSImage+RHResizableImageAdditions.m
├── NSImageView+RHImageLoadingAdditions.h
├── NSImageView+RHImageLoadingAdditions.m
├── NSImageView+RHImageRectAdditions.h
├── NSImageView+RHImageRectAdditions.m
├── NSJSONSerialization+RHTypeAdditions.h
├── NSJSONSerialization+RHTypeAdditions.m
├── NSObject+RHClassInfoAdditions.h
├── NSObject+RHClassInfoAdditions.m
├── NSString+RHCaseAdditions.h
├── NSString+RHCaseAdditions.m
├── NSString+RHNumberAdditions.h
├── NSString+RHNumberAdditions.m
├── NSString+RHRot13Additions.h
├── NSString+RHRot13Additions.m
├── NSString+RHURLEncodingAdditions.h
├── NSString+RHURLEncodingAdditions.m
├── NSTextField+RHLabelAdditions.h
├── NSTextField+RHLabelAdditions.m
├── NSTextField+RHSizeAdditions.h
├── NSTextField+RHSizeAdditions.m
├── NSThread+RHBlockAdditions.h
├── NSThread+RHBlockAdditions.m
├── NSUserDefaults+RHColorAdditions.h
├── NSUserDefaults+RHColorAdditions.m
├── NSView+RHSnapshotAdditions.h
├── NSView+RHSnapshotAdditions.m
├── NSWindow+RHPreventCaptureAdditions.h
├── NSWindow+RHPreventCaptureAdditions.m
├── NSWindow+RHResizeAdditions.h
├── NSWindow+RHResizeAdditions.m
├── RHARCSupport.h
├── RHAdditions.h
├── RHDraggableImageView.h
├── RHDraggableImageView.m
├── RHGetBSDProcessList.h
├── RHGetBSDProcessList.m
├── RHGoogleURLShortener.h
├── RHGoogleURLShortener.m
├── RHLoggingSupport.h
├── RHLoggingSupport.m
├── RHMushParser.h
├── RHMushParser.m
├── RHProgressiveURLConnection.h
├── RHProgressiveURLConnection.m
├── RHWeakSelectorForwarder.h
├── RHWeakSelectorForwarder.m
├── RHWeakValue.h
├── RHWeakValue.m
├── UIApplication+RHStatusBarBoundsAdditions.h
├── UIApplication+RHStatusBarBoundsAdditions.m
├── UIColor+RHInterpolationAdditions.h
├── UIColor+RHInterpolationAdditions.m
├── UIDevice+RHDeviceIdentifierAdditions.h
├── UIDevice+RHDeviceIdentifierAdditions.m
├── UIImage+RHComparingAdditions.h
├── UIImage+RHComparingAdditions.m
├── UIImage+RHPixelAdditions.h
├── UIImage+RHPixelAdditions.m
├── UIImage+RHResizingAdditions.h
├── UIImage+RHResizingAdditions.m
├── UILabel+RHSizeAdditions.h
├── UILabel+RHSizeAdditions.m
├── UIView+RHCompletedActionBadgeAdditions.h
├── UIView+RHCompletedActionBadgeAdditions.m
├── UIView+RHSnapshotAdditions.h
└── UIView+RHSnapshotAdditions.m
/LICENSE:
--------------------------------------------------------------------------------
1 | RHAdditions
2 |
3 | Copyright (c) 2011-2013 Richard Heard. All rights reserved.
4 |
5 | Redistribution and use in source and binary forms, with or without
6 | modification, are permitted provided that the following conditions
7 | are met:
8 | 1. Redistributions of source code must retain the above copyright
9 | notice, this list of conditions and the following disclaimer.
10 | 2. Redistributions in binary form must reproduce the above copyright
11 | notice, this list of conditions and the following disclaimer in the
12 | documentation and/or other materials provided with the distribution.
13 | 3. The name of the author may not be used to endorse or promote products
14 | derived from this software without specific prior written permission.
15 |
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## RHAdditions
2 |
3 | Various Objective-C categories and additions that have served me well over the years.
4 | Hopefully then can serve you too!
5 |
6 |
7 | ## Licence
8 |
9 | Released under the Modified BSD License. (Attribution Required)
10 |
11 | RHAdditions
12 |
13 | Copyright (c) 2011-2013 Richard Heard. All rights reserved.
14 |
15 | Redistribution and use in source and binary forms, with or without
16 | modification, are permitted provided that the following conditions
17 | are met:
18 | 1. Redistributions of source code must retain the above copyright
19 | notice, this list of conditions and the following disclaimer.
20 | 2. Redistributions in binary form must reproduce the above copyright
21 | notice, this list of conditions and the following disclaimer in the
22 | documentation and/or other materials provided with the distribution.
23 | 3. The name of the author may not be used to endorse or promote products
24 | derived from this software without specific prior written permission.
25 |
26 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 |
37 |
38 |
39 | ##Content Overview
40 |
41 | ###RHARCSupport
42 | Supporting macros to allow for building both with and without ARC enabled.
43 |
44 | ###RHLoggingSupport
45 | Provides debug and error logging macros.
46 |
47 | ###NSArray+RHFirstObjectAdditions
48 | Adds a `firstObject` method for balance.
49 |
50 | ###NSBundle+RHLaunchAtLoginAdditions
51 | Adds support for Launch At Login via `SMLoginItemSetEnabled()`.
52 |
53 | ###NSImage+RHImageRepresentationAdditions
54 | Adds various (PNG / JPEG / GIF) representations.
55 |
56 | ###NSObject+RHClassInfoAdditions
57 | Adds a `logClassInfo` method to dump a classes ivars, properties and methods.
58 |
59 | ###NSString+RHNumberAdditions
60 | Adds a bunch of number methods to better reflect those available on NSNumber.
61 |
62 | ###NSString+RHRot13Additions
63 | Adds a rot13 method for various "non security related" purposes.
64 |
65 | ###NSString+RHURLEncodingAdditions
66 | Adds URL encoding methods.
67 |
68 | ###NSThread+RHBlockAdditions
69 | Adds methods for running blocks on specific threads. Useful when specific tasks need to be performed on a specific thread etc.
70 |
71 | ###NSView+RHSnapshotAdditions
72 | Provides NSView snapshotting, useful for animations and cover transitions.
73 |
74 | ###UIView+RHSnapshotAdditions
75 | Provides UIView snapshotting, useful for animations and cover transitions.
76 |
77 | ###NSWindow+RHResizeAdditions
78 | Adds a method to allow for resizing of a windows contentSize over a specific duration.
79 |
80 | ###UIApplication+RHStatusBarBoundsAdditions
81 | Adds statusBar width, height and bounds methods.
82 |
83 | ###UIColor+RHInterpolationAdditions
84 | Provides interpolation between 2 UIColors, useful for frame animations etc.
85 |
86 | ###UIDevice+RHDeviceIdentifierAdditions
87 | Provides a mac address / sha1 based set of device identifiers.
88 |
89 | ###UIImage+RHComparingAdditions
90 | Adds pixel level image comparison, supports various thresholds for slight variations in images (eg from colour correction etc.)
91 | Also supports resizing one of the 2 images being compared if they are not the same size, so that a px by px comparison is possible.
92 |
93 | ###UIImage+RHPixelAdditions
94 | Adds support for accessing the underlying raw pixel data of a UIImage, both in bulk and on a point by point basis.
95 | Returned format is RGBA pre-multiplied as is supported by CoreGraphics.
96 |
97 | ###UIImage+RHResizingAdditions
98 | Adds support for resizing of UIImages that are backed by a CGImage, preserves rotation metadata and scale.
99 |
100 | ###UILabel+RHSizeAdditions
101 | Adds some simple `widthForHeight:` etc. methods.
102 |
103 | ###UIView+RHCompletedActionBadgeAdditions
104 | Adds support for showing a completed action badge from a given view. eg: Upon tapping a submit button and getting a conformation from the server you could show a green checkmark over the submit buttons view.
105 |
--------------------------------------------------------------------------------
/RHAdditions.podspec:
--------------------------------------------------------------------------------
1 | Pod::Spec.new do |s|
2 | s.name = "RHAdditions"
3 | s.version = "1.4.0"
4 | s.homepage = "https://github.com/heardrwt/RHAdditions"
5 | s.summary = "Various Objective-C categories and additions that have served me well over the years. Hopefully then can serve you too!"
6 | s.author = 'Richard Heard'
7 | s.source = { :git => 'https://github.com/heardrwt/RHAdditions.git', :tag => s.version.to_s}
8 | s.requires_arc = true
9 | s.ios.source_files = 'RHAdditions/{RHAdditions,RHARCSupport,RHLoggingSupport,NSArray,NSDate,NSDictionary,NSJSONSerialization,NSObject,NSString,NSThread,NSUserDefaults,RHGoogleURLShortener,RHProgressiveURLConnection,RHWeakSelectorForwarder,RHWeakValue,RHMushParser,UI}*.{h,m}'
10 | s.ios.frameworks = 'UIKit'
11 | s.ios.deployment_target = '5.0'
12 | s.osx.source_files = 'RHAdditions/{RHAdditions,RHARCSupport,RHLoggingSupport,NSArray,NSDate,NSDictionary,NSJSONSerialization,NSObject,NSString,NSThread,NSUserDefaults,RHGoogleURLShortener,RHProgressiveURLConnection,RHWeakSelectorForwarder,RHWeakValue,NSBundle,NSImage,NSImageView,NSTextField,NSView,NSWindow,RHGetBSDProcessList,RHDraggableImageView,RHMushParser}*.{h,m}'
13 | s.osx.frameworks = 'Foundation', 'Cocoa', 'Carbon', 'ServiceManagement'
14 | s.osx.deployment_target = '10.7'
15 | s.license = {
16 | :type => 'Modified BSD',
17 | :text => <<-LICENSE
18 | Copyright (c) 2011-2013 Richard Heard. All rights reserved.
19 |
20 | Redistribution and use in source and binary forms, with or without
21 | modification, are permitted provided that the following conditions
22 | are met:
23 | 1. Redistributions of source code must retain the above copyright
24 | notice, this list of conditions and the following disclaimer.
25 | 2. Redistributions in binary form must reproduce the above copyright
26 | notice, this list of conditions and the following disclaimer in the
27 | documentation and/or other materials provided with the distribution.
28 | 3. The name of the author may not be used to endorse or promote products
29 | derived from this software without specific prior written permission.
30 |
31 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 | LICENSE
42 | }
43 |
44 | end
45 |
--------------------------------------------------------------------------------
/RHAdditions.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 46;
7 | objects = {
8 |
9 | /* Begin PBXFileReference section */
10 | 1360CAD616E495F100F54D48 /* RHAdditions */ = {isa = PBXFileReference; lastKnownFileType = folder; path = RHAdditions; sourceTree = ""; };
11 | /* End PBXFileReference section */
12 |
13 | /* Begin PBXGroup section */
14 | 13ADCE7916E18649001F4190 = {
15 | isa = PBXGroup;
16 | children = (
17 | 1360CAD616E495F100F54D48 /* RHAdditions */,
18 | );
19 | sourceTree = "";
20 | };
21 | /* End PBXGroup section */
22 |
23 | /* Begin PBXProject section */
24 | 13ADCE7A16E18649001F4190 /* Project object */ = {
25 | isa = PBXProject;
26 | attributes = {
27 | LastUpgradeCheck = 0460;
28 | };
29 | buildConfigurationList = 13ADCE7D16E18649001F4190 /* Build configuration list for PBXProject "RHAdditions" */;
30 | compatibilityVersion = "Xcode 3.2";
31 | developmentRegion = English;
32 | hasScannedForEncodings = 0;
33 | knownRegions = (
34 | en,
35 | );
36 | mainGroup = 13ADCE7916E18649001F4190;
37 | projectDirPath = "";
38 | projectRoot = "";
39 | targets = (
40 | );
41 | };
42 | /* End PBXProject section */
43 |
44 | /* Begin XCBuildConfiguration section */
45 | 13ADCE7E16E18649001F4190 /* Debug */ = {
46 | isa = XCBuildConfiguration;
47 | buildSettings = {
48 | };
49 | name = Debug;
50 | };
51 | 13ADCE7F16E18649001F4190 /* Release */ = {
52 | isa = XCBuildConfiguration;
53 | buildSettings = {
54 | };
55 | name = Release;
56 | };
57 | /* End XCBuildConfiguration section */
58 |
59 | /* Begin XCConfigurationList section */
60 | 13ADCE7D16E18649001F4190 /* Build configuration list for PBXProject "RHAdditions" */ = {
61 | isa = XCConfigurationList;
62 | buildConfigurations = (
63 | 13ADCE7E16E18649001F4190 /* Debug */,
64 | 13ADCE7F16E18649001F4190 /* Release */,
65 | );
66 | defaultConfigurationIsVisible = 0;
67 | defaultConfigurationName = Release;
68 | };
69 | /* End XCConfigurationList section */
70 | };
71 | rootObject = 13ADCE7A16E18649001F4190 /* Project object */;
72 | }
73 |
--------------------------------------------------------------------------------
/RHAdditions.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/RHAdditions.xcodeproj/project.xcworkspace/xcshareddata/RHAdditions.xccheckout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDESourceControlProjectIdentifier
6 | 21C788D4-444A-4E0E-8FB5-37BE3BE31C34
7 | IDESourceControlProjectName
8 | RHAdditions
9 | IDESourceControlProjectOriginsDictionary
10 |
11 | 993E00E8-926F-4241-A9A7-DBA6298A1612
12 | https://github.com/heardrwt/RHAdditions.git
13 |
14 | IDESourceControlProjectPath
15 | RHAdditions.xcodeproj/project.xcworkspace
16 | IDESourceControlProjectRelativeInstallPathDictionary
17 |
18 | 993E00E8-926F-4241-A9A7-DBA6298A1612
19 | ../..
20 |
21 | IDESourceControlProjectURL
22 | https://github.com/heardrwt/RHAdditions.git
23 | IDESourceControlProjectVersion
24 | 110
25 | IDESourceControlProjectWCCIdentifier
26 | 993E00E8-926F-4241-A9A7-DBA6298A1612
27 | IDESourceControlProjectWCConfigurations
28 |
29 |
30 | IDESourceControlRepositoryExtensionIdentifierKey
31 | public.vcs.git
32 | IDESourceControlWCCIdentifierKey
33 | 993E00E8-926F-4241-A9A7-DBA6298A1612
34 | IDESourceControlWCCName
35 | RHAdditions
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/RHAdditions/NSAlert+RHBlockAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSAlert+RHBlockAdditions.h
3 | //
4 | // Created by Richard Heard on 18/10/2013.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | typedef NSInteger RHModalResponse;
33 |
34 | @interface NSAlert (RHBlockAdditions)
35 |
36 | -(void)rh_beginSheetModalForWindow:(NSWindow *)sheetWindow completionHandler:(void (^)(RHModalResponse returnCode))handler;
37 |
38 | +(instancetype)rh_defaultAlertWithTitle:(NSString*)title; //NSAlertFirstButtonReturn == OK NSAlertSecondButtonReturn == Cancel
39 |
40 | @end
41 |
--------------------------------------------------------------------------------
/RHAdditions/NSAlert+RHBlockAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSAlert+RHBlockAdditions.m
3 | //
4 | // Created by Richard Heard on 18/10/2013.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSAlert+RHBlockAdditions.h"
31 | #import "RHARCSupport.h"
32 |
33 | @implementation NSAlert (RHBlockAdditions)
34 |
35 | -(void)rh_beginSheetModalForWindow:(NSWindow *)sheetWindow completionHandler:(void (^)(RHModalResponse returnCode))handler{
36 | if (handler) handler = [handler copy];
37 |
38 | //use the provided block method if available
39 | #if defined(MAC_OS_X_VERSION_10_9) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9)
40 | if ([self respondsToSelector:@selector(beginSheetModalForWindow:completionHandler:)]){
41 | [self beginSheetModalForWindow:sheetWindow completionHandler:handler];
42 | return;
43 | }
44 | #endif //end 10.9+
45 |
46 | #pragma clang diagnostic push
47 | #pragma clang diagnostic ignored "-Wdeprecated-declarations"
48 | [self beginSheetModalForWindow:sheetWindow modalDelegate:self didEndSelector:@selector(rh_alertDidEnd:returnCode:contextInfo:) contextInfo:(__bridge_retained void *)handler];
49 | #pragma clang diagnostic pop
50 |
51 | }
52 |
53 | -(void)rh_alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo{
54 | if (contextInfo){
55 | void (^completionBlock)(RHModalResponse returnCode) = (__bridge_transfer void (^)(RHModalResponse))(contextInfo);
56 | completionBlock(returnCode);
57 | arc_release(completionBlock);
58 | }
59 | }
60 |
61 | +(instancetype)rh_defaultAlertWithTitle:(NSString*)title{
62 | NSAlert *alert = [[NSAlert alloc] init];
63 | [alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
64 | [alert addButtonWithTitle:NSLocalizedString(@"Cancel", nil)];
65 | [alert setMessageText:title];
66 | return arc_autorelease(alert);
67 | }
68 |
69 | @end
70 |
71 |
72 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
73 | @interface RHFixCategoryBugClassNSARHBA : NSObject @end @implementation RHFixCategoryBugClassNSARHBA @end
74 |
75 |
--------------------------------------------------------------------------------
/RHAdditions/NSArray+RHFirstObjectAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSArray+RHFirstObjectAdditions.h
3 | //
4 | // Created by Richard Heard on 15/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface NSArray (RHFirstObjectAdditions)
33 |
34 | -(id)firstObject; // returns nil instead of throwing an exception if empty array
35 |
36 | @end
37 |
38 | @interface NSMutableArray (RHFirstObjectAdditions)
39 |
40 | -(void)removeFirstObject;
41 |
42 | @end
43 |
44 |
--------------------------------------------------------------------------------
/RHAdditions/NSArray+RHFirstObjectAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSArray+RHFirstObjectAdditions.m
3 | //
4 | // Created by Richard Heard on 15/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSArray+RHFirstObjectAdditions.h"
31 |
32 | @implementation NSArray (RHFirstObjectAdditions)
33 |
34 | -(id)firstObject{
35 | return (self.count > 0) ? [self objectAtIndex:0] : nil;
36 | }
37 |
38 | @end
39 |
40 | @implementation NSMutableArray (RHFirstObjectAdditions)
41 |
42 | -(void)removeFirstObject{
43 | if (self.count > 0) [self removeObjectAtIndex:0];
44 | }
45 |
46 | @end
47 |
48 |
49 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
50 | @interface RHFixCategoryBugClassNSARHFOA : NSObject @end @implementation RHFixCategoryBugClassNSARHFOA @end
51 |
52 |
--------------------------------------------------------------------------------
/RHAdditions/NSArray+RHNumberAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSArray+RHNumberAdditions.h
3 | //
4 | // Created by Richard Heard on 15/07/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface NSArray (RHNumberAdditions)
33 |
34 | -(NSNumber *)numberAtIndex:(NSUInteger)index; // NSNumber or nil
35 |
36 | //primitive getters
37 | -(BOOL)boolAtIndex:(NSUInteger)index;
38 | -(int)intAtIndex:(NSUInteger)index;
39 | -(long)longAtIndex:(NSUInteger)index;
40 | -(unsigned long)unsignedLongAtIndex:(NSUInteger)index;
41 | -(long long)longLongAtIndex:(NSUInteger)index;
42 | -(unsigned long long)unsignedLongLongAtIndex:(NSUInteger)index;
43 | -(double)doubleAtIndex:(NSUInteger)index;
44 | -(float)floatAtIndex:(NSUInteger)index;
45 | -(NSInteger)integerAtIndex:(NSUInteger)index;
46 | -(NSUInteger)usignedIntegerAtIndex:(NSUInteger)index;
47 |
48 | @end
49 |
50 | @interface NSMutableArray (RHNumberAdditions)
51 |
52 | //primitive additions
53 | -(void)addBool:(BOOL)value;
54 | -(void)addInt:(int)value;
55 | -(void)addLong:(long)value;
56 | -(void)addUnsignedLong:(unsigned long)value;
57 | -(void)addLongLong:(long long)value;
58 | -(void)addUnsignedLongLong:(unsigned long long)value;
59 | -(void)addDouble:(double)value;
60 | -(void)addFloat:(float)value;
61 | -(void)addInteger:(NSInteger)value;
62 | -(void)addUnsignedInteger:(NSUInteger)value;
63 |
64 | //primitive insertions
65 | -(void)insertBool:(BOOL)value atIndex:(NSUInteger)index;
66 | -(void)insertInt:(int)value atIndex:(NSUInteger)index;
67 | -(void)insertLong:(long)value atIndex:(NSUInteger)index;
68 | -(void)insertLongLong:(long long)value atIndex:(NSUInteger)index;
69 | -(void)insertUnsignedLongLong:(unsigned long long)value atIndex:(NSUInteger)index;
70 | -(void)insertDouble:(double)value atIndex:(NSUInteger)index;
71 | -(void)insertFloat:(float)value atIndex:(NSUInteger)index;
72 | -(void)insertInteger:(NSInteger)value atIndex:(NSUInteger)index;
73 | -(void)insertUnsignedInteger:(NSUInteger)value atIndex:(NSUInteger)index;
74 |
75 | @end
76 |
77 |
78 |
--------------------------------------------------------------------------------
/RHAdditions/NSArray+RHObjectAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSArray+RHObjectAdditions.h
3 | //
4 | // Created by Richard Heard on 26/11/2013.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface NSArray (RHObjectAdditions)
33 |
34 | -(NSArray*)arrayByRemovingObject:(id)anObject;
35 | -(NSArray*)arrayByRemovingObjectsFromArray:(NSArray*)otherArray;
36 |
37 | -(NSArray*)objectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate;
38 |
39 | @end
40 |
41 |
--------------------------------------------------------------------------------
/RHAdditions/NSArray+RHObjectAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSArray+RHObjectAdditions.m
3 | //
4 | // Created by Richard Heard on 26/11/2013.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSArray+RHObjectAdditions.h"
31 |
32 | @implementation NSArray (RHObjectAdditions)
33 |
34 | -(NSArray*)arrayByRemovingObject:(id)anObject{
35 | return [self arrayByRemovingObjectsFromArray:[NSArray arrayWithObject:anObject]];
36 | }
37 |
38 | -(NSArray*)arrayByRemovingObjectsFromArray:(NSArray*)otherArray{
39 | return [self objectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
40 | return ![otherArray containsObject:obj];
41 | }];
42 | }
43 |
44 | -(NSArray*)objectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate{
45 | NSIndexSet *indexes = [self indexesOfObjectsPassingTest:predicate];
46 | return [self objectsAtIndexes:indexes];
47 | }
48 |
49 | @end
50 |
51 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
52 | @interface RHFixCategoryBugClassNSARHOA : NSObject @end @implementation RHFixCategoryBugClassNSARHOA @end
53 |
54 |
--------------------------------------------------------------------------------
/RHAdditions/NSBundle+RHLaunchAtLoginAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSBundle+RHLaunchAtLoginAdditions.h
3 | //
4 | // Created by Richard Heard on 4/07/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 | #import
32 |
33 | @interface NSBundle (RHLaunchAtLoginAdditions)
34 |
35 | -(BOOL)launchAtLogin;
36 | -(BOOL)setLaunchAtLogin:(BOOL)enabled;
37 |
38 | @end
39 |
40 | extern BOOL RHLaunchAtLoginEnabledForBundleIdentifier(NSString *bundleIdentifier);
41 | extern BOOL RHLaunchAtLoginSetEnabledForBundleIdentifier(NSString *bundleIdentifier, BOOL enabled);
42 |
43 |
--------------------------------------------------------------------------------
/RHAdditions/NSBundle+RHLaunchAtLoginAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSBundle+RHLaunchAtLoginAdditions.m
3 | //
4 | // Created by Richard Heard on 4/07/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSBundle+RHLaunchAtLoginAdditions.h"
31 |
32 | @implementation NSBundle (RHLaunchAtLoginAdditions)
33 |
34 | -(BOOL)launchAtLogin{
35 | return RHLaunchAtLoginEnabledForBundleIdentifier(self.bundleIdentifier);
36 | }
37 |
38 | -(BOOL)setLaunchAtLogin:(BOOL)enabled{
39 | return RHLaunchAtLoginSetEnabledForBundleIdentifier(self.bundleIdentifier, enabled);
40 | }
41 |
42 | @end
43 |
44 | extern BOOL RHLaunchAtLoginEnabledForBundleIdentifier(NSString *bundleIdentifier){
45 | if (!bundleIdentifier) return NO;
46 |
47 | BOOL enabled = NO;
48 |
49 | #pragma clang diagnostic push
50 | #pragma clang diagnostic ignored "-Wdeprecated-declarations"
51 | //See: rdar://17951732 ER: Need a way to query a given a given domains SMLoginItemSetEnabled() state given that SMCopyAllJobDictionaries is deprecated in 10.10.
52 | CFArrayRef jobs = SMCopyAllJobDictionaries(kSMDomainUserLaunchd);
53 | #pragma clang diagnostic pop
54 |
55 | if (!jobs) return NO;
56 |
57 | for (CFIndex i = 0; i < CFArrayGetCount(jobs); i++) {
58 | NSDictionary *job = CFArrayGetValueAtIndex(jobs, i);
59 | if ([bundleIdentifier isEqualToString:[job objectForKey:@"Label"]]){
60 | enabled = [[job objectForKey:@"OnDemand"] boolValue];
61 | break;
62 | }
63 | }
64 |
65 | CFRelease(jobs);
66 |
67 | return enabled;
68 | }
69 |
70 | extern BOOL RHLaunchAtLoginSetEnabledForBundleIdentifier(NSString *bundleIdentifier, BOOL enabled){
71 | if (!bundleIdentifier) return NO;
72 | return SMLoginItemSetEnabled((__bridge CFStringRef)bundleIdentifier, enabled);
73 | }
74 |
75 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
76 | @interface RHFixCategoryBugClassNSBRHLALA : NSObject @end @implementation RHFixCategoryBugClassNSBRHLALA @end
77 |
78 |
--------------------------------------------------------------------------------
/RHAdditions/NSDate+RHCalendarAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSDate+RHCalendarAdditions.h
3 | //
4 | // Created by Richard Heard on 4/07/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface NSCalendar (RHCalendarAdditions)
33 |
34 | +(NSCalendar*)gregorianCalendar;
35 |
36 | @end
37 |
38 |
39 | @interface NSDate (RHCalendarAdditions)
40 |
41 | //components
42 | -(NSDateComponents*)componentsForGregorianCalendar;
43 |
44 | //seconds
45 | -(NSDate*)dateByAddingSeconds:(NSInteger)seconds;
46 | -(NSInteger)secondsBetweenDates:(NSDate*)otherDate;
47 |
48 | //days
49 | -(NSDate*)dateByAddingDays:(NSInteger)days;
50 | -(NSInteger)daysBetweenDates:(NSDate*)otherDate;
51 |
52 | -(NSDate*)previousDay;
53 | -(NSDate*)nextDay;
54 |
55 | //months
56 | -(NSDate*)dateByAddingMonths:(NSInteger)months;
57 |
58 | -(NSDate*)previousMonth;
59 | -(NSDate*)nextMonth;
60 |
61 |
62 | //normalization
63 | -(NSDate*)normalizedDate; //strips all time info.
64 |
65 |
66 | @end
67 |
--------------------------------------------------------------------------------
/RHAdditions/NSDate+RHCalendarAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSDate+RHCalendarAdditions.m
3 | //
4 | // Created by Richard Heard on 4/07/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSDate+RHCalendarAdditions.h"
31 | #import "RHARCSupport.h"
32 |
33 | @implementation NSCalendar (RHCalendarAdditions)
34 |
35 | +(NSCalendar*)gregorianCalendar{
36 | static NSCalendar *_gregorianCalendar = nil;
37 | static dispatch_once_t onceToken;
38 | dispatch_once(&onceToken, ^{
39 | _gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
40 | });
41 |
42 | return _gregorianCalendar;
43 | }
44 |
45 | @end
46 |
47 |
48 | @implementation NSDate (RHCalendarAdditions)
49 |
50 | //components
51 | -(NSDateComponents*)componentsForGregorianCalendar{
52 |
53 | NSUInteger components = NSCalendarUnitEra
54 | | NSCalendarUnitYear
55 | | NSCalendarUnitMonth
56 | | NSCalendarUnitDay
57 | | NSCalendarUnitHour
58 | | NSCalendarUnitMinute
59 | | NSCalendarUnitSecond
60 | | NSCalendarUnitWeekday
61 | | NSCalendarUnitWeekdayOrdinal
62 | | NSCalendarUnitQuarter
63 | | NSCalendarUnitWeekOfMonth
64 | | NSCalendarUnitWeekOfYear
65 | | NSCalendarUnitYearForWeekOfYear
66 | | NSCalendarUnitCalendar
67 | | NSCalendarUnitTimeZone;
68 |
69 | return [[NSCalendar gregorianCalendar] components:components fromDate:self];
70 | }
71 |
72 | //seconds
73 | -(NSDate*)dateByAddingSeconds:(NSInteger)seconds{
74 | NSDateComponents *components = arc_autorelease([[NSDateComponents alloc] init]);
75 | [components setSecond:seconds];
76 |
77 | return [[NSCalendar gregorianCalendar] dateByAddingComponents:components toDate:self options:0];
78 | }
79 |
80 | -(NSInteger)secondsBetweenDates:(NSDate*)otherDate{
81 | NSDateComponents *components = [[NSCalendar gregorianCalendar] components:NSCalendarUnitSecond fromDate:self toDate:otherDate options:0];
82 | return ABS(components.second);
83 | }
84 |
85 | //days
86 | -(NSDate*)dateByAddingDays:(NSInteger)days{
87 | NSDateComponents *components = arc_autorelease([[NSDateComponents alloc] init]);
88 | [components setDay:days];
89 |
90 | return [[NSCalendar gregorianCalendar] dateByAddingComponents:components toDate:self options:0];
91 | }
92 |
93 | -(NSInteger)daysBetweenDates:(NSDate*)otherDate{
94 | NSDateComponents *components = [[NSCalendar gregorianCalendar] components:NSCalendarUnitDay fromDate:self toDate:otherDate options:0];
95 | return ABS(components.day);
96 | }
97 |
98 | -(NSDate*)previousDay{
99 | return [self dateByAddingDays:-1];
100 | }
101 | -(NSDate*)nextDay{
102 | return [self dateByAddingDays:1];
103 | }
104 |
105 | //months
106 | -(NSDate*)dateByAddingMonths:(NSInteger)months{
107 | NSDateComponents *components = arc_autorelease([[NSDateComponents alloc] init]);
108 | [components setMonth:months];
109 |
110 | return [[NSCalendar gregorianCalendar] dateByAddingComponents:components toDate:self options:0];
111 | }
112 |
113 | -(NSDate*)previousMonth{
114 | return [self dateByAddingMonths:-1];
115 | }
116 |
117 | -(NSDate*)nextMonth{
118 | return [self dateByAddingMonths:1];
119 | }
120 |
121 |
122 | //normalization
123 | -(NSDate*)normalizedDate{
124 | NSDateComponents* components = [[NSCalendar gregorianCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self];
125 | return [[NSCalendar gregorianCalendar] dateFromComponents:components];
126 | }
127 |
128 | @end
129 |
130 |
131 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
132 | @interface RHFixCategoryBugClassNSDRHCA : NSObject @end @implementation RHFixCategoryBugClassNSDRHCA @end
133 |
134 |
--------------------------------------------------------------------------------
/RHAdditions/NSDictionary+RHNumberAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSDictionary+RHNumberAdditions.h
3 | //
4 | // Created by Richard Heard on 15/07/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface NSDictionary (RHNumberAdditions)
33 |
34 | -(NSNumber *)numberForKey:(id)aKey;
35 |
36 | //primitive getters
37 | -(BOOL)boolForKey:(id)aKey;
38 | -(int)intForKey:(id)aKey;
39 | -(long)longForKey:(id)aKey;
40 | -(unsigned long)unsignedLongForKey:(id)aKey;
41 | -(long long)longLongForKey:(id)aKey;
42 | -(unsigned long long)unsignedLongLongForKey:(id)aKey;
43 | -(double)doubleForKey:(id)aKey;
44 | -(float)floatForKey:(id)aKey;
45 | -(NSInteger)integerForKey:(id)aKey;
46 | -(NSUInteger)usignedIntegerForKey:(id)aKey;
47 |
48 | @end
49 |
50 | @interface NSMutableDictionary (RHNumberAdditions)
51 |
52 | //primitive insertions
53 | -(void)setBool:(BOOL)value forKey:(id )aKey;
54 | -(void)setInt:(int)value forKey:(id )aKey;
55 | -(void)setLong:(long)value forKey:(id )aKey;
56 | -(void)setLongLong:(long long)value forKey:(id )aKey;
57 | -(void)setUnsignedLongLong:(unsigned long long)value forKey:(id )aKey;
58 | -(void)setDouble:(double)value forKey:(id )aKey;
59 | -(void)setFloat:(float)value forKey:(id )aKey;
60 | -(void)setInteger:(NSInteger)value forKey:(id )aKey;
61 | -(void)setUnsignedInteger:(NSUInteger)value forKey:(id )aKey;
62 |
63 | @end
64 |
65 |
66 | @interface NSUserDefaults (RHNumberAdditions)
67 |
68 | -(NSNumber *)numberForKey:(NSString *)aKey;
69 |
70 | -(int)intForKey:(NSString *)aKey;
71 | -(long)longForKey:(NSString *)aKey;
72 | -(unsigned long)unsignedLongForKey:(NSString *)aKey;
73 | -(long long)longLongForKey:(NSString *)aKey;
74 | -(unsigned long long)unsignedLongLongForKey:(NSString *)aKey;
75 | -(NSUInteger)usignedIntegerForKey:(NSString *)aKey;
76 |
77 | -(void)setInt:(int)value forKey:(NSString *)aKey;
78 | -(void)setLong:(long)value forKey:(NSString *)aKey;
79 | -(void)setLongLong:(long long)value forKey:(NSString *)aKey;
80 | -(void)setUnsignedLongLong:(unsigned long long)value forKey:(NSString *)aKey;
81 | -(void)setUnsignedInteger:(NSUInteger)value forKey:(NSString *)aKey;
82 |
83 | -(void)incrementKey:(NSString *)aKey;
84 | -(void)decrementKey:(NSString *)aKey;
85 | -(void)incrementKey:(NSString *)aKey byAmount:(NSInteger)amount;
86 |
87 | @end
88 |
--------------------------------------------------------------------------------
/RHAdditions/NSImage+RHImageRepresentationAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSImage+RHImageRepresentationAdditions.h
3 | //
4 | // Created by Richard Heard on 2/07/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface NSImage (RHImageRepresentationAdditions)
33 |
34 | -(NSData*)PNGRepresentation;
35 | -(NSData*)JPEGRepresentationWithCompressionFactor:(float)compressionFactor;
36 | -(NSData*)GIFRepresentation;
37 |
38 | -(NSData*)bestRepresentation; //returns the most suitable rep for the given NSImage. (gif or png)
39 |
40 | @end
41 |
42 | extern NSData* RHImagePNGRepresentationForImage(NSImage *image);
43 | extern NSData* RHImageJPEGRepresentationForImage(NSImage *image, float compressionFactor);
44 | extern NSData* RHImageGIFRepresentationForImage(NSImage *image);
45 |
46 | extern NSData* RHImageBestRepresentationForImage(NSImage *image);
47 |
48 |
--------------------------------------------------------------------------------
/RHAdditions/NSImageView+RHImageLoadingAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSImageView+RHImageLoadingAdditions.h
3 | //
4 | // Created by Richard Heard on 2/05/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // Adds async image loading support to NSImageView
30 |
31 | #import
32 |
33 | @interface NSImageView (RHImageLoadingAdditions)
34 |
35 | @property (nonatomic, retain) NSImage *placeholderImage;
36 | @property (nonatomic, retain) NSImage *errorImage;
37 | @property (nonatomic, assign) BOOL showsLoadingSpinner;
38 |
39 | -(void)setImageWithContentsOfURL:(NSURL*)url;
40 | -(void)setImageWithContentsOfURL:(NSURL*)url placeholderImage:(NSImage*)placeholderImage;
41 | -(void)setImageWithContentsOfURL:(NSURL*)url placeholderImage:(NSImage*)placeholderImage errorImage:(NSImage*)errorImage;
42 |
43 | -(void)stopFetchingImage;
44 |
45 | @end
46 |
--------------------------------------------------------------------------------
/RHAdditions/NSImageView+RHImageRectAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSImageView+RHImageRectAdditions.h
3 | //
4 | // Created by Richard Heard on 6/10/2013.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface NSImageView (RHImageRectAdditions)
33 |
34 | -(NSSize)imageScale; //takes into account NSImageScale
35 | -(NSSize)scaledImageSize; //takes into account NSImageScale
36 | -(NSRect)imageRect; //takes into account NSImageAlignment & NSImageScale
37 |
38 | @end
39 |
40 |
--------------------------------------------------------------------------------
/RHAdditions/NSImageView+RHImageRectAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSImageView+RHImageRectAdditions.m
3 | //
4 | // Created by Richard Heard on 6/10/2013.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSImageView+RHImageRectAdditions.h"
31 |
32 | @implementation NSImageView (RHImageRectAdditions)
33 |
34 | -(NSSize)imageScale{
35 | CGFloat imageW = self.image.size.width;
36 | CGFloat imageH = self.image.size.height;
37 |
38 | if (imageH < 1.0 || imageW < 1.0) return NSZeroSize;
39 |
40 | NSRect drawingBounds = [self.cell drawingRectForBounds:self.bounds];
41 | CGFloat width = drawingBounds.size.width;
42 | CGFloat height = drawingBounds.size.height;
43 |
44 | CGFloat scaleX = width / imageW;
45 | CGFloat scaleY = height / imageH;
46 | CGFloat scale = 1.0;
47 |
48 | switch (self.imageScaling) {
49 | case NSImageScaleProportionallyDown:
50 | scale = fminf(scaleX, scaleY);
51 | scale = MIN(scale, (CGFloat) 1.0); //max scale at 1.0 (ie scale down only)
52 | return CGSizeMake(scale, scale);
53 |
54 | case NSImageScaleProportionallyUpOrDown:
55 | scale = fminf(scaleX, scaleY);
56 | return CGSizeMake(scale, scale);
57 |
58 | case NSImageScaleAxesIndependently:
59 | return CGSizeMake(scaleX, scaleY);
60 |
61 | case NSImageScaleNone:
62 | default: return CGSizeMake(scale, scale);
63 | }
64 | }
65 |
66 |
67 | -(NSSize)scaledImageSize{
68 | NSSize scale = [self imageScale];
69 |
70 | CGFloat scaledW = self.image.size.width * scale.width;
71 | CGFloat scaledH = self.image.size.height * scale.height;
72 |
73 | return NSMakeSize(scaledW, scaledH);
74 | }
75 |
76 | -(NSRect)imageRect{
77 |
78 | NSRect drawingBounds = [self.cell drawingRectForBounds:self.bounds];
79 |
80 | CGFloat width = drawingBounds.size.width;
81 | CGFloat height = drawingBounds.size.height;
82 |
83 | NSSize scaled = [self scaledImageSize];
84 | CGFloat scaledW = scaled.width;
85 | CGFloat scaledH = scaled.height;
86 |
87 | CGFloat minX = (CGFloat) 0.0;
88 | CGFloat minY = (CGFloat) 0.0;
89 | CGFloat maxX = (CGFloat) width - scaledW;
90 | CGFloat maxY = (CGFloat) height - scaledH;
91 | CGFloat centeredX = (CGFloat) ceil((width - scaledW) * (CGFloat)0.5);
92 | CGFloat centeredY = (CGFloat) ceil((height - scaledH) * (CGFloat)0.5);
93 |
94 | NSRect result = NSZeroRect;
95 |
96 | switch (self.imageAlignment) {
97 | case NSImageAlignCenter: result = NSMakeRect(centeredX, centeredY, scaledW, scaledH); break;
98 | case NSImageAlignTop: result = NSMakeRect(centeredX, maxY, scaledW, scaledH); break;
99 | case NSImageAlignTopLeft: result = NSMakeRect(minX, maxY, scaledW, scaledH); break;
100 | case NSImageAlignTopRight: result = NSMakeRect(maxX, maxY, scaledW, scaledH); break;
101 | case NSImageAlignLeft: result = NSMakeRect(minX, centeredY, scaledW, scaledH); break;
102 | case NSImageAlignBottom: result = NSMakeRect(centeredX, minY, scaledW, scaledH); break;
103 | case NSImageAlignBottomLeft: result = NSMakeRect(minX, minY, scaledW, scaledH); break;
104 | case NSImageAlignBottomRight: result = NSMakeRect(maxX, minY, scaledW, scaledH); break;
105 | case NSImageAlignRight: result = NSMakeRect(maxX, centeredY, scaledW, scaledH); break;
106 | }
107 |
108 | //offset by the origin of the cells drawingRect (we are working in imageview bounds atm)
109 | result = NSOffsetRect(result, drawingBounds.origin.x, drawingBounds.origin.y);
110 |
111 | return result;
112 |
113 | }
114 |
115 | @end
116 |
117 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
118 | @interface RHFixCategoryBugClassNSIVRHIRA : NSObject @end @implementation RHFixCategoryBugClassNSIVRHIRA @end
119 |
120 |
--------------------------------------------------------------------------------
/RHAdditions/NSJSONSerialization+RHTypeAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSJSONSerialization+RHTypeAdditions.h
3 | //
4 | // Created by Richard Heard on 25/05/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // Add some type safety to NSJSONSerialization return types
30 |
31 | #import
32 |
33 | @interface NSJSONSerialization (RHTypeAdditions)
34 |
35 | +(NSArray*)arrayWithData:(NSData *)data;
36 | +(NSArray*)arrayWithData:(NSData *)data error:(NSError **)error;
37 | +(NSMutableArray*)mutableArrayWithData:(NSData *)data error:(NSError **)error;
38 |
39 | +(NSDictionary*)dictionaryWithData:(NSData *)data;
40 | +(NSDictionary*)dictionaryWithData:(NSData *)data error:(NSError **)error;
41 | +(NSMutableDictionary*)mutableDictionaryWithData:(NSData *)data error:(NSError **)error;
42 |
43 | @end
44 |
45 | @interface NSDictionary (RHJSONDataAdditions)
46 |
47 | -(NSData*)JSONData;
48 | -(NSData*)JSONDataWithError:(NSError **)error;
49 |
50 | +(id)dictionaryWithJSONData:(NSData*)data;
51 | -(id)initWithJSONData:(NSData*)data error:(NSError **)error;
52 |
53 | @end
54 |
55 | @interface NSArray (RHJSONDataAdditions)
56 |
57 | -(NSData*)JSONData;
58 | -(NSData*)JSONDataWithError:(NSError **)error;
59 |
60 | +(id)arrayWithJSONData:(NSData*)data;
61 | -(id)initWithJSONData:(NSData*)data error:(NSError **)error;
62 |
63 | @end
64 |
65 |
--------------------------------------------------------------------------------
/RHAdditions/NSJSONSerialization+RHTypeAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSJSONSerialization+RHTypeAdditions.m
3 | //
4 | // Created by Richard Heard on 25/05/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSJSONSerialization+RHTypeAdditions.h"
31 | #import "RHARCSupport.h"
32 |
33 | @implementation NSJSONSerialization (RHTypeAdditions)
34 |
35 | +(NSArray*)arrayWithData:(NSData *)data{
36 | return [self arrayWithData:data error:nil];
37 | }
38 |
39 | +(NSArray*)arrayWithData:(NSData *)data error:(NSError **)error{
40 | id result = [self JSONObjectWithData:data options:0 error:error];
41 | if (![result isKindOfClass:[NSArray class]]) return nil;
42 | return result;
43 | }
44 |
45 | +(NSMutableArray*)mutableArrayWithData:(NSData *)data error:(NSError **)error{
46 | id result = [self JSONObjectWithData:data options:NSJSONReadingMutableContainers error:error];
47 | if (![result isKindOfClass:[NSMutableArray class]]) return nil;
48 | return result;
49 | }
50 |
51 | +(NSDictionary*)dictionaryWithData:(NSData *)data{
52 | return [self dictionaryWithData:data error:nil];
53 | }
54 | +(NSDictionary*)dictionaryWithData:(NSData *)data error:(NSError **)error{
55 | id result = [self JSONObjectWithData:data options:0 error:error];
56 | if (![result isKindOfClass:[NSDictionary class]]) return nil;
57 | return result;
58 | }
59 |
60 | +(NSMutableDictionary*)mutableDictionaryWithData:(NSData *)data error:(NSError **)error{
61 | id result = [self JSONObjectWithData:data options:NSJSONReadingMutableContainers error:error];
62 | if (![result isKindOfClass:[NSMutableDictionary class]]) return nil;
63 | return result;
64 | }
65 |
66 | @end
67 |
68 | @implementation NSDictionary (RHJSONDataAdditions)
69 |
70 | -(NSData*)JSONData{
71 | return [self JSONDataWithError:nil];
72 | }
73 |
74 | -(NSData*)JSONDataWithError:(NSError **)error{
75 | return [NSJSONSerialization dataWithJSONObject:self options:0 error:error];
76 | }
77 |
78 | +(id)dictionaryWithJSONData:(NSData*)data{
79 | return arc_autorelease([[self alloc] initWithJSONData:data error:nil]);
80 | }
81 |
82 | -(id)initWithJSONData:(NSData*)data error:(NSError **)error{
83 | NSDictionary *dictionary = [NSJSONSerialization dictionaryWithData:data error:error];
84 | if (!dictionary) return nil;
85 | return [self initWithDictionary:dictionary];
86 | }
87 |
88 | @end
89 |
90 |
91 | @implementation NSArray (RHJSONDataAdditions)
92 |
93 | -(NSData*)JSONData{
94 | return [self JSONDataWithError:nil];
95 | }
96 |
97 | -(NSData*)JSONDataWithError:(NSError **)error{
98 | return [NSJSONSerialization dataWithJSONObject:self options:0 error:error];
99 | }
100 |
101 | +(id)arrayWithJSONData:(NSData*)data{
102 | return arc_autorelease([[self alloc] initWithJSONData:data error:nil]);
103 | }
104 |
105 | -(id)initWithJSONData:(NSData*)data error:(NSError **)error{
106 | NSArray *array = [NSJSONSerialization arrayWithData:data error:error];
107 | if (!array) return nil;
108 | return [self initWithArray:array];
109 | }
110 |
111 | @end
112 |
113 |
114 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
115 | @interface RHFixCategoryBugClassNSJSONSRHTA : NSObject @end @implementation RHFixCategoryBugClassNSJSONSRHTA @end
116 |
117 |
--------------------------------------------------------------------------------
/RHAdditions/NSObject+RHClassInfoAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSObject+RHClassInfoAdditions.h
3 | //
4 | // Created by Richard Heard on 25/02/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // see http://stackoverflow.com/questions/2299841/objective-c-introspection-reflection
30 |
31 | #import
32 | #import
33 |
34 | @interface NSObject (RHClassInfoAdditions)
35 |
36 | -(void)logClassInfo;
37 |
38 | @end
39 |
40 | extern NSDictionary* RHInfoForClass(Class debugClass);
41 |
42 |
--------------------------------------------------------------------------------
/RHAdditions/NSObject+RHClassInfoAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSObject+RHClassInfoAdditions.h
3 | //
4 | // Created by Richard Heard on 25/02/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSObject+RHClassInfoAdditions.h"
31 |
32 | @implementation NSObject (RHClassInfoAdditions)
33 |
34 | -(void)logClassInfo{
35 | NSLog(@"%@", RHInfoForClass([self class]));
36 | }
37 |
38 | @end
39 |
40 | NSDictionary* RHInfoForClass(Class debugClass){
41 | u_int count;
42 |
43 | Ivar* ivars = class_copyIvarList(debugClass, &count);
44 | NSMutableArray* ivarArray = [NSMutableArray arrayWithCapacity:count];
45 | for (u_int i = 0; i < count ; i++){
46 | const char* ivarName = ivar_getName(ivars[i]);
47 | [ivarArray addObject:[NSString stringWithCString:ivarName encoding:NSUTF8StringEncoding]];
48 | }
49 | free(ivars);
50 |
51 | objc_property_t* properties = class_copyPropertyList(debugClass, &count);
52 | NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];
53 | for (u_int i = 0; i < count ; i++){
54 | const char* propertyName = property_getName(properties[i]);
55 | [propertyArray addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
56 | }
57 | free(properties);
58 |
59 | Method* methods = class_copyMethodList(debugClass, &count);
60 | NSMutableArray* methodArray = [NSMutableArray arrayWithCapacity:count];
61 | for (u_int i = 0; i < count ; i++){
62 | SEL selector = method_getName(methods[i]);
63 | const char* methodName = sel_getName(selector);
64 | [methodArray addObject:[NSString stringWithCString:methodName encoding:NSUTF8StringEncoding]];
65 | }
66 | free(methods);
67 |
68 | return [NSDictionary dictionaryWithObjectsAndKeys:
69 | ivarArray, @"ivars",
70 | propertyArray, @"properties",
71 | methodArray, @"methods",
72 | nil];
73 | }
74 |
75 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
76 | @interface RHFixCategoryBugClassNSORHCIA : NSObject @end @implementation RHFixCategoryBugClassNSORHCIA @end
77 |
78 |
--------------------------------------------------------------------------------
/RHAdditions/NSString+RHCaseAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSString+RHCaseAdditions.h
3 | //
4 | // Created by Richard Heard on 16/07/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface NSString (RHCaseAdditions)
33 |
34 | -(NSString*)uppercaseFirstString;
35 | -(NSString*)lowercaseFirstString;
36 | -(NSString*)camelcaseString;
37 | -(NSString*)underscoreString;
38 |
39 | -(NSString*)stringByDeletingComments;
40 | -(NSString*)stringByTrimmingWhitespaceAndNewlineCharacters;
41 |
42 | @end
43 |
44 |
--------------------------------------------------------------------------------
/RHAdditions/NSString+RHCaseAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSString+RHCaseAdditions.m
3 | //
4 | // Created by Richard Heard on 13/07/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSString+RHCaseAdditions.h"
31 |
32 | @implementation NSString (RHCaseAdditions)
33 |
34 | -(NSString*)uppercaseFirstString{
35 | if (self.length < 1) return self;
36 | return [[[self substringToIndex:1] uppercaseString] stringByAppendingString:[self substringFromIndex:1]];
37 | }
38 |
39 | -(NSString*)lowercaseFirstString{
40 | if (self.length < 1) return self;
41 | return [[[self substringToIndex:1] lowercaseString] stringByAppendingString:[self substringFromIndex:1]];
42 | }
43 |
44 | -(NSString*)camelcaseString{
45 | NSArray *components = [self componentsSeparatedByString:@"_"];
46 | NSMutableString *result = [NSMutableString string];
47 |
48 | for (NSString *component in components) {
49 | if (result.length == 0){
50 | [result appendString:[component lowercaseFirstString]];
51 | } else {
52 | [result appendString:[component uppercaseFirstString]];
53 | }
54 | }
55 |
56 | return [NSString stringWithString:result];
57 | }
58 |
59 | -(NSString*)underscoreString{
60 | NSMutableString *result = [NSMutableString stringWithString:self];
61 | NSRange range = [result rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]];
62 |
63 | while (range.location != NSNotFound) {
64 | [result replaceCharactersInRange:range withString:[[result substringWithRange:range] lowercaseString]];
65 | [result insertString:@"_" atIndex:range.location];
66 | range = [result rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]];
67 | }
68 |
69 | return [NSString stringWithString:result];
70 | }
71 |
72 | -(NSString*)stringByDeletingComments{
73 | NSRange range;
74 | NSMutableString *result = [NSMutableString stringWithString:self];
75 |
76 | //remove: /* comment */
77 | while ((range = [result rangeOfString:@"/\\*.*?\\*/" options:NSRegularExpressionSearch]).location != NSNotFound){
78 | [result replaceCharactersInRange:range withString:@""];
79 | }
80 |
81 | //remove: //comment
82 | while ((range = [result rangeOfString:@"//.*?\\n" options:NSRegularExpressionSearch]).location != NSNotFound){
83 | [result replaceCharactersInRange:range withString:@""];
84 | }
85 |
86 | return [NSString stringWithString:result];
87 | }
88 |
89 | -(NSString*)stringByTrimmingWhitespaceAndNewlineCharacters{
90 | return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
91 | }
92 |
93 | @end
94 |
95 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
96 | @interface RHFixCategoryBugClassNSSRHCA : NSObject @end @implementation RHFixCategoryBugClassNSSRHCA @end
97 |
98 |
--------------------------------------------------------------------------------
/RHAdditions/NSString+RHNumberAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSString+RHNumberAdditions.h
3 | //
4 | // Created by Richard Heard on 13/11/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface NSString (RHNumberAdditions)
33 |
34 | -(char)charValue;
35 | -(unsigned char)unsignedCharValue;
36 | -(short)shortValue;
37 | -(unsigned short)unsignedShortValue;
38 | -(unsigned int)unsignedIntValue;
39 | -(long)longValue;
40 | -(unsigned long)unsignedLongValue;
41 | -(unsigned long long)unsignedLongLongValue;
42 | -(NSUInteger)unsignedIntegerValue;
43 |
44 | @end
45 |
46 |
--------------------------------------------------------------------------------
/RHAdditions/NSString+RHNumberAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSString+RHNumberAdditions.m
3 | //
4 | // Created by Richard Heard on 13/11/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSString+RHNumberAdditions.h"
31 |
32 | @implementation NSString (RHNumberAdditions)
33 |
34 | -(char)charValue{
35 | return (char)[self characterAtIndex:0];
36 | }
37 |
38 | -(unsigned char)unsignedCharValue{
39 | return (unsigned char)[self characterAtIndex:0];
40 | }
41 |
42 | -(short)shortValue{
43 | return (short)[self longValue];
44 | }
45 |
46 | -(unsigned short)unsignedShortValue{
47 | return (unsigned short)[self unsignedLongValue];
48 | }
49 |
50 | -(unsigned int)unsignedIntValue{
51 | return (unsigned int)[self unsignedLongValue];
52 | }
53 |
54 | -(long)longValue{
55 | return (long)strtol([self cStringUsingEncoding:NSASCIIStringEncoding], NULL, 10);
56 | }
57 |
58 | -(unsigned long)unsignedLongValue{
59 | return (unsigned long)strtoul([self cStringUsingEncoding:NSASCIIStringEncoding], NULL, 10);
60 | }
61 |
62 | -(unsigned long long)unsignedLongLongValue{
63 | return (unsigned long long)strtoull([self cStringUsingEncoding:NSASCIIStringEncoding], NULL, 10);
64 | }
65 |
66 | -(NSUInteger)unsignedIntegerValue{
67 | #if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
68 | typedef unsigned long NSUInteger;
69 | return [self unsignedLongValue];
70 | #else
71 | return [self unsignedIntValue];
72 | #endif
73 | }
74 |
75 | @end
76 |
77 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
78 | @interface RHFixCategoryBugClassNSSRHNA : NSObject @end @implementation RHFixCategoryBugClassNSSRHNA @end
79 |
80 |
--------------------------------------------------------------------------------
/RHAdditions/NSString+RHRot13Additions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSString+RHRot13Additions.h
3 | //
4 | // Created by Richard Heard on 15/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // Provides a basic ROT13 implementation for ascii strings.
30 |
31 | #import
32 |
33 | @interface NSString (RHRot13Additions)
34 |
35 | -(NSString*)rh_rot13;
36 |
37 | @end
38 |
39 |
--------------------------------------------------------------------------------
/RHAdditions/NSString+RHRot13Additions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSString+RHRot13Additions.m
3 | //
4 | // Created by Richard Heard on 15/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSString+RHRot13Additions.h"
31 |
32 | @implementation NSString (RHRot13Additions)
33 |
34 | -(NSString*)rh_rot13{
35 | char out[[self length] + 1];
36 | strcpy(out, [self cStringUsingEncoding:NSASCIIStringEncoding]);
37 |
38 | for( char *ch=out; *ch != '\0'; ch++){
39 | if( 0x40 < *ch && *ch < 0x5B ) {*ch = (((*ch - 0x41) + 0x0D) % 0x1A) + 0x41; }//A-Z
40 | if( 0x60 < *ch && *ch < 0x7B ) {*ch = (((*ch - 0x61) + 0x0D) % 0x1A) + 0x61;} //a-z
41 | }
42 |
43 | return [NSString stringWithCString:out encoding:NSASCIIStringEncoding];
44 |
45 | }
46 |
47 | @end
48 |
49 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
50 | @interface RHFixCategoryBugClassNSSRHR13A : NSObject @end @implementation RHFixCategoryBugClassNSSRHR13A @end
51 |
52 |
--------------------------------------------------------------------------------
/RHAdditions/NSString+RHURLEncodingAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSString+RHURLEncodingAdditions.h
3 | //
4 | // Created by Richard Heard on 30/09/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface NSString (RHURLEncodingAdditions)
33 |
34 | -(NSString*)stringByURLEncodingStringWithEncoding:(NSStringEncoding)encoding;
35 | -(NSString*)stringByFormURLEncodingStringWithEncoding:(NSStringEncoding)encoding; //x-www-form-urlencoded. (+ instead of %20)
36 |
37 | @end
38 |
39 | extern CFStringRef RHCreateStringByURLEncodingStringWithEncoding(CFStringRef string, CFStringEncoding encoding);
40 |
41 |
--------------------------------------------------------------------------------
/RHAdditions/NSString+RHURLEncodingAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSString+RHURLEncodingAdditions.m
3 | //
4 | // Created by Richard Heard on 30/09/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSString+RHURLEncodingAdditions.h"
31 | #import "RHARCSupport.h"
32 |
33 | @implementation NSString (RHURLEncodingAdditions)
34 |
35 | -(NSString*)stringByURLEncodingStringWithEncoding:(NSStringEncoding)encoding{
36 | NSString *string = arc_autorelease((__bridge_transfer NSString*)RHCreateStringByURLEncodingStringWithEncoding((__bridge CFStringRef)self, CFStringConvertNSStringEncodingToEncoding(encoding)));
37 | return string;
38 | }
39 |
40 | -(NSString*)stringByFormURLEncodingStringWithEncoding:(NSStringEncoding)encoding{
41 | NSString *string = arc_autorelease((__bridge_transfer NSString*)RHCreateStringByURLEncodingStringWithEncoding((__bridge CFStringRef)self, CFStringConvertNSStringEncodingToEncoding(encoding)));
42 | return [string stringByReplacingOccurrencesOfString:@"%20" withString:@"+"];
43 | }
44 |
45 |
46 | @end
47 |
48 | extern CFStringRef RHCreateStringByURLEncodingStringWithEncoding(CFStringRef string, CFStringEncoding encoding){
49 | if (!string) return NULL;
50 | CFStringRef escapeChars = CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`");
51 | CFStringRef result = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, string, NULL, escapeChars, encoding);
52 | return result;
53 | }
54 |
55 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
56 | @interface RHFixCategoryBugClassNSSRHUEA : NSObject @end @implementation RHFixCategoryBugClassNSSRHUEA @end
57 |
58 |
--------------------------------------------------------------------------------
/RHAdditions/NSTextField+RHLabelAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSTextField+RHLabelAdditions.h
3 | //
4 | // Created by Richard Heard on 6/05/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface NSTextField (RHLabelAdditions)
33 |
34 | //a pre-configured NSTextField instance as per Label in IB.
35 | +(id)newLabelWithFrame:(NSRect)frame;
36 | +(id)newWrappingLabelWithFrame:(NSRect)frame;
37 |
38 | @end
39 |
--------------------------------------------------------------------------------
/RHAdditions/NSTextField+RHLabelAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSTextField+RHLabelAdditions.m
3 | //
4 | // Created by Richard Heard on 6/05/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSTextField+RHLabelAdditions.h"
31 |
32 | @implementation NSTextField (RHLabelAdditions)
33 |
34 | +(id)newLabelWithFrame:(NSRect)frame{
35 | NSTextField *new = [[self alloc] initWithFrame:frame];
36 |
37 | [new setStringValue:@"New Label"];
38 | [new setBezeled:NO];
39 | [new setDrawsBackground:NO];
40 | [new setEditable:NO];
41 | [new setSelectable:NO];
42 | [new setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:[[new cell] controlSize]]]];
43 | [new.cell setUsesSingleLineMode:YES];
44 | [new.cell setTruncatesLastVisibleLine:YES];
45 |
46 | return new;
47 | }
48 |
49 | +(id)newWrappingLabelWithFrame:(NSRect)frame{
50 | NSTextField *new = [self newLabelWithFrame:frame];
51 |
52 | [new.cell setWraps:YES];
53 | [new.cell setUsesSingleLineMode:NO];
54 | [new.cell setTruncatesLastVisibleLine:NO];
55 |
56 | return new;
57 | }
58 | @end
59 |
60 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
61 | @interface RHFixCategoryBugClassNSTFRHLA : NSObject @end @implementation RHFixCategoryBugClassNSTFRHLA @end
62 |
63 |
--------------------------------------------------------------------------------
/RHAdditions/NSTextField+RHSizeAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSTextField+RHSizeAdditions.h
3 | //
4 | // Created by Richard Heard on 14/06/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface NSTextField (RHSizeAdditions)
33 |
34 | -(NSSize)sizeWithMaxWidth:(CGFloat)maxWidth;
35 | -(NSSize)sizeWithMaxHeight:(CGFloat)maxHeight;
36 |
37 | -(CGFloat)heightForWidth:(CGFloat)width;
38 | -(CGFloat)widthForHeight:(CGFloat)height;
39 |
40 | @end
41 |
42 | extern NSSize RHAttributedStringRequiredSizeWithMaxSize(NSAttributedString* attributedString, NSSize maxSize);
43 |
44 | //other
45 | extern NSAttributedString* RHAttributedStringWithAlignment(NSAttributedString *string, NSTextAlignment alignment);
46 |
47 |
--------------------------------------------------------------------------------
/RHAdditions/NSTextField+RHSizeAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSTextField+RHSizeAdditions.m
3 | //
4 | // Created by Richard Heard on 14/06/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSTextField+RHSizeAdditions.h"
31 | #import "RHARCSupport.h"
32 |
33 | @implementation NSTextField (RHSizeAdditions)
34 |
35 | -(NSSize)sizeWithMaxWidth:(CGFloat)maxWidth{
36 | NSSize maxSize = NSMakeSize(maxWidth, CGFLOAT_MAX);
37 | return RHAttributedStringRequiredSizeWithMaxSize(self.attributedStringValue, maxSize);
38 | }
39 |
40 | -(NSSize)sizeWithMaxHeight:(CGFloat)maxHeight{
41 | NSSize maxSize = NSMakeSize(CGFLOAT_MAX, maxHeight);
42 | return RHAttributedStringRequiredSizeWithMaxSize(self.attributedStringValue, maxSize);
43 | }
44 |
45 | -(CGFloat)heightForWidth:(CGFloat)width{
46 | return [self sizeWithMaxWidth:width].height;
47 | }
48 | -(CGFloat)widthForHeight:(CGFloat)height{
49 | return [self sizeWithMaxHeight:height].width;
50 | }
51 |
52 | @end
53 |
54 | NSSize RHAttributedStringRequiredSizeWithMaxSize(NSAttributedString* attributedString, NSSize maxSize){
55 | NSSize result = NSZeroSize;
56 |
57 | NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:maxSize];
58 | NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
59 | NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
60 | [layoutManager addTextContainer:textContainer];
61 | [textStorage addLayoutManager:layoutManager];
62 | [layoutManager setHyphenationFactor:0.0];
63 |
64 | //force layout by querying glyphs for our text container
65 | [layoutManager glyphRangeForTextContainer:textContainer];
66 |
67 | //grab our result
68 | result = [layoutManager usedRectForTextContainer:textContainer].size;
69 |
70 | //take into account any extra height added for an empty line with cursor
71 | CGFloat extraLineHeight = [layoutManager extraLineFragmentRect].size.height;
72 | result.height -= extraLineHeight;
73 |
74 | //cleanup
75 | arc_release(layoutManager);
76 | arc_release(textStorage);
77 | arc_release(textContainer);
78 |
79 | //return
80 | return result;
81 | }
82 |
83 | //other
84 | NSAttributedString* RHAttributedStringWithAlignment(NSAttributedString *string, NSTextAlignment alignment) {
85 | NSMutableParagraphStyle *paragraphStyle = arc_autorelease([[NSMutableParagraphStyle alloc] init]);
86 | [paragraphStyle setAlignment:alignment];
87 |
88 | NSMutableAttributedString *mutableString = arc_autorelease([[NSMutableAttributedString alloc] initWithAttributedString:string]);
89 | [mutableString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];
90 |
91 | return arc_autorelease([[NSAttributedString alloc] initWithAttributedString:mutableString]);
92 | }
93 |
94 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
95 | @interface RHFixCategoryBugClassNSTFRHSA : NSObject @end @implementation RHFixCategoryBugClassNSTFRHSA @end
96 |
97 |
--------------------------------------------------------------------------------
/RHAdditions/NSThread+RHBlockAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSThread+RHBlockAdditions.h
3 | //
4 | // Created by Richard Heard on 22/8/11.
5 | // Copyright (c) 2011 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // Provides methods for running blocks on specific threads, useful when specific
30 | // tasks need to be performed on a single thread for thread safety etc.
31 |
32 | #import
33 |
34 | typedef void (^VoidBlock)(void);
35 |
36 | @interface NSThread (RHBlockAdditions)
37 |
38 | -(void)rh_performBlock:(VoidBlock)block;
39 | -(void)rh_performBlock:(VoidBlock)block waitUntilDone:(BOOL)wait;
40 | -(void)rh_performBlock:(VoidBlock)block afterDelay:(NSTimeInterval)delay;
41 |
42 | +(void)rh_performBlockOnMainThread:(VoidBlock)block;
43 | +(void)rh_performBlockOnMainThread:(VoidBlock)block waitUntilDone:(BOOL)wait;
44 | +(void)rh_performBlockInBackground:(VoidBlock)block;
45 |
46 | //private
47 | -(void)_rh_runBlock:(void (^)())block;
48 |
49 | @end
50 |
51 |
--------------------------------------------------------------------------------
/RHAdditions/NSThread+RHBlockAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSThread+RHBlockAdditions.m
3 | //
4 | // Created by Richard Heard on 22/8/11.
5 | // Copyright (c) 2011 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSThread+RHBlockAdditions.h"
31 | #import "RHARCSupport.h"
32 |
33 | @implementation NSThread (RHBlockAdditions)
34 |
35 | #pragma mark - public
36 | -(void)rh_performBlock:(VoidBlock)block{
37 | [self rh_performBlock:block waitUntilDone:YES];
38 | }
39 |
40 | -(void)rh_performBlock:(VoidBlock)block waitUntilDone:(BOOL)wait{
41 | //if current thread and wait (run directly)
42 | if ([[NSThread currentThread] isEqual:self] && wait){
43 | block(); return;
44 | }
45 | [self performSelector:@selector(_rh_runBlock:) onThread:self withObject:arc_autorelease([block copy]) waitUntilDone:wait];
46 | }
47 |
48 | -(void)rh_performBlock:(VoidBlock)block afterDelay:(NSTimeInterval)delay{
49 | [self performSelector:@selector(rh_performBlock:) withObject:arc_autorelease([block copy]) afterDelay:delay];
50 | }
51 |
52 |
53 | #pragma mark - helpers
54 | +(void)rh_performBlockOnMainThread:(VoidBlock)block{
55 | [[NSThread mainThread] rh_performBlock:block];
56 | }
57 |
58 | +(void)rh_performBlockOnMainThread:(VoidBlock)block waitUntilDone:(BOOL)wait{
59 | [[NSThread mainThread] rh_performBlock:block waitUntilDone:wait];
60 | }
61 |
62 | +(void)rh_performBlockInBackground:(VoidBlock)block{
63 | [NSThread performSelectorInBackground:@selector(_rh_runBlock:) withObject:arc_autorelease([block copy])];
64 | }
65 |
66 | -(void)_rh_runBlock:(void (^)())block{
67 | if (block) block();
68 | }
69 |
70 | @end
71 |
72 |
73 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
74 | @interface RHFixCategoryBugClassRHBA : NSObject @end @implementation RHFixCategoryBugClassRHBA @end
75 |
76 |
--------------------------------------------------------------------------------
/RHAdditions/NSUserDefaults+RHColorAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSUserDefaults+RHColorAdditions.h
3 | //
4 | // Created by Richard Heard on 8/05/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #ifdef __APPLE__
31 | #import "TargetConditionals.h"
32 | #endif
33 |
34 | #if (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
35 | #import
36 | #else
37 | #import
38 | #endif
39 |
40 |
41 | @interface NSUserDefaults (RHColorAdditions)
42 |
43 | #if (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
44 |
45 | -(void)setColor:(UIColor*)color forKey:(NSString*)key;
46 | -(UIColor*)colorForKey:(NSString*)key;
47 |
48 | #else
49 |
50 | -(void)setColor:(NSColor*)color forKey:(NSString*)key;
51 | -(NSColor*)colorForKey:(NSString*)key;
52 |
53 | #endif
54 |
55 | @end
56 |
--------------------------------------------------------------------------------
/RHAdditions/NSUserDefaults+RHColorAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSUserDefaults+RHColorAdditions.m
3 | //
4 | // Created by Richard Heard on 8/05/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSUserDefaults+RHColorAdditions.h"
31 |
32 | @implementation NSUserDefaults (RHColorAdditions)
33 |
34 | -(void)setColor:(id)color forKey:(NSString*)key{
35 | [self setObject:[NSKeyedArchiver archivedDataWithRootObject:color] forKey:key];
36 | }
37 |
38 | -(id)colorForKey:(NSString*)key{
39 | NSData *data = [self dataForKey:key];
40 | return data ? [NSKeyedUnarchiver unarchiveObjectWithData:data] : nil;
41 | }
42 |
43 | @end
44 |
45 | @interface RHFixCategoryBugClassNSUDRHCA : NSObject @end @implementation RHFixCategoryBugClassNSUDRHCA @end
46 |
47 |
--------------------------------------------------------------------------------
/RHAdditions/NSView+RHSnapshotAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSView+RHSnapshotAdditions.h
3 | //
4 | // Created by Richard Heard on 2/07/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | //Provides NSView snapshotting, useful for animations and cover transitions.
30 |
31 | #import
32 |
33 | @interface NSView (RHSnapshotAdditions)
34 |
35 | -(NSImage *)rh_snapshot;
36 |
37 | @end
38 |
39 |
--------------------------------------------------------------------------------
/RHAdditions/NSView+RHSnapshotAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSView+RHSnapshotAdditions.h
3 | //
4 | // Created by Richard Heard on 2/07/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 | #import "RHARCSupport.h"
32 |
33 | @implementation NSView (RHSnapshotAdditions)
34 |
35 | -(NSImage *)rh_snapshot{
36 |
37 | NSBitmapImageRep *imageRep = [self bitmapImageRepForCachingDisplayInRect:self.bounds];
38 | [imageRep setSize:self.bounds.size];
39 |
40 | [self cacheDisplayInRect:self.bounds toBitmapImageRep:imageRep];
41 |
42 | NSImage* image = [[NSImage alloc] initWithSize:self.bounds.size];
43 | [image addRepresentation:imageRep];
44 |
45 | return arc_autorelease(image);
46 |
47 | }
48 |
49 | @end
50 |
51 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
52 | @interface RHFixCategoryBugClassNSVRHSA : NSObject @end @implementation RHFixCategoryBugClassNSVRHSA @end
53 |
54 |
--------------------------------------------------------------------------------
/RHAdditions/NSWindow+RHPreventCaptureAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSWindow+RHPreventCaptureAdditions.h
3 | //
4 | // Created by Richard Heard on 7/03/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // This addition prevents a specified window from being captured in a screenshot.
30 | // WARNING WARNING WARNING: This uses PRIVATE CGS APIs. Use at your own risk.
31 | // Not recommended to be shipped in production apps.
32 |
33 | #if defined(INCLUDE_PRIVATE_API) && INCLUDE_PRIVATE_API
34 |
35 | #import
36 | #import
37 |
38 | @interface NSWindow (RHPreventCaptureAdditions)
39 | -(BOOL)preventWindowFromBeingCaptured;
40 | @end
41 |
42 | extern BOOL RHPreventWindowCaptureForWindow(NSWindow *window);
43 |
44 |
45 | #endif
46 |
--------------------------------------------------------------------------------
/RHAdditions/NSWindow+RHPreventCaptureAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSWindow+RHPreventCaptureAdditions.m
3 | //
4 | // Created by Richard Heard on 7/03/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #if defined(INCLUDE_PRIVATE_API) && INCLUDE_PRIVATE_API
31 |
32 | #import "NSWindow+RHPreventCaptureAdditions.h"
33 |
34 | @implementation NSWindow (RHPreventCaptureAdditions)
35 | -(BOOL)preventWindowFromBeingCaptured{
36 | return RHPreventWindowCaptureForWindow(self);
37 | }
38 | @end
39 |
40 | //define the private CGS methods we need to exclude a window from being captured.
41 | // ------ PRIVATE API ------
42 | typedef int CGSConnectionID;
43 | typedef int CGSWindowID;
44 | typedef CFTypeRef CGSRegionRef;
45 |
46 | CG_EXTERN CGSConnectionID CGSMainConnectionID(void);
47 |
48 | CG_EXTERN CGError CGSGetWindowBounds(CGSConnectionID cid, CGSWindowID wid, CGRect *rectOut);
49 |
50 | CG_EXTERN CGError CGSNewRegionWithRect(const CGRect *rect, CGSRegionRef *regionOut);
51 | CG_EXTERN bool CGSRegionIsEmpty(CGSRegionRef region);
52 | CG_EXTERN CGError CGSReleaseRegion(CGSRegionRef region);
53 |
54 | CG_EXTERN CGError CGSSetWindowCaptureExcludeShape(CGSConnectionID cid, CGSWindowID wid, CGSRegionRef region);
55 | // ------ END PRIVATE API ------
56 |
57 |
58 | //implementation
59 |
60 | BOOL RHPreventWindowCaptureForWindow(NSWindow *window){
61 | CGSConnectionID cid = CGSMainConnectionID();
62 | CGSWindowID wid = (int)window.windowNumber;
63 |
64 | CGRect windowRectOut;
65 | CGError err = CGSGetWindowBounds(cid, wid, &windowRectOut);
66 | if (err != kCGErrorSuccess){
67 | NSLog(@"Error: failed to get windows bounds with error %i", err);
68 | return NO;
69 | }
70 | windowRectOut.origin = CGPointZero;
71 |
72 | CGSRegionRef regionOut;
73 | err = CGSNewRegionWithRect(&windowRectOut, ®ionOut);
74 | if (err != kCGErrorSuccess){
75 | NSLog(@"Error: failed to get create CGSRegion with error %i", err);
76 | return NO;
77 | }
78 |
79 | //CFShow(regionOut);
80 |
81 | err = CGSSetWindowCaptureExcludeShape(cid, wid, regionOut);
82 | if (err != kCGErrorSuccess){
83 | NSLog(@"Error: failed to set capture exclude shape for window with error %i", err);
84 | CGSReleaseRegion(regionOut);
85 | return NO;
86 | }
87 |
88 | CGSReleaseRegion(regionOut);
89 |
90 | return YES;
91 | }
92 |
93 | #endif
94 |
--------------------------------------------------------------------------------
/RHAdditions/NSWindow+RHResizeAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // NSWindow+RHResizeAdditions.h
3 | //
4 | // Created by Richard Heard on 4/02/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 | #import
32 |
33 | @interface NSWindow (RHResizeAdditions)
34 |
35 | -(void)resizeForContentSize:(NSSize)size duration:(CGFloat)duration;
36 |
37 | @end
38 |
39 |
--------------------------------------------------------------------------------
/RHAdditions/NSWindow+RHResizeAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // NSWindow+RHResizeAdditions.m
3 | //
4 | // Created by Richard Heard on 4/02/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "NSWindow+RHResizeAdditions.h"
31 |
32 | @implementation NSWindow (RHResizeAdditions)
33 |
34 | #pragma mark - View Controller Methods
35 |
36 | -(void)resizeForContentSize:(NSSize)size duration:(CGFloat)duration{
37 | NSWindow *window = self;
38 |
39 | NSRect frame = [window contentRectForFrameRect:[window frame]];
40 |
41 | CGFloat newX = NSMinX(frame) + (0.5* (NSWidth(frame) - size.width));
42 |
43 | NSRect newFrame = [window frameRectForContentRect:NSMakeRect(newX, NSMaxY(frame) - size.height, size.width, size.height)];
44 |
45 | //keep the view fully on screen
46 | NSRect screenFrame = self.screen.frame;
47 | if (newFrame.origin.x < screenFrame.origin.x + 1.0) {
48 | newFrame.origin.x = screenFrame.origin.x + 1.0;
49 | }
50 | if (NSMaxX(newFrame) > NSMaxX(screenFrame) - 1.0) {
51 | newFrame.origin.x = NSMaxX(screenFrame) - NSWidth(newFrame) - 1.0;
52 | }
53 |
54 | //keep it pinned to the edge of the screen, if currently pinned to the edge
55 | if (NSMinX(self.frame) <= NSMinX(screenFrame) + 2.0){
56 | //left edge of the screen
57 | newFrame.origin.x = NSMinX(screenFrame) + 1.0;
58 | }
59 |
60 | if (NSMaxX(self.frame) >= NSMaxX(screenFrame) - 2.0){
61 | //left edge of the screen
62 | newFrame.origin.x = NSMaxX(screenFrame) - NSWidth(newFrame) - 1.0;
63 | }
64 |
65 | //resize
66 | if (duration > 0.0f){
67 | [NSAnimationContext beginGrouping];
68 | [[NSAnimationContext currentContext] setDuration:duration];
69 | [[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
70 |
71 | [[window animator] setFrame:newFrame display:YES];
72 | [NSAnimationContext endGrouping];
73 | } else {
74 | [window setFrame:newFrame display:YES];
75 | }
76 |
77 | }
78 |
79 | @end
80 |
81 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
82 | @interface RHFixCategoryBugClassNSWRHRA : NSObject @end @implementation RHFixCategoryBugClassNSWRHRA @end
83 |
84 |
--------------------------------------------------------------------------------
/RHAdditions/RHARCSupport.h:
--------------------------------------------------------------------------------
1 | //
2 | // RHARCSupport.h
3 | //
4 | // Created by Richard Heard on 3/07/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // supporting macros for code to allow building with and without arc
30 |
31 |
32 | #ifndef __has_feature
33 | // not LLVM Compiler
34 | #define __has_feature(x) 0
35 | #endif
36 |
37 | #ifndef CF_CONSUMED
38 | #if __has_feature(attribute_cf_consumed)
39 | #define CF_CONSUMED __attribute__((cf_consumed))
40 | #else
41 | #define CF_CONSUMED
42 | #endif
43 | #endif
44 |
45 |
46 | // ----- ARC Enabled -----
47 | #if __has_feature(objc_arc) && !defined(ARC_IS_ENABLED)
48 |
49 | #define ARC_IS_ENABLED 1
50 |
51 | //define retain count macro wrappers
52 | #define arc_retain(x) (x)
53 | #define arc_release(x)
54 | #define arc_release_nil(x) (x = nil)
55 | #define arc_autorelease(x) (x)
56 | #define arc_super_dealloc()
57 |
58 | //add CF bridging methods
59 | #define ARCBridgingRetain(x) CFBridgingRetain(x)
60 | #define ARCBridgingRelease(x) CFBridgingRelease(x)
61 |
62 | #endif
63 |
64 |
65 | // ----- ARC Disabled -----
66 | #if !__has_feature(objc_arc) && !defined(ARC_IS_ENABLED)
67 |
68 | #define ARC_IS_ENABLED 0
69 |
70 | //define retain count macro wrappers
71 | #define arc_retain(x) ([x retain])
72 | #define arc_release(x) ([x release])
73 | #define arc_release_nil(x) [x release]; x = nil;
74 | #define arc_autorelease(x) ([x autorelease])
75 | #define arc_super_dealloc() ([super dealloc])
76 |
77 | //add arc keywords if not already defined
78 | #ifndef __bridge
79 | #define __bridge
80 | #endif
81 | #ifndef __bridge_retained
82 | #define __bridge_retained
83 | #endif
84 | #ifndef __bridge_transfer
85 | #define __bridge_transfer
86 | #endif
87 |
88 | #ifndef __autoreleasing
89 | #define __autoreleasing
90 | #endif
91 | #ifndef __strong
92 | #define __strong
93 | #endif
94 | #ifndef __weak
95 | #define __weak
96 | #endif
97 | #ifndef __unsafe_unretained
98 | #define __unsafe_unretained
99 | #endif
100 |
101 | //add CF bridging methods (we inline these ourselves because they are not included in older sdks)
102 | NS_INLINE CF_RETURNS_RETAINED CFTypeRef ARCBridgingRetain(id X) {
103 | return X ? CFRetain((CFTypeRef)X) : NULL;
104 | }
105 |
106 | NS_INLINE id ARCBridgingRelease(CFTypeRef CF_CONSUMED X) {
107 | return [(id)CFMakeCollectable(X) autorelease];
108 | }
109 |
110 | #endif
111 |
112 |
113 | //if clarity helper
114 | #define ARC_IS_NOT_ENABLED (!(ARC_IS_ENABLED))
115 |
116 |
--------------------------------------------------------------------------------
/RHAdditions/RHAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // RHAdditions.h
3 | // RHAdditions
4 | //
5 | // Created by Richard Heard on 7/04/13.
6 | // Copyright (c) 2013 Richard Heard. All rights reserved.
7 | //
8 | // Redistribution and use in source and binary forms, with or without
9 | // modification, are permitted provided that the following conditions
10 | // are met:
11 | // 1. Redistributions of source code must retain the above copyright
12 | // notice, this list of conditions and the following disclaimer.
13 | // 2. Redistributions in binary form must reproduce the above copyright
14 | // notice, this list of conditions and the following disclaimer in the
15 | // documentation and/or other materials provided with the distribution.
16 | // 3. The name of the author may not be used to endorse or promote products
17 | // derived from this software without specific prior written permission.
18 | //
19 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 |
30 |
31 | #ifdef __APPLE__
32 | #import "TargetConditionals.h"
33 | #endif
34 |
35 | //don't include private stuff in App Store builds
36 | #if !defined(INCLUDE_PRIVATE_API)
37 | #define INCLUDE_PRIVATE_API 0
38 | #endif
39 |
40 | //common
41 | #import "RHARCSupport.h"
42 | #import "RHLoggingSupport.h"
43 |
44 | #import "NSArray+RHFirstObjectAdditions.h"
45 | #import "NSArray+RHNumberAdditions.h"
46 | #import "NSArray+RHObjectAdditions.h"
47 | #import "NSDate+RHCalendarAdditions.h"
48 | #import "NSDictionary+RHNumberAdditions.h"
49 | #import "NSJSONSerialization+RHTypeAdditions.h"
50 | #import "NSObject+RHClassInfoAdditions.h"
51 | #import "NSString+RHCaseAdditions.h"
52 | #import "NSString+RHNumberAdditions.h"
53 | #import "NSString+RHRot13Additions.h"
54 | #import "NSString+RHURLEncodingAdditions.h"
55 | #import "NSThread+RHBlockAdditions.h"
56 | #import "NSUserDefaults+RHColorAdditions.h"
57 |
58 | //objects
59 | #import "RHGoogleURLShortener.h"
60 | #import "RHMushParser.h"
61 | #import "RHProgressiveURLConnection.h"
62 | #import "RHWeakSelectorForwarder.h"
63 | #import "RHWeakValue.h"
64 |
65 | #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
66 | //ios only
67 | #import "UIApplication+RHStatusBarBoundsAdditions.h"
68 | #import "UIColor+RHInterpolationAdditions.h"
69 | #import "UIDevice+RHDeviceIdentifierAdditions.h"
70 | #import "UIImage+RHComparingAdditions.h"
71 | #import "UIImage+RHPixelAdditions.h"
72 | #import "UIImage+RHResizingAdditions.h"
73 | #import "UILabel+RHSizeAdditions.h"
74 | #import "UIView+RHCompletedActionBadgeAdditions.h"
75 | #import "UIView+RHSnapshotAdditions.h"
76 |
77 | #else
78 | //mac only
79 | #import "NSAlert+RHBlockAdditions.h"
80 | #import "NSBundle+RHLaunchAtLoginAdditions.h"
81 | #import "NSImage+RHImageRepresentationAdditions.h"
82 | #import "NSImage+RHResizableImageAdditions.h"
83 | #import "NSImageView+RHImageLoadingAdditions.h"
84 | #import "NSImageView+RHImageRectAdditions.h"
85 | #import "NSTextField+RHLabelAdditions.h"
86 | #import "NSTextField+RHSizeAdditions.h"
87 | #import "NSView+RHSnapshotAdditions.h"
88 | #import "NSWindow+RHPreventCaptureAdditions.h"
89 | #import "NSWindow+RHResizeAdditions.h"
90 | #import "RHGetBSDProcessList.h"
91 | #import "RHDraggableImageView.h"
92 |
93 | #endif
94 |
95 |
--------------------------------------------------------------------------------
/RHAdditions/RHDraggableImageView.h:
--------------------------------------------------------------------------------
1 | //
2 | // RHDraggableImageView.h
3 | //
4 | // Created by Richard Heard on 6/10/2013.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface RHDraggableImageView : NSImageView {
33 | BOOL _allowsDragging;
34 |
35 | BOOL _mouseDown;
36 | NSPoint _mouseDownPoint;
37 | NSEvent *_mouseDownEvent;
38 | BOOL _dragInProgress;
39 | NSInteger _currentDraggingSequenceNumber;
40 |
41 | NSString *_representedFilename;
42 | NSURL *_representedURL;
43 |
44 | CGFloat _maximumDragImageEdgeSize;
45 | }
46 |
47 | @property (assign) BOOL allowsDragging; //defaults to yes
48 |
49 | @property (nonatomic, copy) NSString *representedFilename; //filename for the image, provided to the drag recipient. default is "DraggedImage.png" unless representedURL has been set, in which case lastPathComponent is used. If set, will override lastPathComponent.
50 | @property (nonatomic, copy) NSURL *representedURL; //if specified, instead of creating a new PNG representation for a drag promise, the file at the url will instead be copied to the requested destination.
51 |
52 | @property (nonatomic, assign) CGFloat maximumDragImageEdgeSize; //cap the size of the dragging representation so as to not be unwieldy on screen. default is 400.
53 |
54 | @end
55 |
56 | extern CGFloat RHDistanceBetweenPoints(NSPoint p1, NSPoint p2);
57 |
58 |
--------------------------------------------------------------------------------
/RHAdditions/RHGetBSDProcessList.h:
--------------------------------------------------------------------------------
1 | //
2 | // RHGetBSDProcessList.h
3 | //
4 | // Created by Richard Heard on 11/04/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 | #include
32 |
33 | extern NSArray* RHGetBSDProcessList(void); //returns an array of dictionaries with { processID / processName / userID / userName }
34 | extern int GetBSDProcessList(struct kinfo_proc **procList, size_t *procCount);
35 |
36 |
--------------------------------------------------------------------------------
/RHAdditions/RHGoogleURLShortener.h:
--------------------------------------------------------------------------------
1 | //
2 | // RHGoogleURLShortener.h
3 | //
4 | // Created by Richard Heard on 25/05/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | //you must provide your google service API key to use this class eg:
33 | //[RHGoogleURLShortener setGoogleURLShortenerAPIKey:@"123xyz"];
34 |
35 | typedef void (^RHGoogleURLShortenerCompletionBlock)(NSURL *shortURL, NSError *error);
36 | //if shortening fails, we return nil and set error!
37 |
38 | @interface RHGoogleURLShortener : NSObject {
39 | //ivars be private
40 | RHGoogleURLShortenerCompletionBlock _completionBlock;
41 | NSURL *_originalURL;
42 | NSMutableData *_mutableData;
43 | NSURLConnection *_connection;
44 | id _retainedSelf;
45 | }
46 |
47 | +(void)setGoogleURLShortenerAPIKey:(NSString*)key;
48 |
49 | +(id)shortenURL:(NSURL*)url withCompletion:(RHGoogleURLShortenerCompletionBlock)completion;
50 | -(void)cancel;
51 |
52 | @end
53 |
--------------------------------------------------------------------------------
/RHAdditions/RHLoggingSupport.h:
--------------------------------------------------------------------------------
1 | //
2 | // RHLoggingSupport.h
3 | //
4 | // Created by Richard Heard on 3/07/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // logging macros
30 | //
31 | // To enable logging by default for debug builds use:
32 | // #define RH_LOGGING_ALWAYS (defined(DEBUG) && DEBUG)
33 | //
34 | // To disable logging permanently use:
35 | // #define RH_LOGGING_NEVER 1
36 | //
37 | // Otherwise set RHLoggingEnabled to YES in NSUserDefaults to selectively enable logging. "defaults write RHLoggingEnabled -bool yes"
38 |
39 | #import
40 |
41 | #if (defined(RH_LOGGING_NEVER) && RH_LOGGING_NEVER)
42 | #define RHLog(format, ...)
43 | #else
44 | extern BOOL _RHLoggingEnabled;
45 | extern void _RHLoggingInit(void);
46 |
47 | #define RHLog(format, ...) do{ _RHLoggingInit(); if (__builtin_expect(_RHLoggingEnabled, _RHLoggingEnabled)) NSLog( @"%s:%i %@ ", __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat: format, ##__VA_ARGS__]); } while(0)
48 | #endif
49 |
50 | #define RHErrorLog(format, ...) do{ NSLog( @"%s:%i %@ ", __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat: format, ##__VA_ARGS__]); } while (0)
51 |
52 |
--------------------------------------------------------------------------------
/RHAdditions/RHLoggingSupport.m:
--------------------------------------------------------------------------------
1 | //
2 | // RHLoggingSupport.m
3 | //
4 | // Created by Richard Heard on 22/05/13.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "RHLoggingSupport.h"
31 |
32 | #if (defined(RH_LOGGING_ALWAYS) && RH_LOGGING_ALWAYS)
33 | BOOL _RHLoggingEnabled = YES;
34 | #else
35 | BOOL _RHLoggingEnabled = NO;
36 | #endif
37 |
38 |
39 | #ifndef _RH_LOGGING_INIT
40 | #define _RH_LOGGING_INIT 1
41 |
42 | void _RHLoggingInit(void){
43 | static dispatch_once_t onceToken;
44 | dispatch_once(&onceToken, ^{
45 | if (!_RHLoggingEnabled){
46 | _RHLoggingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"RHLoggingEnabled"];
47 | }
48 | if (_RHLoggingEnabled) NSLog(@"RHLogging enabled.");
49 | });
50 | }
51 |
52 | #endif
53 |
54 |
--------------------------------------------------------------------------------
/RHAdditions/RHMushParser.h:
--------------------------------------------------------------------------------
1 | //
2 | // RHMushParser.h
3 | //
4 | // Created by Richard Heard on 6/11/2013.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // MGMushParser Copyright (c) 2012 Matt Greenfield. All rights reserved.
8 | //
9 | // Redistribution and use in source and binary forms, with or without
10 | // modification, are permitted provided that the following conditions
11 | // are met:
12 | // 1. Redistributions of source code must retain the above copyright
13 | // notice, this list of conditions and the following disclaimer.
14 | // 2. Redistributions in binary form must reproduce the above copyright
15 | // notice, this list of conditions and the following disclaimer in the
16 | // documentation and/or other materials provided with the distribution.
17 | // 3. The name of the author may not be used to endorse or promote products
18 | // derived from this software without specific prior written permission.
19 | //
20 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 | //
31 | // A Cocoa port of the awesome MGMushParser from MGBoxKit.
32 | // https://github.com/sobri909/MGBoxKit
33 | //
34 | // Parse an NSString containing text and markup into a formatted NSAttributedString.
35 | //
36 | // "Mush" Markup Format:
37 | // - **Bold** is achieved with **double asterisks**
38 | // - _Italics_ are achieved with //double slashes//
39 | // - Underlining is achieved with __double underscores__
40 | // - `Monospacing` is achieved with `single back-ticks`
41 | // - Coloured text is achieved with {#6699CC|the coloured text}
42 | //
43 | // eg: NSString *format = @"**bold**, //italic//, __underline__, `monospaced`, and {#0000FF|coloured text}";
44 | //
45 | // Requirements: OS X 10.7+ / iOS 4.0+
46 | //
47 |
48 | #ifdef __APPLE__
49 | #import "TargetConditionals.h"
50 | #endif
51 |
52 | #if (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
53 | #import
54 | #import
55 | #else
56 | #import
57 | #import
58 | #endif
59 |
60 | @interface RHMushParser : NSObject {
61 | NSString *_markup;
62 | id _baseFont;
63 | id _baseColor;
64 |
65 | //internal
66 | id _boldFont;
67 | id _italicFont;
68 | id _monospaceFont;
69 | NSMutableAttributedString *_mutableAttributedString;
70 | }
71 |
72 | #if (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
73 |
74 | @property (nonatomic, copy) NSString *markup;
75 | @property (nonatomic, retain) UIFont *baseFont;
76 | @property (nonatomic, retain) UIColor *baseColor;
77 | +(NSAttributedString *)attributedStringFromMarkup:(NSString *)markup withFont:(UIFont *)font andColor:(UIColor *)color;
78 |
79 | #else
80 |
81 | @property (nonatomic, copy) NSString *markup;
82 | @property (nonatomic, retain) NSFont *baseFont;
83 | @property (nonatomic, retain) NSColor *baseColor;
84 | +(NSAttributedString *)attributedStringFromMarkup:(NSString *)markup withFont:(NSFont *)font andColor:(NSColor *)color;
85 |
86 | #endif
87 |
88 | -(void)parse;
89 | -(void)strip;
90 | -(NSAttributedString *)attributedString;
91 |
92 | @end
93 |
94 | extern id RHColorFromHexString(NSString *string);
95 |
96 |
--------------------------------------------------------------------------------
/RHAdditions/RHProgressiveURLConnection.h:
--------------------------------------------------------------------------------
1 | //
2 | // RHProgressiveURLConnection.h
3 | //
4 | // Created by Richard Heard on 27/10/2013.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | //notifications (object == self)
33 | extern NSString * const RHProgressiveURLConnectionDidUpdateProgressNotification;
34 | extern NSString * const RHProgressiveURLConnectionDidFinishNotification;
35 |
36 | @class RHProgressiveURLConnection;
37 |
38 | typedef void (^RHProgressiveURLConnectionProgressBlock)(RHProgressiveURLConnection *connection, double percentageProgress);
39 | typedef void (^RHProgressiveURLConnectionCompletionBlock)(NSURLResponse *response, NSData *data, NSError *connectionError);
40 |
41 | @interface RHProgressiveURLConnection : NSObject {
42 | @private
43 | NSURLConnection *_connection;
44 | id _delegate; //strong, however nil'd once download is finished
45 |
46 | NSURLResponse *_response;
47 | NSMutableData *_responseData; //we should provide an easy way to keep this out of memory for large files
48 | NSError *_responseError;
49 |
50 | long long _expectedContentLength;
51 | double _percentageProgress;
52 |
53 | NSOperationQueue *_delegateQueue;
54 |
55 | RHProgressiveURLConnectionProgressBlock _progressBlock;
56 | RHProgressiveURLConnectionCompletionBlock _completionBlock;
57 | }
58 |
59 | @property (nonatomic, readonly) NSURLConnection *connection;
60 | @property (nonatomic, readonly) id delegate;
61 |
62 | @property (nonatomic, readonly) NSURLResponse *response;
63 | @property (nonatomic, readonly) NSData *responseData;
64 | @property (nonatomic, readonly) NSError *responseError;
65 |
66 | @property (nonatomic, readonly) long long expectedContentLength;
67 | @property (nonatomic, readonly) double percentageProgress; //KVO observable
68 |
69 | //designated initializer
70 | -(id)initWithRequest:(NSURLRequest *)request delegate:(id /* */)delegate startImmediately:(BOOL)startImmediately; //(delegate has all methods passed through to it from the internal NSURLConnection, if implemented)
71 |
72 | //init helpers
73 | -(id)initWithRequest:(NSURLRequest *)request;
74 | -(id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate;
75 | +(RHProgressiveURLConnection*)connectionWithRequest:(NSURLRequest *)request;
76 |
77 | //passthrough methods to the internal NSURLConnection
78 | -(void)start;
79 | -(void)cancel;
80 |
81 | //progress blocks
82 | -(void)setDelegateQueue:(NSOperationQueue*)blockQueue; //defaults to the main queue (nil == main queue)
83 | -(void)setProgressBlock:(RHProgressiveURLConnectionProgressBlock)progressBlock; //called when our progress changes, useful for updating UI etc.
84 | -(void)setCompletionBlock:(RHProgressiveURLConnectionCompletionBlock)completionBlock; //called on completion, useful for updating UI etc.
85 |
86 | //same as the NSURLConnection counterpart, except that the method returns the created progressiveURLConnection instance, which can be monitored for progress / status changes
87 | +(RHProgressiveURLConnection*)sendAsynchronousRequest:(NSURLRequest*)request queue:(NSOperationQueue*)queue completionHandler:(RHProgressiveURLConnectionCompletionBlock)handler;
88 |
89 | @end
90 |
91 |
--------------------------------------------------------------------------------
/RHAdditions/RHWeakSelectorForwarder.h:
--------------------------------------------------------------------------------
1 | //
2 | // RHWeakSelectorForwarder.h
3 | //
4 | // Created by Richard Heard on 9/09/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // A class that maintains a weak pointer to an object, and forwards any methods
30 | // called on it to its target.
31 | //
32 | // This is useful when you have an NSTimer inside a long lived controller,
33 | // that must run until dealloc'd. (to avoid retain cycles)
34 | //
35 | // We have the timer retain our forwarding object which holds a weak pointer
36 | // to ourselves, hence breaking any retain cycle.
37 |
38 |
39 | #import
40 | #import "RHARCSupport.h"
41 |
42 | @interface RHWeakSelectorForwarder : NSObject {
43 |
44 | #if ARC_IS_ENABLED
45 | __weak id _target;
46 | #else
47 | id _target;
48 | #endif
49 |
50 | BOOL _invalidated;
51 | }
52 |
53 | -(id)initWithTarget:(id)target; //designated initialiser
54 |
55 | #if ARC_IS_ENABLED
56 | @property (nonatomic, weak) id target;
57 | #else
58 | @property (nonatomic, assign) id target;
59 | #endif
60 |
61 | -(void)invalidate; //nils the target; must be called before target goes away, i.e. from inside targets dealloc method
62 | -(BOOL)isValid;
63 |
64 | @end
65 |
66 |
--------------------------------------------------------------------------------
/RHAdditions/RHWeakSelectorForwarder.m:
--------------------------------------------------------------------------------
1 | //
2 | // RHWeakSelectorForwarder.m
3 | //
4 | // Created by Richard Heard on 9/09/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "RHWeakSelectorForwarder.h"
31 |
32 | @implementation RHWeakSelectorForwarder
33 |
34 | @synthesize target=_target;
35 |
36 | -(id)initWithTarget:(id)target{
37 | self = [super init];
38 | if (self){
39 | if (!target) [NSException raise:NSInternalInconsistencyException format:@"Error: Unable to create an RHWeakSelectorForwarder instance with a nil target."];
40 | _target = target;
41 | }
42 | return self;
43 | }
44 |
45 | -(void)invalidate{
46 | _target = nil;
47 | _invalidated = YES;
48 | }
49 |
50 | -(BOOL)isValid{
51 | return !_invalidated;
52 | }
53 |
54 | //the default method forwarding technique that is used when all is well. (faster than invocations). Once invalidated we fall through to the methodSignatureForSelector: && forwardInvocation: methods below.
55 | -(id)forwardingTargetForSelector:(SEL)aSelector{
56 | if (_invalidated) return nil;
57 | return _target; //if target is nil, we fall through to methodSignatureForSelector which then falls through to forwardInvocation:, which logs an appropriate error.
58 | }
59 |
60 |
61 | #pragma mark - Forwarding mechanics.
62 |
63 | -(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
64 | id target = _target;
65 |
66 | if (_invalidated || !target){
67 | //make one up, (this is to force a call to forwardInvocation) its never actually used by forwardInvocation, however this allows us to ignore/raise for the erroneous call.
68 | return [NSMethodSignature signatureWithObjCTypes:[[NSString stringWithFormat:@"%s%s%s", @encode(void), @encode(id), @encode(SEL)] UTF8String]];
69 | }
70 |
71 | //query the target, if it succeeds, return it, default forwarding flow
72 | NSMethodSignature *targetSignature = [target methodSignatureForSelector:aSelector];
73 | if (targetSignature) return targetSignature;
74 |
75 | //default behaviour
76 | return [super methodSignatureForSelector:aSelector];
77 |
78 | }
79 |
80 | -(void)forwardInvocation:(NSInvocation *)anInvocation{
81 | SEL selector = [anInvocation selector];
82 | id target = _target;
83 |
84 | //if invalidated, complain.
85 | if (_invalidated){
86 | NSLog(@"Error: %@, which has been invalidated was asked to forward a method '%s'. Ignoring. Did you forget to invalidate your NSTimer?", self, sel_getName(selector));
87 | return;
88 | }
89 |
90 | //if no target, complain (this could happen under ARC, due to the __weak _target variable declaration)
91 | if (!target){
92 | NSLog(@"Error: %@ was asked to forward a method '%s' to a nil target.", self, sel_getName(selector));
93 | return;
94 | }
95 |
96 |
97 | //otherwise let _target deal with it in an appropriate manor, ie raise an exception or handle it.
98 | [anInvocation invokeWithTarget:target];
99 |
100 | }
101 |
102 | @end
103 |
104 |
--------------------------------------------------------------------------------
/RHAdditions/RHWeakValue.h:
--------------------------------------------------------------------------------
1 | //
2 | // RHWeakValue.h
3 | //
4 | // Created by Richard Heard on 22/07/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface RHWeakValue : NSObject {
33 | id _weakValue;
34 | }
35 |
36 | +(RHWeakValue *)weakValueWithObject:(id)object;
37 |
38 | -(id)initWithObject:(id)object;
39 |
40 | @property (nonatomic, weak, readonly) id weakValue;
41 |
42 | @end
43 |
44 |
--------------------------------------------------------------------------------
/RHAdditions/RHWeakValue.m:
--------------------------------------------------------------------------------
1 | //
2 | // RHWeakValue.m
3 | //
4 | // Created by Richard Heard on 22/07/13.
5 | // Copyright (c) 2013 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "RHWeakValue.h"
31 | #import "RHARCSupport.h"
32 |
33 | @implementation RHWeakValue
34 | @synthesize weakValue=_weakValue;
35 |
36 | +(RHWeakValue *)weakValueWithObject:(id)object{
37 | return arc_autorelease([[self alloc] initWithObject:object]);
38 | }
39 |
40 | -(id)initWithObject:(id)object{
41 | self = [super init];
42 | if (self){
43 | _weakValue = object;
44 | }
45 | return self;
46 | }
47 |
48 | -(id)weakValue{
49 | return _weakValue;
50 | }
51 |
52 | -(BOOL)isEqual:(id)object {
53 | if ([object isKindOfClass:[RHWeakValue class]]){
54 | return [[object weakValue] isEqual:self.weakValue];
55 | } else {
56 | return [super isEqual:object];
57 | }
58 | }
59 |
60 | -(NSString*)description{
61 | return [NSString stringWithFormat:@"<%@: %p, weakValue:%@>", NSStringFromClass(self.class), self, _weakValue];
62 | }
63 |
64 | @end
65 |
66 |
--------------------------------------------------------------------------------
/RHAdditions/UIApplication+RHStatusBarBoundsAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIApplication+RHStatusBarBoundsAdditions.h
3 | //
4 | // Created by Richard Heard on 5/11/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface UIApplication (RHStatusBarBoundsAdditions)
33 |
34 | -(CGFloat)rh_statusBarHeight;
35 | -(CGFloat)rh_statusBarWidth;
36 | -(CGRect)rh_statusBarBounds;
37 |
38 | @end
39 |
40 |
--------------------------------------------------------------------------------
/RHAdditions/UIApplication+RHStatusBarBoundsAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIApplication+RHStatusBarBoundsAdditions.m
3 | //
4 | // Created by Richard Heard on 5/11/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "UIApplication+RHStatusBarBoundsAdditions.h"
31 |
32 | @implementation UIApplication (RHStatusBarBoundsAdditions)
33 |
34 | -(CGFloat)rh_statusBarHeight{
35 | CGRect frame = [[UIApplication sharedApplication] statusBarFrame];
36 |
37 | switch ([[UIApplication sharedApplication] statusBarOrientation]) {
38 | case UIInterfaceOrientationPortrait: return frame.size.height;
39 | case UIInterfaceOrientationPortraitUpsideDown: return frame.size.height;
40 | case UIInterfaceOrientationLandscapeLeft: return frame.size.width;
41 | case UIInterfaceOrientationLandscapeRight: return frame.size.width;
42 | default: break;
43 | }
44 | return 0; //unknown, none
45 | }
46 |
47 | -(CGFloat)rh_statusBarWidth{
48 | CGRect frame = [[UIApplication sharedApplication] statusBarFrame];
49 |
50 | switch ([[UIApplication sharedApplication] statusBarOrientation]) {
51 | case UIInterfaceOrientationPortrait: return frame.size.width;
52 | case UIInterfaceOrientationPortraitUpsideDown: return frame.size.width;
53 | case UIInterfaceOrientationLandscapeLeft: return frame.size.height;
54 | case UIInterfaceOrientationLandscapeRight: return frame.size.height;
55 | default: break;
56 | }
57 | return 0; //unknown, none
58 | }
59 |
60 | -(CGRect)rh_statusBarBounds{
61 | return CGRectMake(0.0f, 0.0f, [self rh_statusBarWidth], [self rh_statusBarHeight]);
62 | }
63 |
64 | @end
65 |
66 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
67 | @interface RHFixCategoryBugClassUIARHSBBA : NSObject @end @implementation RHFixCategoryBugClassUIARHSBBA @end
68 |
69 |
--------------------------------------------------------------------------------
/RHAdditions/UIColor+RHInterpolationAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIColor+RHInterpolationAdditions.h
3 | //
4 | // Created by Richard Heard on 10/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // Provides interpolation between 2 UIColors, useful for frame animations etc.
30 |
31 | #import
32 | #import
33 |
34 | @interface UIColor (RHInterpolationAdditions)
35 |
36 | //if color spaces are not compatible, returns nil;
37 | -(UIColor*)blendedColorWithFraction:(CGFloat)fraction ofColor:(UIColor*)color;
38 |
39 | @end
40 |
41 |
--------------------------------------------------------------------------------
/RHAdditions/UIColor+RHInterpolationAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIColor+RHInterpolationAdditions.m
3 | //
4 | // Created by Richard Heard on 10/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "UIColor+RHInterpolationAdditions.h"
31 |
32 | @implementation UIColor (RHInterpolationAdditions)
33 |
34 | static inline float interpolate(CGFloat a, CGFloat b, CGFloat fraction) {
35 | return (a + ((b - a) * fraction));
36 | }
37 |
38 | -(UIColor*)blendedColorWithFraction:(CGFloat)fraction ofColor:(UIColor*)endColor{
39 | if (fraction <= 0.0f) return self;
40 | if (fraction >= 1.0f) return endColor;
41 |
42 |
43 | CGFloat a1, b1, c1, d1 = 0.0f;
44 | CGFloat a2, b2, c2, d2 = 0.0f;
45 |
46 | #define i(x) interpolate(x ## 1, x ## 2, fraction)
47 |
48 | //iOS5 +
49 | if ([UIColor instancesRespondToSelector:@selector(getWhite:alpha:)]){
50 |
51 | //white
52 | if ([self getWhite:&a1 alpha:&b1] && [endColor getWhite:&a2 alpha:&b2]){
53 | return [UIColor colorWithWhite:i(a) alpha:i(b)];
54 | }
55 |
56 | //RGB
57 | if ([self getRed:&a1 green:&b1 blue:&c1 alpha:&d1] && [endColor getRed:&a2 green:&b2 blue:&c2 alpha:&d2]){
58 | return [UIColor colorWithRed:i(a) green:i(b) blue:i(c) alpha:i(d)];
59 | }
60 |
61 | //HSB
62 | if ([self getHue:&a1 saturation:&b1 brightness:&c1 alpha:&d1] && [endColor getHue:&a2 saturation:&b2 brightness:&c2 alpha:&d2]){
63 | return [UIColor colorWithHue:i(a) saturation:i(b) brightness:i(c) alpha:i(d)];
64 | }
65 | } else if (self.CGColor && endColor.CGColor){
66 | //use the underlying CGColorRef
67 |
68 | NSInteger componentCount = CGColorGetNumberOfComponents(self.CGColor);
69 |
70 | if (componentCount == CGColorGetNumberOfComponents(endColor.CGColor)){
71 | //same number of components, we can interpolate
72 |
73 | const CGFloat *selfComponents = CGColorGetComponents(self.CGColor);
74 | const CGFloat *endComponents = CGColorGetComponents(endColor.CGColor);
75 |
76 | CGFloat *outComponents = calloc(sizeof(CGFloat), componentCount);
77 |
78 | for (int i = 0; i < componentCount ; i++){
79 | outComponents[i] = interpolate(selfComponents[i], endComponents[i], fraction);
80 | }
81 |
82 | CGColorRef newColorRef = CGColorCreate(CGColorGetColorSpace(self.CGColor), outComponents);
83 | UIColor *newColor = [UIColor colorWithCGColor:newColorRef];
84 |
85 | CGColorRelease(newColorRef);
86 | free(outComponents);
87 |
88 | return newColor;
89 | }
90 |
91 | }
92 |
93 | //error
94 | return nil;
95 | }
96 |
97 | @end
98 |
99 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
100 | @interface RHFixCategoryBugClassUICRHIA : NSObject @end @implementation RHFixCategoryBugClassUICRHIA @end
101 |
102 |
--------------------------------------------------------------------------------
/RHAdditions/UIDevice+RHDeviceIdentifierAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIDevice+RHDeviceIdentifierAdditions.h
3 | //
4 | // Created by Richard Heard on 29/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface UIDevice (RHDeviceIdentifierAdditions)
33 |
34 | -(NSString *)rh_macAddressForInterface:(NSString*)interface; //eg en0
35 | -(NSString *)rh_uniqueApplicationDeviceIdentifier;
36 | -(NSString *)rh_uniqueGlobalDeviceIdentifier;
37 |
38 | @end
39 |
40 |
--------------------------------------------------------------------------------
/RHAdditions/UIImage+RHComparingAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+RHComparingAdditions.h
3 | //
4 | // Created by Richard Heard on 18/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // Pixel level image comparison, supports various thresholds for slight variations in images (eg from colour correction etc.)
30 | // also supports resizing one of the 2 images being compared if they are not currently the same size, so that a px by px comparison is possible.
31 |
32 | #import
33 |
34 | #import "UIImage+RHPixelAdditions.h"
35 | #import "UIImage+RHResizingAdditions.h"
36 |
37 | @interface UIImage (RHComparingAdditions)
38 |
39 |
40 | -(CGFloat)percentageDifferenceBetweenImage:(UIImage *)image; //use a default tolerance of 0.1f; (10% of 255 so 25 steps)
41 | -(CGFloat)percentageDifferenceBetweenImage:(UIImage *)image withTolerance:(CGFloat)percentageTolerance; //no scale (ie mismatched images return 1.0f)
42 | -(CGFloat)percentageDifferenceBetweenImage:(UIImage *)image withTolerance:(CGFloat)percentageTolerance andScaleIfImageSizesMismatched:(BOOL)shouldScale;
43 |
44 | @end
45 |
46 |
47 |
48 | #pragma mark - underlying implementation
49 |
50 | CGFloat UIImagePercentageDifferenceBetweenImages(UIImage* image1, UIImage* image2); // default tolerance of 25 (10% of of 255)
51 | CGFloat UIImagePercentageDifferenceBetweenImagesWithTolerance(UIImage* image1, UIImage* image2, CGFloat percentageTolerance, BOOL shouldScaleIfImageSizesMismatched);
52 |
53 |
--------------------------------------------------------------------------------
/RHAdditions/UIImage+RHComparingAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+RHComparingAdditions.m
3 | //
4 | // Created by Richard Heard on 18/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "UIImage+RHComparingAdditions.h"
31 |
32 | @implementation UIImage (RHComparingAdditions)
33 |
34 |
35 | -(CGFloat)percentageDifferenceBetweenImage:(UIImage *)image{
36 | return UIImagePercentageDifferenceBetweenImages(self, image);
37 | }
38 |
39 | -(CGFloat)percentageDifferenceBetweenImage:(UIImage *)image withTolerance:(CGFloat)percentageTolerance{
40 | return UIImagePercentageDifferenceBetweenImagesWithTolerance(self, image, percentageTolerance, NO);
41 | }
42 |
43 | -(CGFloat)percentageDifferenceBetweenImage:(UIImage *)image withTolerance:(CGFloat)percentageTolerance andScaleIfImageSizesMismatched:(BOOL)shouldScale{
44 | return UIImagePercentageDifferenceBetweenImagesWithTolerance(self, image, percentageTolerance, shouldScale);
45 | }
46 |
47 | @end
48 |
49 |
50 | #pragma mark - underlying implementation
51 |
52 | CGFloat UIImagePercentageDifferenceBetweenImages(UIImage* image1, UIImage* image2){
53 | return UIImagePercentageDifferenceBetweenImagesWithTolerance(image1, image2, 0.1f, NO);
54 | }
55 |
56 | CGFloat UIImagePercentageDifferenceBetweenImagesWithTolerance(UIImage* image1, UIImage* image2, CGFloat percentageTolerance, BOOL shouldScaleIfImageSizesMismatched){
57 |
58 | //make sure we have 2 images
59 | if (!image1) return 1.0f;
60 | if (!image2) return 1.0f;
61 | int tolerance = MAX(0.0f, MIN(1.0f, percentageTolerance)) * 255;
62 |
63 | float width = image1.size.width;
64 | float height = image1.size.height;
65 |
66 | if (width != image2.size.width || height != image2.size.height){
67 |
68 | if (shouldScaleIfImageSizesMismatched){
69 | //scale the larger image to match the size of the smaller image
70 | if ((image1.size.width * image1.size.height) > (image2.size.width * image2.size.height)){
71 | image1 = [image1 imageResizedToSize:image2.size];
72 | } else {
73 | image2 = [image2 imageResizedToSize:image1.size];
74 | }
75 |
76 | if (!image1 || !image2){
77 | NSLog(@"Error: Failed to scale an image for comparison.");
78 | return 1.0f;
79 | }
80 |
81 | //reset
82 | width = image1.size.width;
83 | height = image1.size.height;
84 |
85 | } else {
86 | return 1.0f; //maximum difference
87 | }
88 | }
89 |
90 |
91 | pixel *image1Pixels = UIImageCopyRGBAForImage(image1);
92 | pixel *image2Pixels = UIImageCopyRGBAForImage(image2);
93 |
94 | unsigned long detectedDifferences = 0;
95 | unsigned long totalPixelCount = width * height;
96 |
97 | //we will mutate the pointers, so make a copy
98 | pixel *image1Ptr = image1Pixels;
99 | pixel *image2Ptr = image2Pixels;
100 | for (unsigned long index = 0; index < totalPixelCount; index++) {
101 |
102 |
103 | if (abs(image1Ptr->R - image2Ptr->R) > tolerance || abs(image1Ptr->G - image2Ptr->G) > tolerance || abs(image1Ptr->B - image2Ptr->B) > tolerance || abs(image1Ptr->A - image2Ptr->A) > tolerance) {
104 | //one or more pixel components differs by tolerance or more
105 | detectedDifferences++;
106 | }
107 |
108 | //increment pointers
109 | image1Ptr++;
110 | image2Ptr++;
111 |
112 | }
113 |
114 | free(image1Pixels);
115 | free(image2Pixels);
116 |
117 | return (float)detectedDifferences / (float)totalPixelCount;
118 |
119 | }
120 |
121 |
122 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
123 | @interface RHFixCategoryBugClassUIIRHCA : NSObject @end @implementation RHFixCategoryBugClassUIIRHCA @end
124 |
125 |
--------------------------------------------------------------------------------
/RHAdditions/UIImage+RHPixelAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+RHPixelAdditions.h
3 | //
4 | // Created by Richard Heard on 18/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // Supports accessing the underlying raw pixel data of a UIImage, both in bulk and on a point by point basis.
30 | // Returned format is RGBA pre-multiplied as is supported by CoreGraphics.
31 |
32 | #import
33 | #import
34 |
35 |
36 | @interface UIImage (RHPixelAdditions)
37 |
38 |
39 | -(NSData*)rgba;
40 | -(NSData*)rgbaForPoint:(CGPoint)point;
41 |
42 | @end
43 |
44 |
45 | //underlying implementation
46 |
47 | typedef struct pixel {
48 | uint8_t R;
49 | uint8_t G;
50 | uint8_t B;
51 | uint8_t A;
52 | } pixel;
53 |
54 | pixel* UIImageCopyRGBAForImage(UIImage *image);
55 | pixel* UIImageCopyRGBAForPointInImage(CGPoint point, UIImage*image);
56 |
57 | NSData* UIImageGetRGBAForImage(UIImage* image);
58 | NSData* UIImageGetRGBAForPointInImage(CGPoint point, UIImage* image);
59 |
60 |
--------------------------------------------------------------------------------
/RHAdditions/UIImage+RHPixelAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+RHPixelAdditions.m
3 | //
4 | // Created by Richard Heard on 18/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "UIImage+RHPixelAdditions.h"
31 |
32 | @implementation UIImage (RHPixelAdditions)
33 |
34 | -(NSData*)rgba{
35 | return UIImageGetRGBAForImage(self);
36 | }
37 | -(NSData*)rgbaForPoint:(CGPoint)point{
38 | return UIImageGetRGBAForPointInImage(point, self);
39 | }
40 |
41 | @end
42 |
43 |
44 | #pragma mark - underlying implementation
45 |
46 | pixel* UIImageCopyRGBAForImage(UIImage *image){
47 |
48 | if (!image) return NULL;
49 |
50 | CGFloat width = image.size.width;
51 | CGFloat height = image.size.height;
52 | size_t bitsPerComponent = 8;
53 | size_t bytesPerPixel = 4;
54 | size_t bytesPerRow = bytesPerPixel * width;
55 |
56 | pixel *pixels = calloc(width * height, sizeof(pixel));
57 |
58 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
59 | CGContextRef context = CGBitmapContextCreate(pixels, width, height, bitsPerComponent, bytesPerRow, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
60 |
61 | if (context) {
62 | UIGraphicsPushContext(context);
63 | [image drawAtPoint:CGPointMake(0,0)];
64 | UIGraphicsPopContext();
65 | }
66 |
67 | CGColorSpaceRelease(colorSpace);
68 | CGContextRelease(context);
69 |
70 | return pixels;
71 | }
72 |
73 | pixel* UIImageCopyRGBAForPointInImage(CGPoint point, UIImage*image){
74 |
75 | if (!image) return NULL;
76 |
77 | CGFloat width = 1.0f;
78 | CGFloat height = 1.0f;
79 | size_t bitsPerComponent = 8;
80 | size_t bytesPerPixel = 4;
81 | size_t bytesPerRow = bytesPerPixel * width;
82 |
83 | pixel *pixels = calloc(width * height, sizeof(pixel));
84 |
85 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
86 | CGContextRef context = CGBitmapContextCreate(pixels, width, height, bitsPerComponent, bytesPerRow, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
87 |
88 | if (context){
89 | UIGraphicsPushContext(context);
90 | [image drawAtPoint:CGPointMake(-point.x, -point.y)];
91 | UIGraphicsPopContext();
92 | }
93 | CGColorSpaceRelease(colorSpace);
94 | CGContextRelease(context);
95 |
96 | return pixels;
97 | }
98 |
99 |
100 | NSData* UIImageGetRGBAForImage(UIImage* image){
101 | pixel *pixels = UIImageCopyRGBAForImage(image);
102 | if (!pixels) return nil;
103 |
104 | NSData *data = [NSData dataWithBytes:pixels length:(4 * image.size.width * image.size.height)];
105 | free(pixels);
106 |
107 | return data;
108 | }
109 |
110 | NSData* UIImageGetRGBAForPointInImage(CGPoint point, UIImage* image){
111 | pixel *pixels = UIImageCopyRGBAForPointInImage(point, image);
112 | if (!pixels) return nil;
113 |
114 | NSData *data = [NSData dataWithBytes:pixels length:(4 * 1 * 1)];
115 | free(pixels);
116 |
117 | return data;
118 | }
119 |
120 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
121 | @interface RHFixCategoryBugClassUIIRHPA : NSObject @end @implementation RHFixCategoryBugClassUIIRHPA @end
122 |
123 |
--------------------------------------------------------------------------------
/RHAdditions/UIImage+RHResizingAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+RHResizingAdditions.h
3 | //
4 | // Created by Richard Heard on 18/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | // Supports resizing of UIImages that are backed by a CGImage, preserves rotation metadata and scale.
30 |
31 | #import
32 | #import
33 |
34 | @interface UIImage (RHResizingAdditions)
35 |
36 | -(UIImage*)imageResizedToSize:(CGSize)size; //resize the image, preserving image rotation and scale metadata
37 |
38 | @end
39 |
40 |
41 | // underlying implementation
42 | UIImage* UIImageResizeImageToSize(UIImage *image, CGSize size);
43 |
44 |
--------------------------------------------------------------------------------
/RHAdditions/UIImage+RHResizingAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIImage+RHResizingAdditions.m
3 | //
4 | // Created by Richard Heard on 18/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "UIImage+RHResizingAdditions.h"
31 |
32 | @implementation UIImage (RHResizingAdditions)
33 |
34 |
35 | -(UIImage*)imageResizedToSize:(CGSize)size{
36 | return UIImageResizeImageToSize(self, size);
37 | }
38 |
39 | @end
40 |
41 |
42 | #pragma mark - underlying implementation
43 |
44 | UIImage* UIImageResizeImageToSize(UIImage *image, CGSize size){
45 |
46 | CGImageRef imageRef = image.CGImage;
47 | if (!imageRef) return nil; //unsupported
48 |
49 | CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, CGImageGetBitsPerComponent(imageRef), 0, CGImageGetColorSpace(imageRef), CGImageGetBitmapInfo(imageRef));
50 | if (! context) {
51 | //likely an image in a unsupported bitmap parameter combination, try again with a standard set
52 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
53 | context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
54 | CGColorSpaceRelease(colorSpace);
55 | }
56 |
57 | if (!context) return nil; //if that also fails, bail
58 |
59 | //high quality
60 | CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
61 |
62 | CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), image.CGImage);
63 |
64 | CGImageRef resizedImageRef = CGBitmapContextCreateImage(context);
65 |
66 | //apply the same attributes to the new UIImage. (ie we resize the CGImage as is, then let the new image know if it needs to translate it for display etc.)
67 | UIImage *resizedImage = nil;
68 | if (resizedImageRef){
69 | resizedImage = [UIImage imageWithCGImage:resizedImageRef scale:image.scale orientation:image.imageOrientation];
70 | CGImageRelease(resizedImageRef);
71 | }
72 |
73 | CGContextRelease(context);
74 |
75 | return resizedImage;
76 | }
77 |
78 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
79 | @interface RHFixCategoryBugClassUIIRHRA : NSObject @end @implementation RHFixCategoryBugClassUIIRHRA @end
80 |
81 |
--------------------------------------------------------------------------------
/RHAdditions/UILabel+RHSizeAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // UILabel+RHLayoutAdditions.h
3 | //
4 | // Created by Richard Heard on 6/12/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface UILabel (RHSizeAdditions)
33 |
34 | -(CGSize)sizeWithMaxWidth:(CGFloat)maxWidth;
35 | -(CGSize)sizeWithMaxHeight:(CGFloat)maxHeight;
36 |
37 | -(CGFloat)heightForWidth:(CGFloat)width;
38 | -(CGFloat)widthForHeight:(CGFloat)height;
39 | @end
40 |
41 |
--------------------------------------------------------------------------------
/RHAdditions/UILabel+RHSizeAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // UILabel+RHLayoutAdditions.m
3 | //
4 | // Created by Richard Heard on 6/12/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "UILabel+RHSizeAdditions.h"
31 | #import "RHARCSupport.h"
32 |
33 | @implementation UILabel (RHLayoutAdditions)
34 |
35 | -(CGSize)sizeWithMaxWidth:(CGFloat)maxWidth{
36 | if ([self respondsToSelector: @selector(boundingRectWithSize:options:attributes:context:)]) {
37 |
38 | NSMutableParagraphStyle *paragraphStyle = arc_autorelease([[NSMutableParagraphStyle alloc] init]);
39 | paragraphStyle.lineBreakMode = self.lineBreakMode;
40 | NSDictionary *attributes = @{NSFontAttributeName:self.font, NSParagraphStyleAttributeName:paragraphStyle};
41 | CGSize size = [self.text boundingRectWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
42 | return CGSizeMake(MIN(maxWidth, ceilf(size.width)), ceilf(size.height));
43 |
44 | } else {
45 |
46 | #pragma clang diagnostic push
47 | #pragma clang diagnostic ignored "-Wdeprecated-declarations"
48 | CGSize size = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX) lineBreakMode:self.lineBreakMode];
49 | return CGSizeMake(MIN(maxWidth, ceilf(size.width)), ceilf(size.height));
50 | #pragma clang diagnostic pop
51 |
52 | }
53 | }
54 |
55 | -(CGSize)sizeWithMaxHeight:(CGFloat)maxHeight{
56 | if ([self respondsToSelector: @selector(boundingRectWithSize:options:attributes:context:)]) {
57 |
58 | NSMutableParagraphStyle *paragraphStyle = arc_autorelease([[NSMutableParagraphStyle alloc] init]);
59 | paragraphStyle.lineBreakMode = self.lineBreakMode;
60 | NSDictionary *attributes = @{NSFontAttributeName:self.font, NSParagraphStyleAttributeName:paragraphStyle};
61 | CGSize size = [self.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, maxHeight) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
62 | return CGSizeMake(ceilf(size.width), MIN(maxHeight, ceilf(size.height)));
63 |
64 | } else {
65 |
66 | #pragma clang diagnostic push
67 | #pragma clang diagnostic ignored "-Wdeprecated-declarations"
68 | CGSize size = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(CGFLOAT_MAX, maxHeight) lineBreakMode:self.lineBreakMode];
69 | return CGSizeMake(size.width, MIN(maxHeight, size.height));
70 | #pragma clang diagnostic pop
71 |
72 | }}
73 |
74 | -(CGFloat)heightForWidth:(CGFloat)width{
75 | return [self sizeWithMaxWidth:width].height;
76 | }
77 |
78 | -(CGFloat)widthForHeight:(CGFloat)height{
79 | return [self sizeWithMaxHeight:height].width;
80 | }
81 |
82 | @end
83 |
84 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
85 | @interface RHFixCategoryBugClassUILRHSA : NSObject @end @implementation RHFixCategoryBugClassUILRHSA @end
86 |
87 |
--------------------------------------------------------------------------------
/RHAdditions/UIView+RHCompletedActionBadgeAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIView+RHCompletedActionBadgeAdditions.h
3 | //
4 | // Created by Richard Heard on 10/3/11.
5 | // Copyright (c) 2011 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 |
32 | @interface UIView (RHCompletedActionBadgeAdditions)
33 |
34 | -(void)showCompletedActionBadgeImage:(UIImage*)image forSeconds:(NSTimeInterval)seconds;
35 |
36 | @end
37 |
38 |
--------------------------------------------------------------------------------
/RHAdditions/UIView+RHCompletedActionBadgeAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIView+RHCompletedActionBadgeAdditions.m
3 | //
4 | // Created by Richard Heard on 10/3/11.
5 | // Copyright (c) 2011 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import "UIView+RHCompletedActionBadgeAdditions.h"
31 | #import "RHARCSupport.h"
32 |
33 | @implementation UIView (RHCompletedActionBadgeAdditions)
34 |
35 | -(void)showCompletedActionBadgeImage:(UIImage*)image forSeconds:(NSTimeInterval)seconds{
36 | //turn off clips to bounds, but turn it on again once we are done.
37 | BOOL _oldClipsToBounds = self.clipsToBounds;
38 | self.clipsToBounds = NO;
39 |
40 | __block UIImageView *_badge = [[UIImageView alloc] initWithImage:image];
41 | _badge.center = CGPointMake(self.frame.size.width , 0.0f); //checkmark top right, but inset slightly
42 | _badge.alpha = 0.0f;
43 | [_badge setTransform:CGAffineTransformMakeScale(0.01, 0.01)]; //make it small so we can scale it in
44 |
45 | [self addSubview:_badge];
46 |
47 | //delay by 0.8s to give the visual impression of some activity occurring that has succeeded.
48 | [UIView animateWithDuration:0.2f delay:0.8f options:0 animations:^{
49 | _badge.alpha = 1.0f;
50 | [_badge setTransform:CGAffineTransformMakeScale(1.0, 1.0)]; //scale to full size
51 |
52 | } completion:^(BOOL finished) {
53 |
54 | [UIView animateWithDuration:0.8f delay:seconds options:0 animations:^{
55 | //fade out
56 | _badge.alpha = 0.0f;
57 | } completion:^(BOOL finished) {
58 | [_badge removeFromSuperview];
59 | arc_release(_badge);
60 | _badge = nil;
61 | self.clipsToBounds = _oldClipsToBounds;
62 | }];
63 | }];
64 |
65 | }
66 |
67 | @end
68 |
69 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
70 | @interface RHFixCategoryBugClassUIVRHCABA : NSObject @end @implementation RHFixCategoryBugClassUIVRHCABA @end
71 |
72 |
--------------------------------------------------------------------------------
/RHAdditions/UIView+RHSnapshotAdditions.h:
--------------------------------------------------------------------------------
1 | //
2 | // UIView+RHSnapshotAdditions.h
3 | //
4 | // Created by Richard Heard on 15/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 | //Provides UIView snapshotting, useful for animations and cover transitions.
30 |
31 | #import
32 |
33 | @interface UIView (RHSnapshotAdditions)
34 |
35 | -(UIImage*)rh_snapshot;
36 |
37 | @end
38 |
39 |
--------------------------------------------------------------------------------
/RHAdditions/UIView+RHSnapshotAdditions.m:
--------------------------------------------------------------------------------
1 | //
2 | // UIView+RHSnapshotAdditions.m
3 | //
4 | // Created by Richard Heard on 15/03/12.
5 | // Copyright (c) 2012 Richard Heard. All rights reserved.
6 | //
7 | // Redistribution and use in source and binary forms, with or without
8 | // modification, are permitted provided that the following conditions
9 | // are met:
10 | // 1. Redistributions of source code must retain the above copyright
11 | // notice, this list of conditions and the following disclaimer.
12 | // 2. Redistributions in binary form must reproduce the above copyright
13 | // notice, this list of conditions and the following disclaimer in the
14 | // documentation and/or other materials provided with the distribution.
15 | // 3. The name of the author may not be used to endorse or promote products
16 | // derived from this software without specific prior written permission.
17 | //
18 | // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 | // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 | // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 | // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | //
29 |
30 | #import
31 | #import "UIView+RHSnapshotAdditions.h"
32 |
33 | @implementation UIView (RHSnapshotAdditions)
34 |
35 | -(UIImage*)rh_snapshot{
36 |
37 | UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0f);
38 | [self.layer renderInContext:UIGraphicsGetCurrentContext()];
39 | UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
40 | UIGraphicsEndImageContext();
41 |
42 | return image;
43 | }
44 |
45 | @end
46 |
47 | //include an implementation in this file so we don't have to use -load_all for this category to be included in a static lib
48 | @interface RHFixCategoryBugClassUIVRHSA : NSObject @end @implementation RHFixCategoryBugClassUIVRHSA @end
49 |
50 |
--------------------------------------------------------------------------------