Last active
August 29, 2015 14:04
-
-
Save laiso/fe754ecbfdcae276b02a to your computer and use it in GitHub Desktop.
UIViewController+ClassNameOverlay
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
#ifdef DEBUG | |
#import "UIViewController+ClassNameOverlay.h" | |
#endif |
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 <UIKit/UIKit.h> | |
@interface UIViewController (ClassNameOverlay) | |
@end |
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 "UIViewController+ClassNameOverlay.h" | |
#import <objc/runtime.h> | |
@implementation UIViewController(ClassNameOverlay) | |
+ (void)load | |
{ | |
// http://nshipster.com/method-swizzling/ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
SEL originalSelector = @selector(viewWillAppear:); | |
SEL swizzledSelector = @selector(cno_viewWillAppear:); | |
Class class = [self class]; | |
Method originalMethod = class_getInstanceMethod(class, originalSelector); | |
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); | |
BOOL didAddMethod = | |
class_addMethod(class, | |
originalSelector, | |
method_getImplementation(swizzledMethod), | |
method_getTypeEncoding(swizzledMethod)); | |
if (didAddMethod) { | |
class_replaceMethod(class, | |
swizzledSelector, | |
method_getImplementation(originalMethod), | |
method_getTypeEncoding(originalMethod)); | |
} else { | |
method_exchangeImplementations(originalMethod, swizzledMethod); | |
} | |
}); | |
} | |
- (void)cno_viewWillAppear:(BOOL)animated | |
{ | |
[self cno_viewWillAppear:animated]; | |
UIColor *color = [self randomColor]; | |
UILabel *classNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width, 44)]; | |
classNameLabel.text = NSStringFromClass([self class]); | |
classNameLabel.numberOfLines = 0; | |
classNameLabel.font = [UIFont boldSystemFontOfSize:14]; | |
classNameLabel.textColor = color; | |
classNameLabel.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8]; | |
if ([classNameLabel.text hasPrefix:@"UI"]) { | |
classNameLabel.hidden = YES; | |
} else { | |
self.view.layer.borderWidth = 5; | |
self.view.layer.borderColor = color.CGColor; | |
} | |
[self.view addSubview:classNameLabel]; | |
} | |
- (UIColor *)randomColor | |
{ | |
// https://gist.github.com/r3econ/9772706 | |
CGFloat hue = ( arc4random() % 256 / 256.0 ); | |
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; | |
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; | |
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment