您的位置:首页 > 移动开发 > IOS开发

iOS 使用自定义字体

2017-03-08 16:43 155 查看
iOS 项目使用自定义字体

一、准备部分

1. 下载一个字体库,比如 FZY1JW.TTF ,文件添加到项目中;

2.  在Info.plist 文件中添加字体库;

<key>UIAppFonts</key>
<array>
<string>FZY1JW.TTF</string>
</array>
3. 获取字体的名称(FZY1JW.TTF是文件的名称,不一定是字体的名称)

打开字体库,安装后查看详细信息,这里名字是FZY1JW--GB1-0



二、情景:仅个别地方使用

显然这是最简单不过的了,在需要换字体的地方直接使用:

UIFont *myFont = [UIFont fontWithName:@"FZY1JW--GB1-0" size:18];



三、情景:新项目全局更换自定义字体

显然不能用上面的方式,可以考虑自定义一个UILabel,修改字体为自定义字体。

四、情景:老项目全局更换自定义字体
项目上线了,迭代了好几个版本,老板突然说要换成自定义字体,这种最坑,不过还有更坑的,刚改完下一个版本又不想要自定义字体了......

这种只能用 runtime 修改,这也是OC强大的地方之一。

UIFont+CustomFonts.h

#import <UIKit/UIKit.h>

@interface UIFont (CustomFonts)

@end

UIFont+CustomFonts.m
#import "UIFont+CustomFonts.h"

#import <objc/runtime.h>

NSString *const FORegularFontName = @"FZY1JW--GB1-0";
NSString *const FOBoldFontName = @"FZY1JW--GB1-0";
NSString *const FOItalicFontName = @"FZY1JW--GB1-0";

#pragma mark - UIFont category
@implementation UIFont (CustomFonts)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
+ (void)replaceClassSelector:(SEL)originalSelector withSelector:(SEL)modifiedSelector {
Method originalMethod = class_getClassMethod(self, originalSelector);
Method modifiedMethod = class_getClassMethod(self, modifiedSelector);
method_exchangeImplementations(originalMethod, modifiedMethod);
}

+ (void)replaceInstanceSelector:(SEL)originalSelector withSelector:(SEL)modifiedSelector {
Method originalDecoderMethod = class_getInstanceMethod(self, originalSelector);
Method modifiedDecoderMethod = class_getInstanceMethod(self, modifiedSelector);
method_exchangeImplementations(originalDecoderMethod, modifiedDecoderMethod);
}

+ (UIFont *)regularFontWithSize:(CGFloat)size {
return [UIFont fontWithName:FORegularFontName size:size];
}

+ (UIFont *)boldFontWithSize:(CGFloat)size {
return [UIFont fontWithName:FOBoldFontName size:size];
}

+ (UIFont *)italicFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:FOItalicFontName size:fontSize];
}

- (id)initCustomWithCoder:(NSCoder *)aDecoder {
BOOL result = [aDecoder containsValueForKey:@"UIFontDescriptor"];

if (result) {
UIFontDescriptor *descriptor = [aDecoder decodeObjectForKey:@"UIFontDescriptor"];

NSString *fontName;
if ([descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"] isEqualToString:@"CTFontRegularUsage"]) {
fontName = FORegularFontName;
}
else if ([descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"] isEqualToString:@"CTFontEmphasizedUsage"]) {
fontName = FOBoldFontName;
}
else if ([descriptor.fontAttributes[@"NSCTFontUIUsageAttribute"] isEqualToString:@"CTFontObliqueUsage"]) {
fontName = FOItalicFontName;
}
else {
fontName = descriptor.fontAttributes[@"NSFontNameAttribute"];
}

return [UIFont fontWithName:fontName size:descriptor.pointSize];
}

self = [self initCustomWithCoder:aDecoder];

return self;
}

+ (void)load {
[self replaceClassSelector:@selector(systemFontOfSize:) withSelector:@selector(regularFontWithSize:)];
[self replaceClassSelector:@selector(boldSystemFontOfSize:) withSelector:@selector(boldFontWithSize:)];
[self replaceClassSelector:@selector(italicSystemFontOfSize:) withSelector:@selector(italicFontOfSize:)];

[self replaceInstanceSelector:@selector(initWithCoder:) withSelector:@selector(initCustomWithCoder:)];
}
#pragma clang diagnostic pop

@end

来源这里

使用时在 PrefixHeader.pch 文件中引用即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: