简体   繁体   English

未在UILabel-Swift中设置UIFont

[英]UIFont not being set in UILabel- Swift

I have a UILabel and want the font of the text to be SF Mono. 我有一个UILabel,并且希望文本的字体为SF Mono。 So, I use these lines of code: 因此,我使用以下代码行:

let tagBoxLabel = UILabel(frame: CGRect(x: 20, y: 0, width: 0, height: 0))
tagBoxLabel.font = UIFont(name: "SF Mono Regular", size: 10.0)

But the text is not set. 但是文本没有设置。 I have a tried "SFMono-Regular", "SFMono-Regular.otf", etc. Nothing. 我尝试了“ SFMono-Regular”,“ SFMono-Regular.otf”等。什么都没有。

I have correctly imported the .OTF into my project as well, as I can access it on the storyboard. 我也已将.OTF正确导入到我的项目中,因为可以在情节提要中访问它。

Does anyone have any clue of what I'm doing wrong? 有人知道我在做什么错吗? Any help would be immensely appreciated. 任何帮助将不胜感激。 Thanks so much in advance! 非常感谢!

For me, the problem was that the font was not entered properly in Xcode. 对我来说,问题是字体未在Xcode中正确输入。

First, drag and drop your font file into the Xcode project IN FINDER 首先,将您的字体文件拖放到Xcode项目中

Second, drag and drop it into Xcode into the project navigator and click "Copy items if needed" and assign the target membership to your target. 其次,将其拖放到Xcode的项目导航器中,然后单击“如果需要,复制项目”,然后将目标成员资格分配给目标。 This is super important 这是非常重要的

Third, go to your info.plist and enter your font into there using "Fonts provided by application" This is what did it for me. 第三,转到您的info.plist并使用“应用程序提供的字体”在其中输入您的字体。

Fourth, make sure you're using the correct name. 第四,确保使用正确的名称。 It might not be what the file name is. 文件名可能不是。

Hope anyone who finds this is helped by it! 希望任何发现此问题的人都能得到帮助!

Most likely you are not using the correct font name. 您很可能没有使用正确的字体名称。 Try this: 尝试这个:

let fontFamilyNames = UIFont.familyNames
for familyName in fontFamilyNames {
print("------------------------------")
print("Font Family Name = [\(familyName)]")
let names = UIFont.fontNames(forFamilyName: familyName)
print("Font Names = [\(names)]")
}

Can you try getting the font name from there and implement it and run it again? 您可以尝试从那里获取字体名称并实现它,然后再次运行它吗? I got this code from http://giordanoscalzo.tumblr.com/post/95900320382/print-all-ios-fonts-in-swift 我从http://giordanoscalzo.tumblr.com/post/95900320382/print-all-ios-fonts-in-swift中获得了此代码

Second: 第二:

    UIFont.familyNames.forEach({ familyName in
        let fontNames = UIFont.fontNames(forFamilyName: familyName)
        print(familyName, fontNames)
    })

I've been using this guide to add fonts in my apps and its the best thing. 我一直在使用本指南在应用程序中添加字体,这是最好的选择。 Do make sure you check for Copy Bundle Resources section in your Build Phases . 请确保在构建阶段中检查“ 副本捆绑资源”部分。

Follow this link and get the right way of adding your fonts to the app. 单击此链接,以正确的方式将字体添加到应用程序。

Go to your project Build Phases pane by highlighting the XCode project file in your solution explorer and on the right hand side, select “Build Phases”. 通过在解决方案资源管理器中突出显示XCode项目文件,转到项目的“构建阶段”窗格,然后在右侧选择“构建阶段”。 You'll see that one of the sections you can expand is “Copy Bundle Resources”. 您会看到可以扩展的部分之一是“复制捆绑资源”。 Open that list and make sure that your fonts are included in that list. 打开该列表,并确保您的字体包含在该列表中。

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

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