Skip to content

Instantly share code, notes, and snippets.

@bnickel
Last active June 11, 2018 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnickel/312e1d7d0e86e0a50a2b83ca10b32432 to your computer and use it in GitHub Desktop.
Save bnickel/312e1d7d0e86e0a50a2b83ca10b32432 to your computer and use it in GitHub Desktop.
Randomly change the font size while running your app.
@import UIKit;
@interface SEPrankster: NSObject
@property (class, readonly) SEPrankster *sharedPrankser;
- (void)setUpFontRandomness:(BOOL)run;
- (void)setFontCategory:(UIContentSizeCategory)category;
@end
@import JRSwizzle;
#import "SEPrankster.h"
static UIContentSizeCategory SEPranksterPrankContentSizeCategory = nil;
@interface SEPrankster ()
@property (nonatomic) NSTimer *dynamicTypeTimer;
@end
@interface UIApplication (Prank)
- (UIContentSizeCategory)SE_preferredContentSizeCategory;
@end
@implementation SEPrankster
+ (void)load
{
[UIApplication jr_swizzleMethod:@selector(preferredContentSizeCategory) withMethod:@selector(SE_preferredContentSizeCategory) error:NULL];
}
+ (instancetype)sharedPrankster
{
static SEPrankster *prankster;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
prankster = [[self alloc] init];
});
return prankster;
}
- (void)setUpFontRandomness:(BOOL)run
{
if (run && self.dynamicTypeTimer == nil) {
self.dynamicTypeTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(changeSeats:) userInfo:nil repeats:YES];
} else if (!run && self.dynamicTypeTimer) {
[self.dynamicTypeTimer invalidate];
self.dynamicTypeTimer = nil;
[self setFontCategory:nil];
}
}
- (void)changeSeats:(id)blahBlahBlah
{
NSArray<UIContentSizeCategory> *fonts = @[UIContentSizeCategorySmall, UIContentSizeCategoryMedium, UIContentSizeCategoryLarge, UIContentSizeCategoryExtraSmall, UIContentSizeCategoryExtraLarge, UIContentSizeCategoryExtraExtraLarge, UIContentSizeCategoryExtraExtraExtraLarge];
[self setFontCategory:fonts[arc4random_uniform((u_int32_t)fonts.count)]];
}
- (void)setFontCategory:(UIContentSizeCategory)category
{
SEPranksterPrankContentSizeCategory = category;
NSLog(@"Mix up time! Becoming: %@", [[UIApplication sharedApplication] preferredContentSizeCategory]);
[[NSNotificationCenter defaultCenter] postNotificationName:UIContentSizeCategoryDidChangeNotification object:[UIApplication sharedApplication] userInfo:@{UIContentSizeCategoryNewValueKey: [[UIApplication sharedApplication] preferredContentSizeCategory]}];
}
@end
@implementation UIApplication (Prank)
- (UIContentSizeCategory)SE_preferredContentSizeCategory
{
return SEPranksterPrankContentSizeCategory ?: [self SE_preferredContentSizeCategory];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment