iOS开发之苹果系统包含的所有字体库
本篇文章没有什么技术含量,就是简单的记录一个苹果系统所有字体库的知识点
获取所有字体库的核心代码是:
// 列出所有字体家族
NSArray *fontFamilies = [UIFont familyNames];
// 每个家族包含的字体
NSArray *fonts = [UIFont fontNamesForFamilyName:@"字体家族名"];
添加一下Demo代码:
ViewController.h
#import <UIKit/UIKit.h>typedef enum : NSUInteger {/// 字体库ViewControllerType_Familys = 0,/// 字体ViewControllerType_Fonts = 1,
} ViewControllerType;@interface ViewController : UIViewController@property (nonatomic, assign) ViewControllerType pageType;/// 字体库
@property (nonatomic, strong) NSDictionary *fontsDic;@end
ViewController.m
#import "ViewController.h"
#import "FontTableViewCell.h"@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>/// 列表TableView
@property (nonatomic, strong) UITableView *mineTableView;
/// 数据
@property (nonatomic, strong) NSMutableArray *dataArray;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];[self initData];[self initUI];
}#pragma mark - 初始化数据
- (void)initData {if (self.pageType == ViewControllerType_Familys) {_dataArray = [NSMutableArray new];// 列出所有字体家族NSArray *fontFamilies = [UIFont familyNames];for (NSString *family in fontFamilies) {NSMutableDictionary *familyDic = [NSMutableDictionary new];[familyDic setObject:family forKey:@"fontTitle"];NSArray *fonts = [UIFont fontNamesForFamilyName:family];if (fonts.count > 0) {[familyDic setObject:fonts forKey:@"fontsList"];} else {[familyDic setObject:@[] forKey:@"fontsList"];}[_dataArray addObject:familyDic];}} else {NSArray *fontsArray = self.fontsDic[@"fontsList"];_dataArray = [[NSMutableArray alloc] initWithArray:fontsArray];}
}#pragma mark - 初始化UI
- (void)initUI {if (self.pageType == ViewControllerType_Fonts) {self.title = self.fontsDic[@"fontTitle"];}[self.view addSubview:self.mineTableView];}#pragma mark - Getter / Setter
- (UITableView *)mineTableView {if (!_mineTableView) {_mineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];_mineTableView.showsVerticalScrollIndicator = NO;_mineTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;_mineTableView.delegate = self;_mineTableView.dataSource = self;_mineTableView.backgroundColor = [UIColor clearColor];if (self.pageType == ViewControllerType_Familys) {_mineTableView.tableHeaderView = [self createHeaderView];}}return _mineTableView;
}#pragma mark - 创建头View
- (UIView *)createHeaderView {UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];UILabel *headLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];headLab.text = @"苹果系统包含的所有字体库";headLab.textAlignment = NSTextAlignmentCenter;headLab.textColor = [UIColor blackColor];headLab.font = [UIFont boldSystemFontOfSize:20];headLab.backgroundColor = [UIColor cyanColor];[headView addSubview:headLab];return headView;
}#pragma mark - 列表代理 UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.dataArray.count;
}- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {FontTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mineCell"];if (!cell) {cell = [[FontTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mineCell"];}if (self.pageType == ViewControllerType_Familys) {[cell setHeaderDataModel:self.dataArray[indexPath.row]];} else {NSDictionary *dic = @{@"font" : self.dataArray[indexPath.row]};[cell setDataModel:dic];}return cell;
}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {if (self.pageType == ViewControllerType_Familys) {return 44;;} else {return 88;;}
}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {if (self.pageType == ViewControllerType_Familys) {ViewController *vc = [ViewController new];vc.pageType = ViewControllerType_Fonts;vc.fontsDic = self.dataArray[indexPath.row];[self.navigationController pushViewController:vc animated:YES];}
}@end
FontTableViewCell的代码:
FontTableViewCell.h
#import <UIKit/UIKit.h>/** 屏幕宽高 */
#define kScreenWidth ([[UIScreen mainScreen] bounds].size.width)
#define kScreenHeight ([[UIScreen mainScreen] bounds].size.height)NS_ASSUME_NONNULL_BEGIN@interface FontTableViewCell : UITableViewCell/// 设置数据
- (void)setDataModel:(NSDictionary *)model;/// 设置数据
- (void)setHeaderDataModel:(NSDictionary *)model;@endNS_ASSUME_NONNULL_ENDFontTableViewCell.m
#import "FontTableViewCell.h"@interface FontTableViewCell ()/// lab
@property (nonatomic, strong) UILabel *fontLab;@end@implementation FontTableViewCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {self.backgroundColor = [UIColor clearColor];[self.contentView addSubview:self.fontLab];}return self;
}#pragma mark - Getter / Setter
- (UILabel *)fontLab {if (!_fontLab) {_fontLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];_fontLab.textColor = [UIColor blackColor];_fontLab.textAlignment = NSTextAlignmentCenter;_fontLab.clipsToBounds = YES;}return _fontLab;
}#pragma mark - 设置数据
- (void)setDataModel:(NSDictionary *)model {self.fontLab.frame = CGRectMake(0, 0, kScreenWidth, 88);self.fontLab.text = model[@"font"];self.fontLab.font = [UIFont fontWithName:model[@"font"] size:20];}/// 设置数据
- (void)setHeaderDataModel:(NSDictionary *)model {self.fontLab.frame = CGRectMake(0, 0, kScreenWidth, 44);self.fontLab.text = model[@"fontTitle"];self.fontLab.font = [UIFont systemFontOfSize:16];}@end
最终Demo样式如下:
欢迎收藏,谢谢浏览,转载请注明出处,十分感谢~