Created
January 23, 2017 06:28
-
-
Save zongren/a0443cd7bf87b4b3dbd3aef46560fbd1 to your computer and use it in GitHub Desktop.
全局修改字体(Change font globally)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIFont+Swizzeling.h | |
// Font | |
// | |
// Created by 宗仁 on 2017/1/23. | |
// Copyright © 2017年 宗仁. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIFont(Swizzeling) | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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