Skip to content

Instantly share code, notes, and snippets.

@shnhrrsn
Created December 3, 2013 18:53
Show Gist options
  • Save shnhrrsn/7775260 to your computer and use it in GitHub Desktop.
Save shnhrrsn/7775260 to your computer and use it in GitHub Desktop.
Include in your code to create a fake and consistent status bar to take screenshots with.
//
// SHNFakeStatusBar
// Created by Shaun Harison on 12/3/13.
//
#import <objc/runtime.h>
#define CARRIER_STRING @"Apple"
#define TIME_STRING @"Timestamp"
@interface SHNFakeStatusBar : NSObject
@end
@implementation SHNFakeStatusBar
+ (void)load {
Class UIStatusBarTimeItemView = NSClassFromString(@"UIStatusBarTimeItemView");
// iOS 7
if([UIStatusBarTimeItemView instancesRespondToSelector:NSSelectorFromString(@"imageWithText:")]) {
Class UIStatusBarTimeItemView = NSClassFromString(@"UIStatusBarTimeItemView");
Method method = class_getInstanceMethod([self class], @selector(timeViewContentsImage));
method_exchangeImplementations(method, class_getInstanceMethod(UIStatusBarTimeItemView, NSSelectorFromString(@"contentsImage")));
Class UIStatusBarServiceItemView = NSClassFromString(@"UIStatusBarServiceItemView");
method = class_getInstanceMethod([self class], @selector(carrierContentsImage));
method_exchangeImplementations(method, class_getInstanceMethod(UIStatusBarServiceItemView, NSSelectorFromString(@"contentsImage")));
}
// iOS 6
else if([UIStatusBarTimeItemView instancesRespondToSelector:NSSelectorFromString(@"contentsImageForStyle:")]) {
Class UIStatusBarTimeItemView = NSClassFromString(@"UIStatusBarTimeItemView");
Method method = class_getInstanceMethod([self class], @selector(timeContentsImageForStyle:));
method_exchangeImplementations(method, class_getInstanceMethod(UIStatusBarTimeItemView, NSSelectorFromString(@"contentsImageForStyle:")));
Class UIStatusBarServiceItemView = NSClassFromString(@"UIStatusBarServiceItemView");
method = class_getInstanceMethod([self class], @selector(carrierContentsImageForStyle:));
method_exchangeImplementations(method, class_getInstanceMethod(UIStatusBarServiceItemView, NSSelectorFromString(@"contentsImageForStyle:")));
}
}
#pragma mark - iOS 7
- (id)timeViewContentsImage {
return [self imageWithText:TIME_STRING];
}
- (id)carrierContentsImage {
return [self imageWithText:CARRIER_STRING];
}
// Implemented by UIStatusBarItemView
- (id)imageWithText:(NSString*)text { return nil; }
#pragma mark - iOS 6
- (id)timeContentsImageForStyle:(int)style {
CGFloat width = ceilf([TIME_STRING sizeWithFont:[self textFont]].width);
[self beginImageContextWithMinimumWidth:width];
[self drawText:TIME_STRING forStyle:style];
id image = [self imageFromImageContextClippedToWidth:width];
[self endImageContext];
return image;
}
- (id)carrierContentsImageForStyle:(int)style {
CGFloat width = ceilf([CARRIER_STRING sizeWithFont:[self textFont]].width);
[self beginImageContextWithMinimumWidth:width];
[self drawText:CARRIER_STRING forStyle:style];
id image = [self imageFromImageContextClippedToWidth:width];
[self endImageContext];
return image;
}
// Implemented by UIStatusBarItemView
- (id)imageFromImageContextClippedToWidth:(CGFloat)width { return nil; }
- (void)beginImageContextWithMinimumWidth:(CGFloat)width { }
- (void)endImageContext { }
- (void)drawText:(NSString*)text forStyle:(int)style { }
- (UIFont*)textFont { return nil; }
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment