Last active
August 29, 2015 13:56
-
-
Save angelolloqui/8976802 to your computer and use it in GitHub Desktop.
Hack into Pixate to set the cornerRadius into the layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <objc/runtime.h> | |
@protocol Custom_PXBoxModel <NSObject> | |
- (BOOL)hasCornerRadius; | |
- (CGSize)radiusTopLeft; | |
- (CGSize)radiusTopRight; | |
- (CGSize)radiusBottomRight; | |
- (CGSize)radiusBottomLeft; | |
@end | |
@protocol Custom_PXContext <NSObject> | |
- (id<Custom_PXBoxModel>)boxModel; | |
@end | |
@protocol Custom_PXUIView <NSObject> | |
- (void)updateStyleWithRuleSet:(id)rules context:(id<Custom_PXContext>)context; | |
- (void)customUpdateStyleWithRuleSet:(id)rules context:(id<Custom_PXContext>)context; | |
@end | |
static void PXUIView_updateStyleWithRuleSet(UIView<Custom_PXUIView> *self, SEL _cmd, id rules, id<Custom_PXContext>context) { | |
[self customUpdateStyleWithRuleSet:rules context:context]; | |
//If corner radius | |
id<Custom_PXBoxModel> boxModel = [context boxModel]; | |
if ([boxModel hasCornerRadius]) { | |
//Check all radius are equal and change the cornerRadius property in my view | |
CGSize radiusSize = [boxModel radiusTopLeft]; | |
if (CGSizeEqualToSize(radiusSize, [boxModel radiusTopRight]) && | |
CGSizeEqualToSize(radiusSize, [boxModel radiusBottomLeft]) && | |
CGSizeEqualToSize(radiusSize, [boxModel radiusBottomRight])) { | |
self.layer.cornerRadius = radiusSize.height; | |
} | |
} | |
} | |
@implementation UIView (MOAStyling) | |
+ (void)load { | |
//Get main classes | |
Class c_PXUIView = NSClassFromString(@"PXUIView"); | |
Class c_PX_AT = NSClassFromString(@"PX_AT"); | |
Class c_PXBoxModel = NSClassFromString(@"PXBoxModel"); | |
if (c_PXUIView && c_PX_AT && c_PXBoxModel) { | |
//Get all methods | |
Method boxModel = class_getInstanceMethod(c_PX_AT, @selector(boxModel)); | |
Method hasCornerRadius = class_getInstanceMethod(c_PXBoxModel, @selector(hasCornerRadius)); | |
Method radiusTopLeft = class_getInstanceMethod(c_PXBoxModel, @selector(radiusTopLeft)); | |
Method radiusTopRight = class_getInstanceMethod(c_PXBoxModel, @selector(radiusTopRight)); | |
Method radiusBottomRight = class_getInstanceMethod(c_PXBoxModel, @selector(radiusBottomRight)); | |
Method radiusBottomLeft = class_getInstanceMethod(c_PXBoxModel, @selector(radiusBottomLeft)); | |
Method method = class_getInstanceMethod(c_PXUIView, @selector(updateStyleWithRuleSet:context:)); | |
if (method && boxModel && hasCornerRadius && radiusTopLeft && radiusTopRight && radiusBottomLeft && radiusBottomRight) { | |
SEL customSelector = @selector(customUpdateStyleWithRuleSet:context:); | |
//Add our new method | |
class_addMethod(c_PXUIView, customSelector, (IMP)PXUIView_updateStyleWithRuleSet, method_getTypeEncoding(method)); | |
//Swizzle | |
Method method2 = class_getInstanceMethod(c_PXUIView, customSelector); | |
method_exchangeImplementations(method, method2); | |
return; | |
} | |
} | |
NSLog(@"Pixate version not compatible with border-radius hack"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment