如何通过 Xcode 以编程方式在 iOS 中使用自定义字体(例如:Helvetica CY.ttf)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26667951/
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-08-31 03:26:47  来源:igfitidea点击:

How to use custom font in iOS ( Eg: Helvetica CY.ttf ) with Xcode programatically

iosobjective-cfontsxcode6true-type-fonts

提问by Deepak Chandra

  • I am trying to use Helvetica CYfont in my app. I have configured it as recommended by apple docs.
  • The custom font shows up in dropdown in Storyboard, but unable to use the same font programaticallyin my class file.
  • I have logged all font families available for the app, but "Helvetica CY" is not printed on console log.
  • 我正在尝试Helvetica CY在我的应用程序中使用字体。我已经按照苹果文档的推荐配置了它。
  • 自定义字体显示在 Storyboard 的下拉列表中,但无法在我的类文件中以编程方式使用相同的字体
  • 我已经记录了该应用程序可用的所有字体系列,但Helvetica CY控制台日志上没有打印“ ”。

Any help will be appreciated.

任何帮助将不胜感激。

Thanks!

谢谢!

回答by Rajesh Loganathan

Its simple only. Its working for me in xcode6.1 too.

它只是简单。它也适用于 xcode6.1。

Try using this steps :

尝试使用以下步骤:

Step 1:Include your fonts in your XCode project

第 1 步:在 XCode 项目中包含您的字体

Step 2:Make sure that they're included in the target

第 2 步:确保它们包含在目标中

Step 3:Double check that your fonts are included as Resources in your bundle

第 3 步:仔细检查您的字体是否作为资源包含在您的包中

Step 4:Include your iOS custom fonts in your application plist

第 4 步:在您的应用程序 plist 中包含您的 iOS 自定义字体

Step 5:Find the name of the font

第 5 步:找到字体的名称

for (NSString* family in [UIFont familyNames])
{
    NSLog(@"%@", family);

    for (NSString* name in [UIFont fontNamesForFamilyName: family])
    {
        NSLog(@"  %@", name);
    }
}

Step 6:Use UIFont and specify the name of the font

第六步:使用UIFont并指定字体名称

label.font = [UIFont fontWithName:@"Helvetica CY" size:20];

回答by christijk

Apple provides a set of fonts to customize our application and also allow us to add custom fonts.

Apple 提供了一组字体来自定义我们的应用程序,还允许我们添加自定义字体。

Steps

脚步

  1. Copy the font files(.ttf or .otf) to main bundle.

  2. Add the file names into info.plist as a new array named "Fonts provided by application".

  3. It is very important to add the post script name of fonts into this array. That means each font have the font name(with extension) and its postscript name in info.plist.

  4. To find the Post script name,just open the Fontbook application in mac and then add the font file. After adding the font file into font book just look at the information tab of that font,there we can find the Post script name.

  5. Using is pretty simple, use the post script name of the font,

    nameLabel.font = [UIFont fontWithName:@"HelveticaCY-Plain" size:22];
    
  1. 将字体文件(.ttf 或 .otf)复制到主包。

  2. 将文件名作为名为“应用程序提供的字体”的新数组添加到 info.plist 中。

  3. 将字体的后期脚本名称添加到此数组中非常重要。这意味着每个字体在 info.plist 中都有字体名称(带扩展名)和它的 postscript 名称。

  4. 要找到 Post 脚本名称,只需在 mac 中打开 Fontbook 应用程序,然后添加字体文件。将字体文件添加到字体簿后,只需查看该字体的信息选项卡,我们就可以找到Post脚本名称。

  5. 使用很简单,使用字体的post脚本名称,

    nameLabel.font = [UIFont fontWithName:@"HelveticaCY-Plain" size:22];
    

enter image description here

在此处输入图片说明

回答by Markus

I made the same mistake. The key is the answer above:

我犯了同样的错误。关键是上面的答案:

for (NSString* family in [UIFont familyNames]) {
   NSLog(@"%@", family);

   for (NSString* name in [UIFont fontNamesForFamilyName: family]) {
       NSLog(@"  %@", name);
   }
}

I included in my project folder the GOTHIC.TTF file.

我在我的项目文件夹中包含了 GOTHIC.TTF 文件。

And in my Info.plist I included GOTHIC.TTF in "Fonts provided by application".

在我的 Info.plist 中,我在“应用程序提供的字体”中包含了 GOTHIC.TTF。

But surprise! The font it is not called "GOTHIC", but "CenturyGothic".

但惊喜!它的字体不叫“GOTHIC”,而是“CenturyGothic”。

[cell.textLabel setFont:[UIFont fontWithName:@"CenturyGothic" size:24]];

回答by Niharika

just go throw this link and follow steps. it is having all steps http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

只需抛出此链接并按照步骤操作即可。它具有所有步骤 http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

回答by Mohit Padalia

In case someone is interested in Swift 3 code for getting fonts:

如果有人对用于获取字体的 Swift 3 代码感兴趣:

for family in UIFont.familyNames
{
    print("Family: \(family)")

    for name in UIFont.fontNames(forFamilyName: family)
    {
        print("Name: \(name)")
    }
}