转自: <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. Python 操作redis 常用方法

    Python 操作redis 1.字符串 #!/usr/bin/env python # -*- coding:utf-8 -*- import redis # python 操作str class ...

  2. 【LDA】线性判别式分析

    1. LDA是什么 线性判别式分析(Linear Discriminant Analysis),简称为LDA.也称为Fisher线性判别(Fisher Linear Discriminant,FLD) ...

  3. redis集群主流架构方案分析

    Redis在互联网大数据平台有着广泛的应用,主要被用来缓存热点数据,避免海量请求压垮数据库,同时可以提升服务节点的响应速度和并发量.随着数据量的增多,由于redis是占用单台物理机或虚机的内存,内存资 ...

  4. JAVA-JSP内置对象之request对象的其他方法

    相关资料:<21天学通Java Web开发> request对象的其他方法1.request对象除了可以用来获得请求参数,还可以用来获得HTTP标头及其他信息. 方法           ...

  5. laravel中facade serviceprovider的理解

    一个serviceprovider就是一个解决某个功能的公用模块,实际上可以直接用在di里注册然后从di中取出,为啥还要搞个facade呢? 有几个方面的原因 1.把实例化移入到serviceprov ...

  6. Caused by: java.io.IOException: Added a key not lexically larger than previous.

    为了重复这个实验,遇到不少坑 https://www.iteblog.com/archives/1889.html /** * Created by Administrator on 2017/8/1 ...

  7. Mac OS X上如何实现到Linux主机的ssh免登陆

    转载说明: 本文转载自 http://www.aips.me/mac-key-ssh-login-linux.html 生成密钥对 用密码登录远程主机,将公钥拷贝过去 done 第一步:生成密匙对执行 ...

  8. 键盘 Input子系统

    应用层测试代码 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <li ...

  9. valgrind--CPP程序内存泄露检查工具

    内存泄漏是c++程序常见的问题了,特别是服务类程序,当系统模块过多或者逻辑复杂后,很难通过代码看出内存泄漏. valgrind是一个开源的,检测c++程序内存泄漏有效工具,编译时加上-g选项可以定位到 ...

  10. windows已阻止此软件因为无法验证发行者怎么办

    出现提示windows已阻止此软件因为无法验证发行者怎么解决?有的时候访问某些网站会出现类似的提示.导致不能正常运行某个插件,遇到这个问题一般是浏览器的安全级别设置太高了,没有允许脚本控件运行   设 ...