Instantly share code, notes, and snippets.

Embed
What would you like to do?
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