Skip to content

Instantly share code, notes, and snippets.

@laiso
Last active August 29, 2015 14:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laiso/fe754ecbfdcae276b02a to your computer and use it in GitHub Desktop.
Save laiso/fe754ecbfdcae276b02a to your computer and use it in GitHub Desktop.
UIViewController+ClassNameOverlay
#ifdef DEBUG
#import "UIViewController+ClassNameOverlay.h"
#endif
#import <UIKit/UIKit.h>
@interface UIViewController (ClassNameOverlay)
@end
#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