Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NSFont+SystemFont
//
// NSFont+SystemFont.h
// xScope
//
// Created by Craig Hockenberry on 4/17/14.
//
// Thanks to http://nshipster.com/method-swizzling/
#import <Cocoa/Cocoa.h>
@interface NSFont (SystemFont)
@end
//
// NSFont+SystemFont.m
// xScope
//
// Created by Craig Hockenberry on 4/17/14.
//
// Thanks to http://nshipster.com/method-swizzling/
#warning DO NOT USE THIS CODE IN A PRODUCTION BUILD
#import <objc/runtime.h>
#import "NSFont+SystemFont.h"
@implementation NSFont (SystemFont)
static void Swizzle(Class class, SEL originalSelector, SEL swizzledSelector)
{
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)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// class methods
{
Class class = object_getClass((id)self);
Swizzle(class, @selector(systemFontOfSize:), @selector(xxx_systemFontOfSize:));
Swizzle(class, @selector(boldSystemFontOfSize:), @selector(xxx_boldSystemFontOfSize:));
}
// instance methods
{
Class class = [self class];
Swizzle(class, @selector(initWithCoder:), @selector(xxx_initWithCoder:));
}
});
}
#pragma mark - Method Swizzling
#define MAC_OS_X_VERSION_NT 0
#if MAC_OS_X_VERSION_NT == 1
#define REPLACEMENT_FONT @"ComicSansMS"
#define REPLACEMENT_BOLD_FONT @"ComicSansMS-Bold"
#else
#define REPLACEMENT_FONT @"HelveticaNeue"
#define REPLACEMENT_BOLD_FONT @"HelveticaNeue-Bold"
#endif
+ (NSFont *)xxx_systemFontOfSize:(CGFloat)fontSize
{
NSFont *result = [NSFont fontWithName:REPLACEMENT_FONT size:fontSize];
//NSLog(@"%s result = %@", __PRETTY_FUNCTION__, result);
return result;
}
+ (NSFont *)xxx_boldSystemFontOfSize:(CGFloat)fontSize
{
NSFont *result = [NSFont fontWithName:REPLACEMENT_BOLD_FONT size:fontSize];
//NSLog(@"%s result = %@", __PRETTY_FUNCTION__, result);
return result;
}
- (id)xxx_initWithCoder:(NSCoder *)coder
{
NSFont *result = nil;
NSFont *originalFont = [self xxx_initWithCoder:coder];
NSString *originalFontName = [originalFont fontName];
if ([originalFontName isEqualToString:@".LucidaGrandeUI"] || [originalFontName isEqualToString:@"LucidaGrande"]) {
result = [[NSFont fontWithName:REPLACEMENT_FONT size:[originalFont pointSize]] retain];
//NSLog(@"%s originalFont = %@ -> result = %@", __PRETTY_FUNCTION__, originalFont, result);
[originalFont release];
}
else if ([originalFontName isEqualToString:@".LucidaGrandeUI-Bold"] || [originalFontName isEqualToString:@"LucidaGrande-Bold"]) {
result = [[NSFont fontWithName:REPLACEMENT_BOLD_FONT size:[originalFont pointSize]] retain];
//NSLog(@"%s originalFont = %@ -> result = %@", __PRETTY_FUNCTION__, originalFont, result);
[originalFont release];
}
else {
result = originalFont;
//NSLog(@"%s result = %@", __PRETTY_FUNCTION__, result);
}
return result;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment