Xcode 6 中的自定义字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25942326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:37:45  来源:igfitidea点击:

Custom Font in Xcode 6

iosxcodeswiftuifont

提问by GnarGnar

I have a custom font that I have imported into xcode that I want to use as a UIFont. When I try to create a UIFont with the fonts name it doesn't work. 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?

我有一个自定义字体,我已经导入到 xcode 中,我想将其用作 UIFont。当我尝试使用字体名称创建 UIFont 时,它不起作用。我试过查看每个字体系列中的所有字体,但没有看到字体。但是当我转到我的故事板时,我可以使用该字体。任何人都可以指出我正确的方向吗?

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(苹果说真名一样,没有空格):

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 并添加到构建阶段。

回答by Austen Chongpison

Have you added the fonts to your Info.plist. For example:

您是否将字体添加到您的 Info.plist。例如:

Fonts in plist file

plist 文件中的字体

回答by GnarGnar

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.

我不确定究竟是什么让它起作用,但我完成了文章中的所有步骤。

回答by Andy Lebowitz

You need to pull up the fonts, and add them into your 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.

您需要调出字体,并将它们添加到您的 info.plist 中。换句话说,将它们下载到您的项目中,然后您就可以以编程方式访问它们,我相信也可以通过可视化侧边栏的方式来访问它们。

回答by Baraiya Bhadresh

Add the font to your Info.plist. Put it in Fonts provided by application.

将字体添加到您的 Info.plist。把它放在应用程序提供的字体中

enter image description here

在此处输入图片说明

Add your custom font in Target Project ==> Build Phases ==> Copy Bundle Resources enter image description here

在 Target 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];

回答by F. Odenbreit

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)")
    }
}