iOS_第三方字体的导入和使用

本文介绍如何在iOS应用中导入和使用第三方字体文件,包括字体文件的添加步骤、获取字体名称的方法以及如何在UILabel中展示字体效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明: 在iOS中, 系统的字体并不适用于中文, 也不一定是我们想要的. 此时, 我们可以通过向工程导入第三方字体文件(.ttf), 使用我们想要的字体样式.

字体文件和工程文件下载地址:

文章中尽量不使用或少使用封装, 目的是让大家清楚为了实现功能所需要的官方核心API是哪些(如果使用封装, 会在封装外面加以注释)

  • 此文章由 @春雨 编写. 经 @Scott,@黑子 审核. 若转载此文章,请注明出处和作者

iOS中第三方字体的导入和使用

核心API

Class : UIFont, UILable
涉及的API:(API的官方详细注释详见本章结尾)

/** 获取字体族名称的方法. */
+ (NSArray *)familyNames

/** 获取字体家族内的具体字体名称. */
+ (NSArray *)fontNamesForFamilyName:(NSString *)familyName

/** 用指定的字体名称和大小创建UIFont对象. */
+ (UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize

功能实现

Code:

1 . 导入第三方字体文件到工程里.(字体文件链接在说明里.)

  • 首先将第三方字体文件拖入工程里(第三方字体文件名最好不要随意更改, 更改后文件可能会不起作用.);
  • 进入Suporting Files文件夹中的Info.plist文件.
  • 在Info.plist文件中添加一行, Key值选择Fonts provided by application(如果已经有这个数组, 可直接在里面添加)
  • 在数组中添加一行, Value为文件名称(例: manhua.ttf)

bbb

这里写图片描述

2 . 获取添加的第三方字体的名称.(字体家族名称不一定和文件名称相同, 所以有时我们需要自己查找第三方字体家族名称)

/** 1. 获取字体家族名称的数组. */
NSArray *otherFontNameArray = [UIFont familyNames];  

/** 2. 打印输出所有字体家族的名称, 找到添加的第三方字体家族名称, 并记录下来. (我在工程里写了快速获取第三方字体家族名称的方法, 链接地址在说明中.)*/
NSLog(@"otherFontNameArray: %@", otherFontNameArray); 

这里写图片描述

/** 3. 获取第三方字体家族内具体的字体名称.(例如粗体, 斜体等)*/
NSArray *fontNamesArray = [UIFont fontNamesForFamilyName:@"MComic HK"]; /**< 可以看出这个第三方字体家族名称和文件名称不相同. */

/** 4. 打印具体的字体名称*/
NSLog(@"MComic HK: %@", fontNamesArray);

这里写图片描述

3 . 创建一个UILabel的对象, 展示字体样式.

/** 1. 创建UILable的对象. */
UILabel *showFontLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 170, 300, 200)];

/** 2. 设置showFontLabel的font属性. */
showFontLabel.font = [UIFont fontWithName:@"MComicHK-Medium" size:30];  /**< */

/** 3. 给showFontLabel的text属性赋值. */
showFontLabel.text = @"Our mind is sponge, our heart is a stream.我们的脑如海绵,        我们的心如溪流.";

/** 4. 设置showFontLabel的行数限制. */
showFontLabel.numberOfLines = 4;

/** 5. 添加到父视图上. */
[self.view addSubview:showFontLabel];

/** 6. 程序是在MRC下运行, 所以要进行内存管理. */
[showFontLabel release];

这里写图片描述


API 官方注释

/** 
 * @brief  Returns an array of font family names available on the system 
 * 
 * @return  An array of NSString objects, each of which contains the name of a font family.
 */
 + (NSArray *)familyNames
/** 
 * @brief  Returns an array of font names available in a particular font family.
 *
 * @param  <familyName> The name of the font family. Use the familyNames method to get an array of the available font family names on the system.
 *
 * @return   An array of NSString objects, each of which contains a font name associated with the specified family.
 */
 + (NSArray *)fontNamesForFamilyName:(NSString *)familyName

 /**
  * @brief   Creates and returns a font object for the specified font name and size.
  *
  * @param   <fontName>  The fully specified name of the font. This name incorporates both the font family name and the specific style information for the font.
  *
  * @param  <fontSize>  The size (in points) to which the font is scaled. This value must be greater than 0.0.
  * 
  * @return  A font object of the specified name and size.
  */

 + (UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值