码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发 - 使用自定义字体

时间:2015-06-06 16:38:31      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:字体   自定义   ttf   ios   

上次有个同事问我,XXapp的字体怎么那么萌啊?我也想要搞个萌哒哒的字体!于是,就有了今天这篇blog.
首先,我们在iOS开发中,苹果给我们提供了许多的字体,当然这里我就不列举啦,都是些英文,小汤表示看不懂啦,我要自己搞萌萌哒的字体啊啊啊啊!!!

比如说这样的:

技术分享

又比如说这样的:

技术分享

好了,回归正题,上面卖萌的绝对不是作者君啦.
那天,作者君从网上下载了一个ttf的字体如下:
技术分享

然后小汤我非常淡定的执行了下面这段代码:

 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
[self.view addSubview:label];
label.text = @"Hello 汉字";
label.font = [UIFont fontWithName:@"abc.ttf" size:40];

然后运行,咦?界面上的这个字体怎么还是和原来一样啊?!
是不是有很多用第三方字体的小朋友们遇到过这样的问题啊?

那是肯定的啦!因为在这里我们要的字体的名称不叫这个啦!

那么应该叫什么呢?小汤我先写了这么一段代码:

for(NSString *fontfamilyname in [UIFont familyNames])
{
    NSLog(@"family:‘%@‘",fontfamilyname);
    for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname])
    {
        NSLog(@"\tfont:‘%@‘",fontName);
    }
    NSLog(@"-------------");
}

然后,他打印了这么一坨:

2015-06-06 15:28:40.276 字体测试[1347:37490] family:‘Marion2015-06-06 15:28:40.276 字体测试[1347:37490]    font:‘Marion-Italic‘
2015-06-06 15:28:40.276 字体测试[1347:37490]    font:‘Marion-Bold‘
2015-06-06 15:28:40.276 字体测试[1347:37490]    font:‘Marion-Regular‘
2015-06-06 15:28:40.276 字体测试[1347:37490] -------------
2015-06-06 15:28:40.277 字体测试[1347:37490] family:‘Copperplate2015-06-06 15:28:40.277 字体测试[1347:37490]    font:‘Copperplate-Light‘
2015-06-06 15:28:40.277 字体测试[1347:37490]    font:‘Copperplate2015-06-06 15:28:40.277 字体测试[1347:37490]    font:‘Copperplate-Bold‘
2015-06-06 15:28:40.277 字体测试[1347:37490] -------------
2015-06-06 15:28:40.277 字体测试[1347:37490] family:‘Heiti SC‘
2015-06-06 15:28:40.277 字体测试[1347:37490]    font:‘STHeitiSC-Medium‘
2015-06-06 15:28:40.277 字体测试[1347:37490]    font:‘STHeitiSC-Light‘
2015-06-06 15:28:40.277 字体测试[1347:37490] -------------
2015-06-06 15:28:40.277 字体测试[1347:37490] family:‘Iowan Old Style‘
2015-06-06 15:28:40.277 字体测试[1347:37490]    font:‘IowanOldStyle-Italic‘
2015-06-06 15:28:40.279 字体测试[1347:37490]    font:‘IowanOldStyle-Roman‘
2015-06-06 15:28:40.279 字体测试[1347:37490]    font:‘IowanOldStyle-BoldItalic‘
2015-06-06 15:28:40.279 字体测试[1347:37490]    font:‘IowanOldStyle-Bold‘
2015-06-06 15:28:40.279 字体测试[1347:37490] -------------
2015-06-06 15:28:40.279 字体测试[1347:37490] family:‘Courier New2015-06-06 15:28:40.279 字体测试[1347:37490]    font:‘CourierNewPS-BoldMT‘
2015-06-06 15:28:40.280 字体测试[1347:37490]    font:‘CourierNewPS-ItalicMT‘
2015-06-06 15:28:40.280 字体测试[1347:37490]    font:‘CourierNewPSMT2015-06-06 15:28:40.280 字体测试[1347:37490]    font:‘CourierNewPS-BoldItalicMT‘
2015-06-06 15:28:40.280 字体测试[1347:37490] -------------
2015-06-06 15:28:40.280 字体测试[1347:37490] family:‘Apple SD Gothic Neo‘
2015-06-06 15:28:40.280 字体测试[1347:37490]    font:‘AppleSDGothicNeo-Bold‘
2015-06-06 15:28:40.280 字体测试[1347:37490]    font:‘AppleSDGothicNeo-Thin‘
2015-06-06 15:28:40.280 字体测试[1347:37490]    font:‘AppleSDGothicNeo-UltraLight‘
2015-06-06 15:28:40.281 字体测试[1347:37490]    font:‘AppleSDGothicNeo-Regular‘
2015-06-06 15:28:40.281 字体测试[1347:37490]    font:‘AppleSDGothicNeo-Light‘
2015-06-06 15:28:40.281 字体测试[1347:37490]    font:‘AppleSDGothicNeo-Medium‘
2015-06-06 15:28:40.281 字体测试[1347:37490]    font:‘AppleSDGothicNeo-SemiBold‘
2015-06-06 15:28:40.281 字体测试[1347:37490] -------------
2015-06-06 15:28:40.282 字体测试[1347:37490] family:‘Heiti TC‘
2015-06-06 15:28:40.282 字体测试[1347:37490]    font:‘STHeitiTC-Medium‘
2015-06-06 15:28:40.282 字体测试[1347:37490]    font:‘STHeitiTC-Light‘
2015-06-06 15:28:40.282 字体测试[1347:37490] -------------
2015-06-06 15:28:40.282 字体测试[1347:37490] family:‘Gill Sans‘
2015-06-06 15:28:40.282 字体测试[1347:37490]    font:‘GillSans-Italic‘
2015-06-06 15:28:40.282 字体测试[1347:37490]    font:‘GillSans-Bold‘
2015-06-06 15:28:40.282 字体测试[1347:37490]    font:‘GillSans-BoldItalic‘
2015-06-06 15:28:40.282 字体测试[1347:37490]    font:‘GillSans-LightItalic‘
2015-06-06 15:28:40.282 字体测试[1347:37490]    font:‘GillSans2015-06-06 15:28:40.282 字体测试[1347:37490]    font:‘GillSans-Light‘
2015-06-06 15:28:40.283 字体测试[1347:37490]    font:‘GillSans-SemiBold‘
2015-06-06 15:28:40.283 字体测试[1347:37490]    font:‘GillSans-SemiBoldItalic‘
2015-06-06 15:28:40.283 字体测试[1347:37490]    font:‘GillSans-UltraBold‘
2015-06-06 15:28:40.283 字体测试[1347:37490] -------------
2015-06-06 15:28:40.283 字体测试[1347:37490] family:‘Marker Felt‘
2015-06-06 15:28:40.283 字体测试[1347:37490]    font:‘MarkerFelt-Thin‘
2015-06-06 15:28:40.283 字体测试[1347:37490]    font:‘MarkerFelt-Wide‘
2015-06-06 15:28:40.283 字体测试[1347:37490] -------------
2015-06-06 15:28:40.283 字体测试[1347:37490] family:‘Thonburi2015-06-06 15:28:40.283 字体测试[1347:37490]    font:‘Thonburi2015-06-06 15:28:40.283 字体测试[1347:37490]    font:‘Thonburi-Bold‘
2015-06-06 15:28:40.283 字体测试[1347:37490]    font:‘Thonburi-Light‘
2015-06-06 15:28:40.284 字体测试[1347:37490] -------------
2015-06-06 15:28:40.284 字体测试[1347:37490] family:‘Avenir Next Condensed‘
2015-06-06 15:28:40.284 字体测试[1347:37490]    font:‘AvenirNextCondensed-BoldItalic‘
2015-06-06 15:28:40.284 字体测试[1347:37490]    font:‘AvenirNextCondensed-Heavy‘
2015-06-06 15:28:40.284 字体测试[1347:37490]    font:‘AvenirNextCondensed-Medium‘
2015-06-06 15:28:40.284 字体测试[1347:37490]    font:‘AvenirNextCondensed-Regular‘
2015-06-06 15:28:40.284 字体测试[1347:37490]    font:‘AvenirNextCondensed-HeavyItalic‘
2015-06-06 15:28:40.284 字体测试[1347:37490]    font:‘AvenirNextCondensed-MediumItalic‘
2015-06-06 15:28:40.284 字体测试[1347:37490]    font:‘AvenirNextCondensed-Italic‘
2015-06-06 15:28:40.284 字体测试[1347:37490]    font:‘AvenirNextCondensed-UltraLightItalic‘
2015-06-06 15:28:40.284 字体测试[1347:37490]    font:‘AvenirNextCondensed-DemiBold‘
2015-06-06 15:28:40.285 字体测试[1347:37490]    font:‘AvenirNextCondensed-UltraLight‘
2015-06-06 15:28:40.285 字体测试[1347:37490]    font:‘AvenirNextCondensed-Bold‘
2015-06-06 15:28:40.285 字体测试[1347:37490]    font:‘AvenirNextCondensed-DemiBoldItalic‘
2015-06-06 15:28:40.285 字体测试[1347:37490] -------------
2015-06-06 15:28:40.285 字体测试[1347:37490] family:‘Tamil Sangam MN‘
2015-06-06 15:28:40.285 字体测试[1347:37490]    font:‘TamilSangamMN2015-06-06 15:28:40.285 字体测试[1347:37490]    font:‘TamilSangamMN-Bold‘
2015-06-06 15:28:40.285 字体测试[1347:37490] -------------
2015-06-06 15:28:40.285 字体测试[1347:37490] family:‘Helvetica Neue‘
2015-06-06 15:28:40.285 字体测试[1347:37490]    font:‘HelveticaNeue-Italic‘
2015-06-06 15:28:40.285 字体测试[1347:37490]    font:‘HelveticaNeue-Bold‘
2015-06-06 15:28:40.285 字体测试[1347:37490]    font:‘HelveticaNeue-UltraLight‘
2015-06-06 15:28:40.286 字体测试[1347:37490]    font:‘HelveticaNeue-CondensedBlack‘
2015-06-06 15:28:40.286 字体测试[1347:37490]    font:‘HelveticaNeue-BoldItalic‘
2015-06-06 15:28:40.286 字体测试[1347:37490]    font:‘HelveticaNeue-CondensedBold‘
2015-06-06 15:28:40.286 字体测试[1347:37490]    font:‘HelveticaNeue-Medium‘
2015-06-06 15:28:40.286 字体测试[1347:37490]    font:‘HelveticaNeue-Light‘
2015-06-06 15:28:40.286 字体测试[1347:37490]    font:‘HelveticaNeue-Thin‘
2015-06-06 15:28:40.286 字体测试[1347:37490]    font:‘HelveticaNeue-ThinItalic‘
2015-06-06 15:28:40.286 字体测试[1347:37490]    font:‘HelveticaNeue-LightItalic‘
2015-06-06 15:28:40.286 字体测试[1347:37490]    font:‘HelveticaNeue-UltraLightItalic‘
2015-06-06 15:28:40.286 字体测试[1347:37490]    font:‘HelveticaNeue-MediumItalic‘
2015-06-06 15:28:40.286 字体测试[1347:37490]    font:‘HelveticaNeue2015-06-06 15:28:40.287 字体测试[1347:37490] -------------
2015-06-06 15:28:40.287 字体测试[1347:37490] family:‘Gurmukhi MN‘
2015-06-06 15:28:40.287 字体测试[1347:37490]    font:‘GurmukhiMN-Bold‘
2015-06-06 15:28:40.287 字体测试[1347:37490]    font:‘GurmukhiMN2015-06-06 15:28:40.287 字体测试[1347:37490] -------------
2015-06-06 15:28:40.287 字体测试[1347:37490] family:‘Times New Roman‘
2015-06-06 15:28:40.287 字体测试[1347:37490]    font:‘TimesNewRomanPSMT2015-06-06 15:28:40.287 字体测试[1347:37490]    font:‘TimesNewRomanPS-BoldItalicMT‘
2015-06-06 15:28:40.287 字体测试[1347:37490]    font:‘TimesNewRomanPS-ItalicMT‘
2015-06-06 15:28:40.287 字体测试[1347:37490]    font:‘TimesNewRomanPS-BoldMT‘
2015-06-06 15:28:40.287 字体测试[1347:37490] -------------
2015-06-06 15:28:40.287 字体测试[1347:37490] family:‘Georgia2015-06-06 15:28:40.288 字体测试[1347:37490]    font:‘Georgia-BoldItalic‘
2015-06-06 15:28:40.288 字体测试[1347:37490]    font:‘Georgia2015-06-06 15:28:40.288 字体测试[1347:37490]    font:‘Georgia-Italic‘
2015-06-06 15:28:40.288 字体测试[1347:37490]    font:‘Georgia-Bold‘
2015-06-06 15:28:40.288 字体测试[1347:37490] -------------
2015-06-06 15:28:40.288 字体测试[1347:37490] family:‘Apple Color Emoji‘
2015-06-06 15:28:40.288 字体测试[1347:37490]    font:‘AppleColorEmoji2015-06-06 15:28:40.288 字体测试[1347:37490] -------------
2015-06-06 15:28:40.288 字体测试[1347:37490] family:‘Arial Rounded MT Bold‘
2015-06-06 15:28:40.288 字体测试[1347:37490]    font:‘ArialRoundedMTBold2015-06-06 15:28:40.288 字体测试[1347:37490] -------------
2015-06-06 15:28:40.288 字体测试[1347:37490] family:‘Kailasa2015-06-06 15:28:40.289 字体测试[1347:37490]    font:‘Kailasa-Bold‘
2015-06-06 15:28:40.289 字体测试[1347:37490]    font:‘Kailasa2015-06-06 15:28:40.289 字体测试[1347:37490] -------------
2015-06-06 15:28:40.289 字体测试[1347:37490] family:‘Kohinoor Devanagari‘
2015-06-06 15:28:40.289 字体测试[1347:37490]    font:‘KohinoorDevanagari-Light‘
2015-06-06 15:28:40.289 字体测试[1347:37490]    font:‘KohinoorDevanagari-Medium‘
2015-06-06 15:28:40.289 字体测试[1347:37490]    font:‘KohinoorDevanagari-Book‘
2015-06-06 15:28:40.289 字体测试[1347:37490] -------------
2015-06-06 15:28:40.289 字体测试[1347:37490] family:‘Sinhala Sangam MN‘
2015-06-06 15:28:40.289 字体测试[1347:37490]    font:‘SinhalaSangamMN-Bold‘
2015-06-06 15:28:40.289 字体测试[1347:37490]    font:‘SinhalaSangamMN2015-06-06 15:28:40.290 字体测试[1347:37490] -------------
2015-06-06 15:28:40.290 字体测试[1347:37490] family:‘Chalkboard SE‘
2015-06-06 15:28:40.290 字体测试[1347:37490]    font:‘ChalkboardSE-Bold‘
2015-06-06 15:28:40.290 字体测试[1347:37490]    font:‘ChalkboardSE-Light‘
2015-06-06 15:28:40.290 字体测试[1347:37490]    font:‘ChalkboardSE-Regular‘
2015-06-06 15:28:40.290 字体测试[1347:37490] -------------
2015-06-06 15:28:40.290 字体测试[1347:37490] family:‘Superclarendon2015-06-06 15:28:40.290 字体测试[1347:37490]    font:‘Superclarendon-Italic‘
2015-06-06 15:28:40.290 字体测试[1347:37490]    font:‘Superclarendon-Black‘
2015-06-06 15:28:40.290 字体测试[1347:37490]    font:‘Superclarendon-LightItalic‘
2015-06-06 15:28:40.290 字体测试[1347:37490]    font:‘Superclarendon-BlackItalic‘
2015-06-06 15:28:40.291 字体测试[1347:37490]    font:‘Superclarendon-BoldItalic‘
2015-06-06 15:28:40.291 字体测试[1347:37490]    font:‘Superclarendon-Light‘
2015-06-06 15:28:40.291 字体测试[1347:37490]    font:‘Superclarendon-Regular‘
2015-06-06 15:28:40.291 字体测试[1347:37490]    font:‘Superclarendon-Bold‘
2015-06-06 15:28:40.291 字体测试[1347:37490] -------------
2015-06-06 15:28:40.291 字体测试[1347:37490] family:‘Gujarati Sangam MN‘
2015-06-06 15:28:40.291 字体测试[1347:37490]    font:‘GujaratiSangamMN-Bold‘
2015-06-06 15:28:40.291 字体测试[1347:37490]    font:‘GujaratiSangamMN2015-06-06 15:28:40.291 字体测试[1347:37490] -------------
2015-06-06 15:28:40.291 字体测试[1347:37490] family:‘Damascus2015-06-06 15:28:40.291 字体测试[1347:37490]    font:‘DamascusLight2015-06-06 15:28:40.291 字体测试[1347:37490]    font:‘DamascusBold2015-06-06 15:28:40.292 字体测试[1347:37490]    font:‘DamascusSemiBold2015-06-06 15:28:40.292 字体测试[1347:37490]    font:‘DamascusMedium2015-06-06 15:28:40.292 字体测试[1347:37490]    font:‘Damascus2015-06-06 15:28:40.292 字体测试[1347:37490] -------------
2015-06-06 15:28:40.292 字体测试[1347:37490] family:‘Noteworthy2015-06-06 15:28:40.292 字体测试[1347:37490]    font:‘Noteworthy-Light‘
2015-06-06 15:28:40.292 字体测试[1347:37490]    font:‘Noteworthy-Bold‘
2015-06-06 15:28:40.292 字体测试[1347:37490] -------------
2015-06-06 15:28:40.292 字体测试[1347:37490] family:‘Geeza Pro‘
2015-06-06 15:28:40.292 字体测试[1347:37490]    font:‘GeezaPro2015-06-06 15:28:40.292 字体测试[1347:37490]    font:‘GeezaPro-Bold‘
2015-06-06 15:28:40.293 字体测试[1347:37490] -------------
2015-06-06 15:28:40.293 字体测试[1347:37490] family:‘Avenir2015-06-06 15:28:40.293 字体测试[1347:37490]    font:‘Avenir-Medium‘
2015-06-06 15:28:40.293 字体测试[1347:37490]    font:‘Avenir-HeavyOblique‘
2015-06-06 15:28:40.296 字体测试[1347:37490]    font:‘Avenir-Book‘
2015-06-06 15:28:40.296 字体测试[1347:37490]    font:‘Avenir-Light‘
2015-06-06 15:28:40.296 字体测试[1347:37490]    font:‘Avenir-Roman‘
2015-06-06 15:28:40.296 字体测试[1347:37490]    font:‘Avenir-BookOblique‘
2015-06-06 15:28:40.296 字体测试[1347:37490]    font:‘Avenir-Black‘
2015-06-06 15:28:40.296 字体测试[1347:37490]    font:‘Avenir-MediumOblique‘
2015-06-06 15:28:40.296 字体测试[1347:37490]    font:‘Avenir-BlackOblique‘
2015-06-06 15:28:40.296 字体测试[1347:37490]    font:‘Avenir-Heavy‘
2015-06-06 15:28:40.296 字体测试[1347:37490]    font:‘Avenir-LightOblique‘
2015-06-06 15:28:40.297 字体测试[1347:37490]    font:‘Avenir-Oblique‘
2015-06-06 15:28:40.297 字体测试[1347:37490] -------------
2015-06-06 15:28:40.297 字体测试[1347:37490] family:‘Academy Engraved LET‘
2015-06-06 15:28:40.297 字体测试[1347:37490]    font:‘AcademyEngravedLetPlain2015-06-06 15:28:40.297 字体测试[1347:37490] -------------
2015-06-06 15:28:40.297 字体测试[1347:37490] family:‘Mishafi2015-06-06 15:28:40.297 字体测试[1347:37490]    font:‘DiwanMishafi2015-06-06 15:28:40.297 字体测试[1347:37490] -------------
2015-06-06 15:28:40.297 字体测试[1347:37490] family:‘Futura2015-06-06 15:28:40.297 字体测试[1347:37490]    font:‘Futura-CondensedMedium‘
2015-06-06 15:28:40.297 字体测试[1347:37490]    font:‘Futura-CondensedExtraBold‘
2015-06-06 15:28:40.298 字体测试[1347:37490]    font:‘Futura-Medium‘
2015-06-06 15:28:40.298 字体测试[1347:37490]    font:‘Futura-MediumItalic‘
2015-06-06 15:28:40.298 字体测试[1347:37490] -------------
2015-06-06 15:28:40.298 字体测试[1347:37490] family:‘Farah2015-06-06 15:28:40.298 字体测试[1347:37490]    font:‘Farah2015-06-06 15:28:40.298 字体测试[1347:37490] -------------
2015-06-06 15:28:40.298 字体测试[1347:37490] family:‘Kannada Sangam MN‘
2015-06-06 15:28:40.298 字体测试[1347:37490]    font:‘KannadaSangamMN2015-06-06 15:28:40.298 字体测试[1347:37490]    font:‘KannadaSangamMN-Bold‘
2015-06-06 15:28:40.298 字体测试[1347:37490] -------------
2015-06-06 15:28:40.298 字体测试[1347:37490] family:‘Arial Hebrew‘
2015-06-06 15:28:40.298 字体测试[1347:37490]    font:‘ArialHebrew-Bold‘
2015-06-06 15:28:40.299 字体测试[1347:37490]    font:‘ArialHebrew-Light‘
2015-06-06 15:28:40.299 字体测试[1347:37490]    font:‘ArialHebrew2015-06-06 15:28:40.299 字体测试[1347:37490] -------------
2015-06-06 15:28:40.299 字体测试[1347:37490] family:‘Arial2015-06-06 15:28:40.299 字体测试[1347:37490]    font:‘ArialMT2015-06-06 15:28:40.299 字体测试[1347:37490]    font:‘Arial-BoldItalicMT‘
2015-06-06 15:28:40.299 字体测试[1347:37490]    font:‘Arial-BoldMT‘
2015-06-06 15:28:40.299 字体测试[1347:37490]    font:‘Arial-ItalicMT‘
2015-06-06 15:28:40.299 字体测试[1347:37490] -------------
2015-06-06 15:28:40.299 字体测试[1347:37490] family:‘Party LET‘
2015-06-06 15:28:40.299 字体测试[1347:37490]    font:‘PartyLetPlain2015-06-06 15:28:40.299 字体测试[1347:37490] -------------
2015-06-06 15:28:40.300 字体测试[1347:37490] family:‘???????-‘
2015-06-06 15:28:40.300 字体测试[1347:37490]    font:‘--‘
2015-06-06 15:28:40.300 字体测试[1347:37490] -------------
2015-06-06 15:28:40.300 字体测试[1347:37490] family:‘Chalkduster2015-06-06 15:28:40.300 字体测试[1347:37490]    font:‘Chalkduster2015-06-06 15:28:40.300 字体测试[1347:37490] -------------
2015-06-06 15:28:40.300 字体测试[1347:37490] family:‘Hiragino Kaku Gothic ProN‘
2015-06-06 15:28:40.300 字体测试[1347:37490]    font:‘HiraKakuProN-W6‘
2015-06-06 15:28:40.300 字体测试[1347:37490]    font:‘HiraKakuProN-W3‘
2015-06-06 15:28:40.300 字体测试[1347:37490] -------------
2015-06-06 15:28:40.300 字体测试[1347:37490] family:‘Hoefler Text‘
2015-06-06 15:28:40.301 字体测试[1347:37490]    font:‘HoeflerText-Italic‘
2015-06-06 15:28:40.301 字体测试[1347:37490]    font:‘HoeflerText-Regular‘
2015-06-06 15:28:40.301 字体测试[1347:37490]    font:‘HoeflerText-Black‘
2015-06-06 15:28:40.301 字体测试[1347:37490]    font:‘HoeflerText-BlackItalic‘
2015-06-06 15:28:40.301 字体测试[1347:37490] -------------
2015-06-06 15:28:40.301 字体测试[1347:37490] family:‘Optima2015-06-06 15:28:40.301 字体测试[1347:37490]    font:‘Optima-Regular‘
2015-06-06 15:28:40.301 字体测试[1347:37490]    font:‘Optima-ExtraBlack‘
2015-06-06 15:28:40.301 字体测试[1347:37490]    font:‘Optima-BoldItalic‘
2015-06-06 15:28:40.301 字体测试[1347:37490]    font:‘Optima-Italic‘
2015-06-06 15:28:40.301 字体测试[1347:37490]    font:‘Optima-Bold‘
2015-06-06 15:28:40.301 字体测试[1347:37490] -------------
2015-06-06 15:28:40.302 字体测试[1347:37490] family:‘Palatino2015-06-06 15:28:40.302 字体测试[1347:37490]    font:‘Palatino-Bold‘
2015-06-06 15:28:40.302 字体测试[1347:37490]    font:‘Palatino-Roman‘
2015-06-06 15:28:40.302 字体测试[1347:37490]    font:‘Palatino-BoldItalic‘
2015-06-06 15:28:40.302 字体测试[1347:37490]    font:‘Palatino-Italic‘
2015-06-06 15:28:40.302 字体测试[1347:37490] -------------
2015-06-06 15:28:40.302 字体测试[1347:37490] family:‘Malayalam Sangam MN‘
2015-06-06 15:28:40.302 字体测试[1347:37490]    font:‘MalayalamSangamMN-Bold‘
2015-06-06 15:28:40.302 字体测试[1347:37490]    font:‘MalayalamSangamMN2015-06-06 15:28:40.302 字体测试[1347:37490] -------------
2015-06-06 15:28:40.302 字体测试[1347:37490] family:‘Lao Sangam MN‘
2015-06-06 15:28:40.302 字体测试[1347:37490]    font:‘LaoSangamMN2015-06-06 15:28:40.303 字体测试[1347:37490] -------------
2015-06-06 15:28:40.303 字体测试[1347:37490] family:‘Al Nile‘
2015-06-06 15:28:40.303 字体测试[1347:37490]    font:‘AlNile-Bold‘
2015-06-06 15:28:40.303 字体测试[1347:37490]    font:‘AlNile2015-06-06 15:28:40.303 字体测试[1347:37490] -------------
2015-06-06 15:28:40.303 字体测试[1347:37490] family:‘Bradley Hand‘
2015-06-06 15:28:40.303 字体测试[1347:37490]    font:‘BradleyHandITCTT-Bold‘
2015-06-06 15:28:40.303 字体测试[1347:37490] -------------
2015-06-06 15:28:40.303 字体测试[1347:37490] family:‘Hiragino Mincho ProN‘
2015-06-06 15:28:40.303 字体测试[1347:37490]    font:‘HiraMinProN-W6‘
2015-06-06 15:28:40.303 字体测试[1347:37490]    font:‘HiraMinProN-W3‘
2015-06-06 15:28:40.304 字体测试[1347:37490] -------------
2015-06-06 15:28:40.304 字体测试[1347:37490] family:‘Trebuchet MS‘
2015-06-06 15:28:40.304 字体测试[1347:37490]    font:‘Trebuchet-BoldItalic‘
2015-06-06 15:28:40.304 字体测试[1347:37490]    font:‘TrebuchetMS2015-06-06 15:28:40.304 字体测试[1347:37490]    font:‘TrebuchetMS-Bold‘
2015-06-06 15:28:40.304 字体测试[1347:37490]    font:‘TrebuchetMS-Italic‘
2015-06-06 15:28:40.304 字体测试[1347:37490] -------------
2015-06-06 15:28:40.304 字体测试[1347:37490] family:‘Helvetica2015-06-06 15:28:40.304 字体测试[1347:37490]    font:‘Helvetica-Bold‘
2015-06-06 15:28:40.304 字体测试[1347:37490]    font:‘Helvetica2015-06-06 15:28:40.304 字体测试[1347:37490]    font:‘Helvetica-LightOblique‘
2015-06-06 15:28:40.304 字体测试[1347:37490]    font:‘Helvetica-Oblique‘
2015-06-06 15:28:40.305 字体测试[1347:37490]    font:‘Helvetica-BoldOblique‘
2015-06-06 15:28:40.305 字体测试[1347:37490]    font:‘Helvetica-Light‘
2015-06-06 15:28:40.305 字体测试[1347:37490] -------------
2015-06-06 15:28:40.305 字体测试[1347:37490] family:‘Courier2015-06-06 15:28:40.305 字体测试[1347:37490]    font:‘Courier-BoldOblique‘
2015-06-06 15:28:40.305 字体测试[1347:37490]    font:‘Courier2015-06-06 15:28:40.305 字体测试[1347:37490]    font:‘Courier-Bold‘
2015-06-06 15:28:40.305 字体测试[1347:37490]    font:‘Courier-Oblique‘
2015-06-06 15:28:40.305 字体测试[1347:37490] -------------
2015-06-06 15:28:40.305 字体测试[1347:37490] family:‘Cochin2015-06-06 15:28:40.305 字体测试[1347:37490]    font:‘Cochin-Bold‘
2015-06-06 15:28:40.305 字体测试[1347:37490]    font:‘Cochin2015-06-06 15:28:40.306 字体测试[1347:37490]    font:‘Cochin-Italic‘
2015-06-06 15:28:40.306 字体测试[1347:37490]    font:‘Cochin-BoldItalic‘
2015-06-06 15:28:40.306 字体测试[1347:37490] -------------
2015-06-06 15:28:40.306 字体测试[1347:37490] family:‘Devanagari Sangam MN‘
2015-06-06 15:28:40.306 字体测试[1347:37490]    font:‘DevanagariSangamMN2015-06-06 15:28:40.306 字体测试[1347:37490]    font:‘DevanagariSangamMN-Bold‘
2015-06-06 15:28:40.306 字体测试[1347:37490] -------------
2015-06-06 15:28:40.306 字体测试[1347:37490] family:‘Oriya Sangam MN‘
2015-06-06 15:28:40.306 字体测试[1347:37490]    font:‘OriyaSangamMN2015-06-06 15:28:40.306 字体测试[1347:37490]    font:‘OriyaSangamMN-Bold‘
2015-06-06 15:28:40.306 字体测试[1347:37490] -------------
2015-06-06 15:28:40.307 字体测试[1347:37490] family:‘Snell Roundhand‘
2015-06-06 15:28:40.307 字体测试[1347:37490]    font:‘SnellRoundhand-Bold‘
2015-06-06 15:28:40.307 字体测试[1347:37490]    font:‘SnellRoundhand2015-06-06 15:28:40.307 字体测试[1347:37490]    font:‘SnellRoundhand-Black‘
2015-06-06 15:28:40.307 字体测试[1347:37490] -------------
2015-06-06 15:28:40.307 字体测试[1347:37490] family:‘Zapf Dingbats‘
2015-06-06 15:28:40.307 字体测试[1347:37490]    font:‘ZapfDingbatsITC2015-06-06 15:28:40.307 字体测试[1347:37490] -------------
2015-06-06 15:28:40.307 字体测试[1347:37490] family:‘Bodoni 722015-06-06 15:28:40.307 字体测试[1347:37490]    font:‘BodoniSvtyTwoITCTT-Bold‘
2015-06-06 15:28:40.307 字体测试[1347:37490]    font:‘BodoniSvtyTwoITCTT-Book‘
2015-06-06 15:28:40.307 字体测试[1347:37490]    font:‘BodoniSvtyTwoITCTT-BookIta‘
2015-06-06 15:28:40.308 字体测试[1347:37490] -------------
2015-06-06 15:28:40.308 字体测试[1347:37490] family:‘Verdana2015-06-06 15:28:40.308 字体测试[1347:37490]    font:‘Verdana-Italic‘
2015-06-06 15:28:40.308 字体测试[1347:37490]    font:‘Verdana-BoldItalic‘
2015-06-06 15:28:40.308 字体测试[1347:37490]    font:‘Verdana2015-06-06 15:28:40.308 字体测试[1347:37490]    font:‘Verdana-Bold‘
2015-06-06 15:28:40.308 字体测试[1347:37490] -------------
2015-06-06 15:28:40.308 字体测试[1347:37490] family:‘American Typewriter‘
2015-06-06 15:28:40.308 字体测试[1347:37490]    font:‘AmericanTypewriter-CondensedLight‘
2015-06-06 15:28:40.308 字体测试[1347:37490]    font:‘AmericanTypewriter2015-06-06 15:28:40.308 字体测试[1347:37490]    font:‘AmericanTypewriter-CondensedBold‘
2015-06-06 15:28:40.308 字体测试[1347:37490]    font:‘AmericanTypewriter-Light‘
2015-06-06 15:28:40.309 字体测试[1347:37490]    font:‘AmericanTypewriter-Bold‘
2015-06-06 15:28:40.309 字体测试[1347:37490]    font:‘AmericanTypewriter-Condensed‘
2015-06-06 15:28:40.309 字体测试[1347:37490] -------------
2015-06-06 15:28:40.309 字体测试[1347:37490] family:‘Avenir Next2015-06-06 15:28:40.309 字体测试[1347:37490]    font:‘AvenirNext-UltraLight‘
2015-06-06 15:28:40.309 字体测试[1347:37490]    font:‘AvenirNext-UltraLightItalic‘
2015-06-06 15:28:40.309 字体测试[1347:37490]    font:‘AvenirNext-Bold‘
2015-06-06 15:28:40.309 字体测试[1347:37490]    font:‘AvenirNext-BoldItalic‘
2015-06-06 15:28:40.309 字体测试[1347:37490]    font:‘AvenirNext-DemiBold‘
2015-06-06 15:28:40.309 字体测试[1347:37490]    font:‘AvenirNext-DemiBoldItalic‘
2015-06-06 15:28:40.309 字体测试[1347:37490]    font:‘AvenirNext-Medium‘
2015-06-06 15:28:40.310 字体测试[1347:37490]    font:‘AvenirNext-HeavyItalic‘
2015-06-06 15:28:40.310 字体测试[1347:37490]    font:‘AvenirNext-Heavy‘
2015-06-06 15:28:40.310 字体测试[1347:37490]    font:‘AvenirNext-Italic‘
2015-06-06 15:28:40.310 字体测试[1347:37490]    font:‘AvenirNext-Regular‘
2015-06-06 15:28:40.310 字体测试[1347:37490]    font:‘AvenirNext-MediumItalic‘
2015-06-06 15:28:40.310 字体测试[1347:37490] -------------
2015-06-06 15:28:40.310 字体测试[1347:37490] family:‘Baskerville2015-06-06 15:28:40.310 字体测试[1347:37490]    font:‘Baskerville-Italic‘
2015-06-06 15:28:40.310 字体测试[1347:37490]    font:‘Baskerville-SemiBold‘
2015-06-06 15:28:40.310 字体测试[1347:37490]    font:‘Baskerville-BoldItalic‘
2015-06-06 15:28:40.310 字体测试[1347:37490]    font:‘Baskerville-SemiBoldItalic‘
2015-06-06 15:28:40.310 字体测试[1347:37490]    font:‘Baskerville-Bold‘
2015-06-06 15:28:40.311 字体测试[1347:37490]    font:‘Baskerville2015-06-06 15:28:40.311 字体测试[1347:37490] -------------
2015-06-06 15:28:40.311 字体测试[1347:37490] family:‘Khmer Sangam MN‘
2015-06-06 15:28:40.311 字体测试[1347:37490]    font:‘KhmerSangamMN2015-06-06 15:28:40.311 字体测试[1347:37490] -------------
2015-06-06 15:28:40.311 字体测试[1347:37490] family:‘Didot2015-06-06 15:28:40.311 字体测试[1347:37490]    font:‘Didot-Italic‘
2015-06-06 15:28:40.311 字体测试[1347:37490]    font:‘Didot-Bold‘
2015-06-06 15:28:40.311 字体测试[1347:37490]    font:‘Didot2015-06-06 15:28:40.311 字体测试[1347:37490] -------------
2015-06-06 15:28:40.311 字体测试[1347:37490] family:‘Savoye LET‘
2015-06-06 15:28:40.311 字体测试[1347:37490]    font:‘SavoyeLetPlain2015-06-06 15:28:40.312 字体测试[1347:37490] -------------
2015-06-06 15:28:40.312 字体测试[1347:37490] family:‘Bodoni Ornaments‘
2015-06-06 15:28:40.312 字体测试[1347:37490]    font:‘BodoniOrnamentsITCTT2015-06-06 15:28:40.312 字体测试[1347:37490] -------------
2015-06-06 15:28:40.312 字体测试[1347:37490] family:‘Symbol2015-06-06 15:28:40.312 字体测试[1347:37490]    font:‘Symbol2015-06-06 15:28:40.312 字体测试[1347:37490] -------------
2015-06-06 15:28:40.312 字体测试[1347:37490] family:‘Menlo2015-06-06 15:28:40.312 字体测试[1347:37490]    font:‘Menlo-Italic‘
2015-06-06 15:28:40.312 字体测试[1347:37490]    font:‘Menlo-Bold‘
2015-06-06 15:28:40.313 字体测试[1347:37490]    font:‘Menlo-Regular‘
2015-06-06 15:28:40.313 字体测试[1347:37490]    font:‘Menlo-BoldItalic‘
2015-06-06 15:28:40.313 字体测试[1347:37490] -------------
2015-06-06 15:28:40.313 字体测试[1347:37490] family:‘Bodoni 72 Smallcaps‘
2015-06-06 15:28:40.313 字体测试[1347:37490]    font:‘BodoniSvtyTwoSCITCTT-Book‘
2015-06-06 15:28:40.313 字体测试[1347:37490] -------------
2015-06-06 15:28:40.313 字体测试[1347:37490] family:‘DIN Alternate‘
2015-06-06 15:28:40.313 字体测试[1347:37490]    font:‘DINAlternate-Bold‘
2015-06-06 15:28:40.313 字体测试[1347:37490] -------------
2015-06-06 15:28:40.313 字体测试[1347:37490] family:‘Papyrus2015-06-06 15:28:40.314 字体测试[1347:37490]    font:‘Papyrus2015-06-06 15:28:40.314 字体测试[1347:37490]    font:‘Papyrus-Condensed‘
2015-06-06 15:28:40.314 字体测试[1347:37490] -------------
2015-06-06 15:28:40.314 字体测试[1347:37490] family:‘Euphemia UCAS‘
2015-06-06 15:28:40.314 字体测试[1347:37490]    font:‘EuphemiaUCAS-Italic‘
2015-06-06 15:28:40.314 字体测试[1347:37490]    font:‘EuphemiaUCAS2015-06-06 15:28:40.314 字体测试[1347:37490]    font:‘EuphemiaUCAS-Bold‘
2015-06-06 15:28:40.314 字体测试[1347:37490] -------------
2015-06-06 15:28:40.314 字体测试[1347:37490] family:‘Telugu Sangam MN‘
2015-06-06 15:28:40.314 字体测试[1347:37490]    font:‘TeluguSangamMN2015-06-06 15:28:40.314 字体测试[1347:37490]    font:‘TeluguSangamMN-Bold‘
2015-06-06 15:28:40.314 字体测试[1347:37490] -------------
2015-06-06 15:28:40.315 字体测试[1347:37490] family:‘Bangla Sangam MN‘
2015-06-06 15:28:40.315 字体测试[1347:37490]    font:‘BanglaSangamMN-Bold‘
2015-06-06 15:28:40.315 字体测试[1347:37490]    font:‘BanglaSangamMN2015-06-06 15:28:40.315 字体测试[1347:37490] -------------
2015-06-06 15:28:40.315 字体测试[1347:37490] family:‘Zapfino2015-06-06 15:28:40.315 字体测试[1347:37490]    font:‘Zapfino2015-06-06 15:28:40.315 字体测试[1347:37490] -------------
2015-06-06 15:28:40.315 字体测试[1347:37490] family:‘Bodoni 72 Oldstyle‘
2015-06-06 15:28:40.315 字体测试[1347:37490]    font:‘BodoniSvtyTwoOSITCTT-Book‘
2015-06-06 15:28:40.315 字体测试[1347:37490]    font:‘BodoniSvtyTwoOSITCTT-Bold‘
2015-06-06 15:28:40.315 字体测试[1347:37490]    font:‘BodoniSvtyTwoOSITCTT-BookIt‘
2015-06-06 15:28:40.315 字体测试[1347:37490] -------------
2015-06-06 15:28:40.316 字体测试[1347:37490] family:‘DIN Condensed‘
2015-06-06 15:28:40.316 字体测试[1347:37490]    font:‘DINCondensed-Bold‘
2015-06-06 15:28:40.316 字体测试[1347:37490] -------------

不知道其他同学能不能从这么一坨输出里找到他呢?反正我找到了,但是,看着那个输出,小汤表示,这肯定不对啊!
然后,小汤我又写了下面这段代码:

//这段代码用来获取字体的真正名称
NSURL *fontUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"abc" ofType:@"ttf"]];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fontUrl);
CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
CTFontManagerRegisterGraphicsFont(fontRef, NULL);
NSString *fontName = CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
NSLog(@"fontName: %@",fontName);
CGFontRelease(fontRef);

输出结果如下:

2015-06-06 15:32:22.674 字体测试[1381:38645] fontName: --

所以,想来大家都懂我为啥之前不信这就是字体名称了把?字体名称居然是 --,真坑啊…

重新执行这段代码:

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
[self.view addSubview:label];
label.text = @"Hello 汉字";
label.font = [UIFont fontWithName:@"--" size:40];

后的效果如下:
技术分享

OK,到这里,大家应该知道怎么用第三方的.ttf字体了吧?
把我上面那段//这段代码用来获取字体的真正名称的相应代码直接拷贝过去,然后把你们命名的资源文件名称传进去,就得到最后需要的字体名称喽~

iOS开发 - 使用自定义字体

标签:字体   自定义   ttf   ios   

原文地址:http://blog.csdn.net/u010982858/article/details/46388945

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!