转自: <iOS tips: Custom Fonts> Post by Steve Vlaminck

My good friend google told me that using a custom font in iOS is “easy”. And for the most part it is, but I got tripped up in a few places. I happen to have Apples Keychain example code lying around so I’ll be using that in this example. I am also using Xcode 4.5 and focusing on iOS 5.1, and iOS 6. - See more at: http://refactr.com/blog/2012/09/ios-tips-custom-fonts/#sthash.KJANfVRI.dpuf

Adding the font to your project

Drag the .ttf file to your project.

Make sure “Add to targets” is checked.

Verify the font is in the project. There are two places you can do this.

By selecting the font, and verifying “Target Membership” in the Utilities area.

And By selecting your apps target, selecting the “Build Phases” tab, and verifying that your font is in the “Copy Bundle Resources” section.

And finally, add the font to your Info.plist. Note that most apps change the name of their plist to something like -Info.plist. Add a key of “Fonts provided by application”, and make sure it’s an array, then add your font file name as an item in the array. Make sure you use the exact file name, and that your file name has an all-lowercase extension (.TTF apparently doesn’t work on iPhones, but .ttf does).

Knowing your font

This is where I had some issues. When you add your font you use the file name, but when you use your font you use the font name… The EXACT font name. If you ctrl + click on the .ttf file and select “Get Info”, you can find the “Full Name”. That seems to work with a lot of fonts, but the font I’m using doesn’t work that way. I had to open the .ttf file in the Font Book application, and look at the window header. - See more at:

Using this name, you can log what font names you have available by using the fontNamesForFamilyName method like so:

NSLog(@"tt0001m_: %@",
[UIFont fontNamesForFamilyName:@"Swis721 Lt BT"]
);

Which gives the output:

-- ::10.968 GenericKeychain[:c07] tt0001m_: (
"Swiss721BT-Light"
)

Using your font programmatically

Say you have a UILabel:

UILabel *myLabel = [[UILabel alloc] init];
[myLabel setText:@"Label Text"];

We set the font of our label by creating a UIFont, and setting it to our label with setFont.

UIFont *swissLight = [UIFont
fontWithName:@"Swiss721BT-Light"
size:myLabel.font.pointSize];
[myLabel setFont:swissLight];

Using your font in Interface Builder

This is an interesting solution that I got from stackoverflow. First you need to create a subclass of UILabel. Hit Cmd + N (or go to File > New > File…), select “Cocoa Touch” > “Objective-C class”, and hit “Next”. Name your class something like CustomFontSwissLightLabel and choose UILabel in the “Subclass of” section. Open your CustomFontSwissLightLabel.m file and override the awakeFromNib method like so:

- (void)awakeFromNib {
[super awakeFromNib];
self.font = [UIFont fontWithName:@"Swiss721BT-Light"
size:self.font.pointSize];
}

Now add a UILabel to you .xib file. In the “Utilities” area, under the “Identity Inspector” tab change the Class from UILabel to CustomFontSwissLightLabel.

That’s all there is to it. 

It really is pretty easy, but there are a handful of things that can trip you up, and I wrote this blog post because I got tripped up on every one of them :)

This entry was posted in Agile ProcessesSoftware Development and tagged fontsiOSiPadiPhoneobjective-cXcode. Bookmark the permalink.

iOS:自定义字体的更多相关文章

  1. iOS 自定义字体设置

    有时候客户都要求使用方正兰亭刊黑字体,可是iOS没有自带这个字体,肿么办 ,只能自己自定义字体了,下面是自定义字体的几个重要步骤: 1.下载字体资源文件(.ttf或.otf格式的文件) 比如说你要使用 ...

  2. iOS自定义字体

    1.下载字体库,如:DINCond-Bold.otf 2.双击,在mac上安装 3.把下载的字体库拖入工程中: 4.配置info.plist文件 5.xib方式设置自定义字体:Font选Custom, ...

  3. iOS自定义字体及类目

    1:获取字体文件 从各种渠道下载字体文件ttf, 网站或者从别的ipa里扣出来.(以fzltxh.ttf为例) 2:将fzltxh.ttf文件拷贝到工程中 3:在Info.plist中添加项: Fon ...

  4. iOS自定义字体及类目 分类: ios技术 2015-05-15 16:34 195人阅读 评论(0) 收藏

    1:获取字体文件 从各种渠道下载字体文件ttf, 网站或者从别的ipa里扣出来.(以fzltxh.ttf为例) 2:将fzltxh.ttf文件拷贝到工程中 3:在Info.plist中添加项: Fon ...

  5. 【转】IOS版本自定义字体步骤

    本文转载自:http://quick.cocoachina.com/wiki/doku.php?id=ios%E7%89%88%E6%9C%AC%E4%BD%BF%E7%94%A8%E8%87%AA% ...

  6. iOS - 使用自定义字体-苹方字体

    苹方提供了六个字重,font-family 定义如下:苹方-简 常规体font-family: PingFangSC-Regular, sans-serif;苹方-简 极细体font-family: ...

  7. iOS使用自定义字体的方法(内置和任意下载ttf\otf\ttc字体文件)

    最近做了个有关阅读的应用,使用了自定义字体,学习了一下这方面的知识. 1.首先是最简单也普遍的做法,打包内置字符库文件: 把字体库文件添加到工程,如font1.ttf添加到工程,然后在工程plist添 ...

  8. ios 使用自定义字体

    本文转载至 http://blog.csdn.net/yesjava/article/details/8447596   1.下载要使用的自定义字体,格式通常为ttf.otf文件.这里假设是nokia ...

  9. css3 自定义字体的使用方法

    @font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体不怕只能使用Web安全字体,你们当中或许有许 ...

  10. CSS 自定义字体

    移动端如何兼容UI给的字体 [toc] 移动端的默认字体 IOS 默认中文字体是Heiti SC 默认英文字体是Helvetica 默认数字字体是HelveticaNeue Android 默认中文字 ...

随机推荐

  1. 玩转shell命令行

    移动光标快捷键 ctrl+f 向前移动一个字符 ctrl+b 向后移动一个字符 ctrl+a 移动到当前行首home ctrl+e 移动到当前行尾end alt+f 向前移动一个单词 alt+b 向后 ...

  2. Android开发(三)——Android布局中实现圆角边框

    设置corners_bg.xml(设置边框圆角可以在drawable-mdpi目录里定义一个xml): <?xml version="1.0" encoding=" ...

  3. 真机调试报错error ==Error Domain=NSURLErrorDomain Code=-1009 "似乎已断开与互联网的连接。"

    真机调试报错error ==Error Domain=NSURLErrorDomain Code=-1009 "似乎已断开与互联网的连接." 请注意,错误代码是-1009,网上关于 ...

  4. angularjs去掉加载时的{{}}

    添加css <style>        .ng-cloak {display: none;}    </style> 在body头文件中加上class=ng-cloak &l ...

  5. C#使用线程池创建线程

    using System; using System.Threading; public class Example { public static void Main() { // Queue th ...

  6. Postgres数据库补丁脚本示例

    SET client_encoding = 'UTF8';-- 新建序列和表DROP TABLE IF EXISTS cms_user_unit;DROP SEQUENCE IF EXISTS cms ...

  7. hadoop3.0.0测验

    下载地址: hadoop: http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-3.0.0/ 准备工作: 1.master节点与其他节点需要建 ...

  8. Extjs4.x 共享组件,写法

    1.项目架构如下: /Application /ext--框架核心文件 /common--共享 /resources--共享资源 /src--共享js(例如,多个模块共享一个store等) /mode ...

  9. PCL点云曲面重建(1)

    在测量较小的数据时会产生一些误差,这些误差所造成的不规则数据如果直接拿来曲面重建的话,会使得重建的曲面不光滑或者有漏洞,可以采用对数据重采样来解决这样问题,通过对周围的数据点进行高阶多项式插值来重建表 ...

  10. linux源配置

    阿里云源配置官网:http://mirrors.aliyun.com 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS ...