APP中添加自定义字体并使用。
效果图
实操:
1、添加自定义字体文件
2、配置项目plist
文件
设置属性Fonts provided by application
类型为NSArray
3、配置Build Phases
4、实现字体设置
NSString *fontName = xxxx;
UIFont *font = [UIFont fontWithName:fontName size:15.0];
代码实现示例
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
#define SaveFont(name) [FontUtil saveFontName:name]
#define SetFont(size) [FontUtil fontWithSize:size]
@interface FontUtil : NSObject
/// 保存字体
+ (void)saveFontName:(NSString *)name;
/// 获取字体
+ (UIFont *)fontWithSize:(CGFloat)size;
/// 显示所有字体
+ (void)showFonts;
@end
NS_ASSUME_NONNULL_END
#import "FontUtil.h"
#ifdef DEBUG
#define NSLog(FORMAT, ...) fprintf(stderr,"%s\n",[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(...)
#endif
@implementation FontUtil
+ (void)saveFontName:(NSString *)name
{
if (name == nil || name.length <= 0) {
name = @"";
}
[[NSUserDefaults standardUserDefaults] setObject:name forKey:@"fontName"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+ (UIFont *)fontWithSize:(CGFloat)size
{
if (size <= 0.0) {
size = [UIFont systemFontSize];
}
NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"fontName"];
if (name == nil || name.length <= 0) {
return [UIFont systemFontOfSize:size];
}
UIFont *font = [UIFont fontWithName:name size:size];
return font;
}
+ (void)showFonts
{
//
UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
NSLog(@"current font: %@", font);
//
NSArray *fonts = [UIFont familyNames];
[fonts enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@: %@", @(idx), obj);
}];
}
@end
#import "FontViewController.h"
@interface FontViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray *fonts;
@property (nonatomic, strong) UILabel *label;
@end
@implementation FontViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = @"字体选择";
//
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"show" style:UIBarButtonItemStyleDone target:self action:@selector(showClick)];
//
self.label = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, (self.view.frame.size.width - 20.0), 100.0)];
[self.view addSubview:self.label];
self.label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
self.label.textColor = [UIColor blackColor];
self.label.numberOfLines = 2;
self.label.text = @"App应用中的字体自定义使用。\niOSDev 开发作者 张先生。";
self.label.font = SetFont(15.0);
//
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, (self.label.frame.origin.y + self.label.frame.size.height), self.view.frame.size.width, (self.view.frame.size.height - self.label.frame.origin.y - self.label.frame.size.height)) style:UITableViewStylePlain];
[self.view addSubview:tableView];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
tableView.delegate = self;
tableView.dataSource = self;
tableView.tableFooterView = [UIView new];
}
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor whiteColor];
[self setEdgesForExtendedLayout:UIRectEdgeNone];
}
- (void)showClick
{
[FontUtil showFonts];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.fonts.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}
NSDictionary *dict = self.fonts[indexPath.row];
NSString *name = dict[@"title"];
name = [NSString stringWithFormat:@"%ld: %@", indexPath.row, name];
cell.textLabel.text = name;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// [tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *dict = self.fonts[indexPath.row];
NSString *name = dict[@"name"];
//
SaveFont(name);
self.label.font = SetFont(15.0);
}
- (NSArray *)fonts
{
if (_fonts == nil) {
NSMutableArray *array = [[NSMutableArray alloc] init];
//
[array addObject:@{@"title":@"默认", @"name":@""}];
[array addObject:@{@"title":@"行楷繁细体", @"name":@"Xingkai TC"}];
[array addObject:@{@"title":@"凌慧体繁", @"name":@"LingWai TC"}];
[array addObject:@{@"title":@"凌慧体简", @"name":@"LingWai SC"}];
[array addObject:@{@"title":@"娃娃体繁", @"name":@"Wawati TC"}];
[array addObject:@{@"title":@"娃娃体简", @"name":@"Wawati SC"}];
[array addObject:@{@"title":@"硬笔楷书", @"name":@"QXyingbikai"}];
[array addObject:@{@"title":@"毛笔行书", @"name":@"duanningmaobixingsu"}];
//
_fonts = [NSArray arrayWithArray:array];
}
return _fonts;
}
@end
其他事项
1、字体资源下载
字体吧
2、使用Mac字体册
步骤:程序坞——Launchpad——其他——字体册——查看字体——菜单栏——文件——导出字体
3、自定义字体实际名称查找及使用
添加自定义到项目中后,不清楚字体的实际名称?怎么办?
解决方法把添加自定义字体前后的所有字体名称打印出来,通过使用Xcode自带的文件对比工具FileMerge
对比两个文件得出。