Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
全局修改字体(Change font globally)
//
// NSObject+SwizzelingUtils.h
// Font
//
// Created by 宗仁 on 2017/1/23.
// Copyright © 2017年 宗仁. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSObject(SwizzelingUtils)
+(void)swizzlingClassMethod:(SEL)originalSelector toMethod:(SEL)swizzledSelector;
@end
//
// NSObject+SwizzelingUtils.m
// Font
//
// Created by 宗仁 on 2017/1/23.
// Copyright © 2017年 宗仁. All rights reserved.
//
#import "NSObject+SwizzelingUtils.h"
#import <objc/runtime.h>
@implementation NSObject(SwizzelingUtils)
+(void)swizzlingClassMethod:(SEL)originalSelector toMethod:(SEL)swizzledSelector{
Class class = object_getClass((id)self);
Method originalMethod = class_getClassMethod(class, originalSelector);
Method swizzledMethod = class_getClassMethod(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);
}
}
@end
//
// UIFont+Swizzeling.h
// Font
//
// Created by 宗仁 on 2017/1/23.
// Copyright © 2017年 宗仁. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIFont(Swizzeling)
@end
//
// UIFont+Swizzeling.m
// Font
//
// Created by 宗仁 on 2017/1/23.
// Copyright © 2017年 宗仁. All rights reserved.
//
#import "UIFont+Swizzeling.h"
#import "UIFont+YSCCustom.h"
#import "NSObject+SwizzelingUtils.h"
@implementation UIFont(Swizzeling)
+ (void)load {
[UIFont swizzlingClassMethod:@selector(zr_fontWithName:size:) toMethod:@selector(fontWithName:size:)];
[UIFont swizzlingClassMethod:@selector(zr_systemFontOfSize:) toMethod:@selector(systemFontOfSize:)];
}
@end
//
// UIFont+YSCCustom.h
// Font
//
// Created by 宗仁 on 2017/1/23.
// Copyright © 2017年 宗仁. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIFont(YSCCustom)
+ (nullable UIFont *)customFontOfSize:(CGFloat)fontSize;
+ (nullable UIFont *)zr_systemFontOfSize:(CGFloat)fontSize;
+ (nullable UIFont *)zr_fontWithName:(NSString * _Nonnull)fontName size:(CGFloat)fontSize;
@end
//
// UIFont+YSCCustom.m
// Font
//
// Created by 宗仁 on 2017/1/23.
// Copyright © 2017年 宗仁. All rights reserved.
//
#import "UIFont+YSCCustom.h"
static NSString* const CUSTOM_FONT_NAME = @"HYm7gj";
@implementation UIFont(YSCCustom)
+ (nullable UIFont *)customFontOfSize:(CGFloat)fontSize{
return [UIFont zr_fontWithName:CUSTOM_FONT_NAME size:fontSize];
}
+ (UIFont *)zr_systemFontOfSize:(CGFloat)fontSize{
return [UIFont customFontOfSize:fontSize];
}
+ (nullable UIFont *)zr_fontWithName:(NSString *)fontName size:(CGFloat)fontSize{
return [UIFont zr_fontWithName:CUSTOM_FONT_NAME size:fontSize];
}
@end
@implementation Usage
-(instancetype)init{
UIFont *font = [UIFont systemFontOfSize:48];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.