iOS :自定义文字
来源:互联网 发布:知恩中学美术班好吗 编辑:程序博客网 时间:2024/05/17 01:20
转自: <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:
2012-08-24 11:25:10.968 GenericKeychain[7260: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 = [UIFontfontWithName:@"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 :)
- IOS 自定义标题文字
- iOS :自定义文字
- iOS 自定义tabbar上文字出现重影
- iOS 自定义BarButton布局上图下文字
- iOS 开发 系统返回按钮隐藏文字、自定义文字
- IOS 中UIAlertView中获取自定义文本框文字方法
- ios 自定义带占位文字(placeholder)的TextView
- iOS---自定义带占位文字placeholder的TextView
- iOS心得7 按钮的图片和文字自定义位置
- IOS学习 NSNavigationController 自定义:左右按钮、中间文字、背景图片
- iOS UIButton 自定义图片和文字位置详解
- IOS 自定义控件之写文字和画图片
- iOS 自定义textField 显示下划线 左边显示文字 自定义placeholder的颜色和字体
- 自定义ActionBar文字样式
- 自定义 UIButton(图片+文字)
- 自定义textview旋转文字
- 自定义动画状态栏文字
- 自定义画圆+文字
- python 文件系统(1) os 和 os.path
- php-FPM 启动php-fpm: double free or corruption (out)
- 算法设计题目一 顺序表查找
- C# WPF 基础教程
- Andorid时间控件和日期控件的Demo(代码)
- iOS :自定义文字
- 2014.10.14遇到的问题及总结
- 【ZOJ】3820 Building Fire Stations 树的直径
- 10种排序算法总结
- String 的相等性和不可变性
- C# WPF 高级教程
- 最近的生活,我想谈一谈
- 算法设计题目二 冒泡排序改进
- 点聚 保存文档到服务器