转自: <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. poj2528(线段树+区间离散)

    题意:那个城市里要竞选市长,然后在一块墙上可以贴海报为自己拉票,每个人可以贴连续的一块区域,后来帖的可以覆盖前面的,问到最后一共可以看到多少张海报.思路:一看就知道是线段树,只是说要利用到离散化,也不 ...

  2. linux磁盘 分区 物理卷 卷组 逻辑卷 文件系统加载点操作案例

    转自:truemylife.linux磁盘 分区 物理卷 卷组 逻辑卷 文件系统加载点操作案例 基本概念: 磁盘.分区.物理卷[物理部分] 卷组[中间部分] 逻辑卷.文件系统[虚拟化后可控制部分] 磁 ...

  3. Moving Swiftly(从OC切换到Swift)

    Moving Swiftlyhtml, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin: 0 ...

  4. 【WPF】树形结构TreeView的用法(MVVM)

    TreeView控件的用法还是有蛮多坑点的,最好记录一下. 参考项目: https://www.codeproject.com/Articles/26288/Simplifying-the-WPF-T ...

  5. Memory Models And Namespaces

    分开编译 不要把变量和函数的定义放到头文件中. 可以分开编译源文件,然后将它们连接起来生成最终的可执行文件. 如果你只修改了一个文件,你可以只重新编译这个文件,之前编译过的其他文件就不需要再次编译了, ...

  6. Andorid Dialog 显示宽度设置

    dialog.getWindow().setLayout(200, LayoutParams.WRAP_CONTENT);

  7. Java byte类型转换成int类型时需要 & 0XFF的原因

    Java byte类型转换成int类型时需要 & 0XFF的原因 假设有byte b  = -1; 那么b的二进制是:1111 1111. 如果将b直接转换为int类型,那么二进制是 1111 ...

  8. 聊下图片滤镜,手机上的,lookup table(颜色查找表

    今天这里要介绍的是lookup table(颜色查找表),简而言之就是通过将每一个原始的颜色进行转换之后成为一个新的颜色. 打一个比方,比如原始颜色是红色(r:255,g:0,b:0),进行转换后变为 ...

  9. js 控制输入文字个数(换行不算)

    如题,换行符在textarea中是要当成一个字符的.用普通的maxlength属性就不行了,于是想到通过事件来控制输入文字的长度. 注意哦,回车换行不能算成字符.这样的话,普通的substring等方 ...

  10. EF5.x Code First 一对多关联条件查询,Contains,Any,All

    背景 通过多个部门id获取所有用户,部门和用户是多对多. 已知部门id,获取该部门包括该部门下的所有子部门的所有用户. 关系如下: public class Entity:IEntity { publ ...