APP中的字体使用

苹果动态字体Demo

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对比两个文件得出。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值