我想有一个应用程序包括一个自定义字体渲染文本,加载它,然后使用它与标准UIKit元素如UILabel。这可能吗?


当前回答

如果你使用的是xcode 4.3,你必须将字体添加到拷贝Bundle资源下的构建阶段,根据线程https://stackoverflow.com/users/1292829/arne,自定义字体xcode 4.3。这对我来说很有效,下面是我在应用程序中使用自定义字体的步骤:

Add the font to your project. I dragged and dropped the OTF (or TTF) files to a new group I created and accepted xcode's choice of copying the files over to the project folder. Create the UIAppFonts array with your fonts listed as items within the array. Just the names, not the extension (e.g. "GothamBold", "GothamBold-Italic"). Click on the project name way at the top of the Project Navigator on the left side of the screen. Click on the Build Phases tab that appears in the main area of xcode. Expand the "Copy Bundle Resources" section and click on "+" to add the font. Select the font file from the file navigator that pops open when you click on the "+". Do this for every font you have to add to the project.

其他回答

是的,你可以在你的应用程序中使用自定义字体

按照下面的步骤:

将自定义字体文件添加到项目的支持文件中

为你的信息添加一个键。名为UIAppFonts的plist文件。

将该键设置为数组

对于你拥有的每种字体,输入字体文件的全名(包括扩展名)作为UIAppFonts数组的项

保存信息。plist现在在你的应用程序中,你可以简单地调用[UIFont fontWithName:@"你的自定义字体名称"大小:20]来获得与你的UILabels使用的自定义字体 应用后,如果你没有得到正确的字体,然后双击自定义字体,仔细看顶部字体名称是来的,复制这个字体,粘贴,在这里[UIFont fontWithName:@"这里过去你的自定义字体名称"大小:20]

我希望你能得到正确答案

正如之前所有的答案所表明的,这是非常有可能的,而且在更新的iOS版本中非常容易。

我知道这不是一个技术上的答案,但因为很多人都做错了(这实际上违反了许可,如果你被起诉,可能会花费你很多钱),让我在这里强调一个警告:在iOS(或任何其他类型)应用程序中嵌入自定义字体基本上是重新分配字体。大多数商业字体的许可都禁止再分发,所以请确保您按照许可行事。

我建议大家参考我最喜欢的一个简短教程:http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/,上面的信息都来自这个网站。

第1步-将.ttf或.otf从Finder拖到项目中

注意-请确保在主应用程序目标上单击“添加到目标”框

如果你忘记单击将其添加到目标,然后单击项目层次结构中的字体文件,在右侧面板中单击目标会员部分中的主应用程序目标

要确保字体是应用目标的一部分,请确保它们出现在构建阶段的Copy Bundle Resources中

步骤2 -添加字体文件名到你的Plist

去自定义iOS目标属性在你的信息部分,并添加一个关键字,在该部分的项目,称为字体提供的应用程序(你应该看到它作为一个选项,当你输入它,它将自己设置为一个数组。点击小箭头打开数组项,输入你添加的.ttf或.otf文件的名称,让你的应用程序知道这些是你想要可用的字体文件

注意-如果你的应用程序在这一步之后崩溃,那么检查你在这里添加的项目的拼写

步骤3 -找出字体的名称,这样你就可以调用它们

通常情况下,应用程序看到的字体名称与基于该字体的文件名所认为的不同,将其放入代码中,并查看应用程序所做的日志,以查看在代码中调用的字体名称

斯威夫特

for family: String in UIFont.familyNames(){
  print("\(family)")
  for names: String in UIFont.fontNamesForFamilyName(family){
      print("== \(names)")
  }
}

Objective - C

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

你的日志应该是这样的:

步骤4 -使用步骤3中的名称使用新的自定义字体

斯威夫特

 label.font = UIFont(name: "SourceSansPro-Regular", size: 18)

Objective - C

 label.font = [UIFont fontWithName:@"SourceSansPro-Regular" size:18];

如果你使用的是xcode 4.3,你必须将字体添加到拷贝Bundle资源下的构建阶段,根据线程https://stackoverflow.com/users/1292829/arne,自定义字体xcode 4.3。这对我来说很有效,下面是我在应用程序中使用自定义字体的步骤:

Add the font to your project. I dragged and dropped the OTF (or TTF) files to a new group I created and accepted xcode's choice of copying the files over to the project folder. Create the UIAppFonts array with your fonts listed as items within the array. Just the names, not the extension (e.g. "GothamBold", "GothamBold-Italic"). Click on the project name way at the top of the Project Navigator on the left side of the screen. Click on the Build Phases tab that appears in the main area of xcode. Expand the "Copy Bundle Resources" section and click on "+" to add the font. Select the font file from the file navigator that pops open when you click on the "+". Do this for every font you have to add to the project.

在查找器和“获取信息”中找到TTF。在“全名:”的标题下,它给了我一个名字,然后我用fontWithName(我只是复制和粘贴了确切的名字,在这种情况下,没有'.ttf'扩展是必要的)。