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

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

2015-08-15 15:56 399 查看
说明: 在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)





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];




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

功能实现
Code

API 官方注释

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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: