简体   繁体   English

Xcode 6中的自定义字体

[英]Custom Font in Xcode 6

I have a custom font that I have imported into xcode that I want to use as a UIFont. 我有一个自定义字体,我已导入xcode,我想用作UIFont。 When I try to create a UIFont with the fonts name it doesn't work. 当我尝试使用字体名称创建UIFont时,它不起作用。 I've tried looking thru all of the fonts in each font family and I don't see the font. 我试过通过每个字体系列中的所有字体,我没有看到字体。 But when I go to my storyboard I am able to use the font. 但是当我去我的故事板时,我能够使用该字体。 Can anyone point me in the right direction? 谁能指出我正确的方向?

Heres the code for printing the fonts. 下面是打印字体的代码。 The font is called Nexa Light and Nexa Bold (Apple says the real name is the same without a space): 该字体称为Nexa Light和Nexa Bold(Apple表示真实名称相同,没有空格):

for family in UIFont.familyNames() as [String] {
    println("    ---" + family + "---")
    for font in UIFont.fontNamesForFamilyName(family) {
        println("        " + (font as String))
    }
}

Heres the output from all the fonts: 下面是所有字体的输出:

---Marion---
    Marion-Italic
    Marion-Bold
    Marion-Regular
---Copperplate---
    Copperplate-Light
    Copperplate
    Copperplate-Bold
---Heiti SC---
    STHeitiSC-Medium
    STHeitiSC-Light
---Iowan Old Style---
    IowanOldStyle-Italic
    IowanOldStyle-Roman
    IowanOldStyle-BoldItalic
    IowanOldStyle-Bold
---Courier New---
    CourierNewPS-BoldMT
    CourierNewPS-ItalicMT
    CourierNewPSMT
    CourierNewPS-BoldItalicMT
---Apple SD Gothic Neo---
    AppleSDGothicNeo-Bold
    AppleSDGothicNeo-Thin
    AppleSDGothicNeo-UltraLight
    AppleSDGothicNeo-Regular
    AppleSDGothicNeo-Light
    AppleSDGothicNeo-Medium
    AppleSDGothicNeo-SemiBold
---Heiti TC---
    STHeitiTC-Medium
    STHeitiTC-Light
---Gill Sans---
    GillSans-Italic
    GillSans-Bold
    GillSans-BoldItalic
    GillSans-LightItalic
    GillSans
    GillSans-Light
---Marker Felt---
    MarkerFelt-Thin
    MarkerFelt-Wide
---Thonburi---
    Thonburi
    Thonburi-Bold
    Thonburi-Light
---Avenir Next Condensed---
    AvenirNextCondensed-BoldItalic
    AvenirNextCondensed-Heavy
    AvenirNextCondensed-Medium
    AvenirNextCondensed-Regular
    AvenirNextCondensed-HeavyItalic
    AvenirNextCondensed-MediumItalic
    AvenirNextCondensed-Italic
    AvenirNextCondensed-UltraLightItalic
    AvenirNextCondensed-UltraLight
    AvenirNextCondensed-DemiBold
    AvenirNextCondensed-Bold
    AvenirNextCondensed-DemiBoldItalic
---Tamil Sangam MN---
    TamilSangamMN
    TamilSangamMN-Bold
---Helvetica Neue---
    HelveticaNeue-Italic
    HelveticaNeue-Bold
    HelveticaNeue-UltraLight
    HelveticaNeue-CondensedBlack
    HelveticaNeue-BoldItalic
    HelveticaNeue-CondensedBold
    HelveticaNeue-Medium
    HelveticaNeue-Light
    HelveticaNeue-Thin
    HelveticaNeue-ThinItalic
    HelveticaNeue-LightItalic
    HelveticaNeue-UltraLightItalic
    HelveticaNeue-MediumItalic
    HelveticaNeue
---Gurmukhi MN---
    GurmukhiMN-Bold
    GurmukhiMN
---Times New Roman---
    TimesNewRomanPSMT
    TimesNewRomanPS-BoldItalicMT
    TimesNewRomanPS-ItalicMT
    TimesNewRomanPS-BoldMT
---Georgia---
    Georgia-BoldItalic
    Georgia
    Georgia-Italic
    Georgia-Bold
---Apple Color Emoji---
    AppleColorEmoji
---Arial Rounded MT Bold---
    ArialRoundedMTBold
---Kailasa---
    Kailasa-Bold
    Kailasa
---Kohinoor Devanagari---
    KohinoorDevanagari-Light
    KohinoorDevanagari-Medium
    KohinoorDevanagari-Book
---Sinhala Sangam MN---
    SinhalaSangamMN-Bold
    SinhalaSangamMN
---Chalkboard SE---
    ChalkboardSE-Bold
    ChalkboardSE-Light
    ChalkboardSE-Regular
---Superclarendon---
    Superclarendon-Italic
    Superclarendon-Black
    Superclarendon-LightItalic
    Superclarendon-BlackItalic
    Superclarendon-BoldItalic
    Superclarendon-Light
    Superclarendon-Regular
    Superclarendon-Bold
---Gujarati Sangam MN---
    GujaratiSangamMN-Bold
    GujaratiSangamMN
---Damascus---
    DamascusLight
    DamascusBold
    DamascusSemiBold
    DamascusMedium
    Damascus
---Noteworthy---
    Noteworthy-Light
    Noteworthy-Bold
---Geeza Pro---
    GeezaPro
    GeezaPro-Bold
---Avenir---
    Avenir-Medium
    Avenir-HeavyOblique
    Avenir-Book
    Avenir-Light
    Avenir-Roman
    Avenir-BookOblique
    Avenir-Black
    Avenir-MediumOblique
    Avenir-BlackOblique
    Avenir-Heavy
    Avenir-LightOblique
    Avenir-Oblique
---Academy Engraved LET---
    AcademyEngravedLetPlain
---Mishafi---
    DiwanMishafi
---Futura---
    Futura-CondensedMedium
    Futura-CondensedExtraBold
    Futura-Medium
    Futura-MediumItalic
---Farah---
    Farah
---Kannada Sangam MN---
    KannadaSangamMN
    KannadaSangamMN-Bold
---Arial Hebrew---
    ArialHebrew-Bold
    ArialHebrew-Light
    ArialHebrew
---Arial---
    ArialMT
    Arial-BoldItalicMT
    Arial-BoldMT
    Arial-ItalicMT
---Party LET---
    PartyLetPlain
---Chalkduster---
    Chalkduster
---Hiragino Kaku Gothic ProN---
    HiraKakuProN-W6
    HiraKakuProN-W3
---Hoefler Text---
    HoeflerText-Italic
    HoeflerText-Regular
    HoeflerText-Black
    HoeflerText-BlackItalic
---Optima---
    Optima-Regular
    Optima-ExtraBlack
    Optima-BoldItalic
    Optima-Italic
    Optima-Bold
---Palatino---
    Palatino-Bold
    Palatino-Roman
    Palatino-BoldItalic
    Palatino-Italic
---Malayalam Sangam MN---
    MalayalamSangamMN-Bold
    MalayalamSangamMN
---Lao Sangam MN---
    LaoSangamMN
---Al Nile---
    AlNile-Bold
    AlNile
---Bradley Hand---
    BradleyHandITCTT-Bold
---Hiragino Mincho ProN---
    HiraMinProN-W6
    HiraMinProN-W3
---Trebuchet MS---
    Trebuchet-BoldItalic
    TrebuchetMS
    TrebuchetMS-Bold
    TrebuchetMS-Italic
---Helvetica---
    Helvetica-Bold
    Helvetica
    Helvetica-LightOblique
    Helvetica-Oblique
    Helvetica-BoldOblique
    Helvetica-Light
---Courier---
    Courier-BoldOblique
    Courier
    Courier-Bold
    Courier-Oblique
---Cochin---
    Cochin-Bold
    Cochin
    Cochin-Italic
    Cochin-BoldItalic
---Devanagari Sangam MN---
    DevanagariSangamMN
    DevanagariSangamMN-Bold
---Oriya Sangam MN---
    OriyaSangamMN
    OriyaSangamMN-Bold
---Snell Roundhand---
    SnellRoundhand-Bold
    SnellRoundhand
    SnellRoundhand-Black
---Zapf Dingbats---
    ZapfDingbatsITC
---Bodoni 72---
    BodoniSvtyTwoITCTT-Bold
    BodoniSvtyTwoITCTT-Book
    BodoniSvtyTwoITCTT-BookIta
---Verdana---
    Verdana-Italic
    Verdana-BoldItalic
    Verdana
    Verdana-Bold
---American Typewriter---
    AmericanTypewriter-CondensedLight
    AmericanTypewriter
    AmericanTypewriter-CondensedBold
    AmericanTypewriter-Light
    AmericanTypewriter-Bold
    AmericanTypewriter-Condensed
---Avenir Next---
    AvenirNext-UltraLight
    AvenirNext-UltraLightItalic
    AvenirNext-Bold
    AvenirNext-BoldItalic
    AvenirNext-DemiBold
    AvenirNext-DemiBoldItalic
    AvenirNext-Medium
    AvenirNext-HeavyItalic
    AvenirNext-Heavy
    AvenirNext-Italic
    AvenirNext-Regular
    AvenirNext-MediumItalic
---Baskerville---
    Baskerville-Italic
    Baskerville-SemiBold
    Baskerville-BoldItalic
    Baskerville-SemiBoldItalic
    Baskerville-Bold
    Baskerville
---Khmer Sangam MN---
    KhmerSangamMN
---Didot---
    Didot-Italic
    Didot-Bold
    Didot
---Savoye LET---
    SavoyeLetPlain
---Bodoni Ornaments---
    BodoniOrnamentsITCTT
---Symbol---
    Symbol
---Menlo---
    Menlo-Italic
    Menlo-Bold
    Menlo-Regular
    Menlo-BoldItalic
---Bodoni 72 Smallcaps---
    BodoniSvtyTwoSCITCTT-Book
---DIN Alternate---
    DINAlternate-Bold
---Papyrus---
    Papyrus
    Papyrus-Condensed
---Euphemia UCAS---
    EuphemiaUCAS-Italic
    EuphemiaUCAS
    EuphemiaUCAS-Bold
---Telugu Sangam MN---
    TeluguSangamMN
    TeluguSangamMN-Bold
---Bangla Sangam MN---
    BanglaSangamMN-Bold
    BanglaSangamMN
---Zapfino---
    Zapfino
---Bodoni 72 Oldstyle---
    BodoniSvtyTwoOSITCTT-Book
    BodoniSvtyTwoOSITCTT-Bold
    BodoniSvtyTwoOSITCTT-BookIt
---DIN Condensed---
    DINCondensed-Bold

Both fonts are added to the plist and added to the build phases. 两种字体都添加到plist中并添加到构建阶段。

Have you added the fonts to your Info.plist. 您是否已将字体添加到Info.plist中。 For example: 例如:

plist文件中的字体

So I removed the fonts from my project and reloaded them again and it worked! 所以我从我的项目中删除了字体并重新加载它们并且它工作了!

I'm not sure exactly what it was that made it work but I went thru all of the steps in the article . 我不确定它是什么让它起作用但我通过文章中的所有步骤。

You need to pull up the fonts, and add them into your info.plist. 您需要提取字体,并将它们添加到info.plist中。 In other words, download them into your project, then you'll be able to access them programmatically and I believe through the visual sidebar way too. 换句话说,将它们下载到您的项目中,然后您将能够以编程方式访问它们,我也相信通过可视侧边栏方式。

Add the font to your Info.plist. 将字体添加到Info.plist中。 Put it in Fonts provided by application . 把它放在应用程序提供的字体中

在此输入图像描述

Add your custom font in Target Project ==> Build Phases ==> Copy Bundle Resources 在Tar​​get Project中添加自定义字体==> Build Phases ==> Copy Bundle Resources 在此输入图像描述

Use the custom font in code: 在代码中使用自定义字体:

UILable *lblCostum = [[UILable alloc] initWithFrame:CGRectMake(295,60, 21, 20)];
lblCostum.font = [UIFont fontWithName:@"BirdsofParadise" size:12];

Thank you for your Code and helped me. 感谢您的代码并帮助了我。 The Code with Xcode 8 could be: 使用Xcode 8的代码可以是:

for family in UIFont.familyNames {
    print("    ---\(family)---")
    for font in UIFont.fontNames(forFamilyName: family) {
        print("       \(font)")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM