简体   繁体   English

无法在iOS应用中使用自定义字体

[英]Can't use custom font in iOS app

I'm trying to use custom font for my iOS app. 我正在尝试为我的iOS应用程序使用自定义字体。 Here is what I do 这就是我的工作

I added my custom font (m.tff) to Xcode Supporting files directory. 我将自定义字体(m.tff)添加到Xcode支持文件目录中。 Also I create entry called Fonts provided by application with value name of the font (m.tff) in myApp.plist 此外,我创建了一个名为Fonts的条目,由myApp.plist中的字体(m.tff)的值名称提供

In my view I have a label 在我看来,我有一个标签

UILabel *label = [[UILabel alloc]initWithFrame:CGRect(10, 20, 230, 30)];
label.font = [UIFont fontWithName:@"My custom font" size:15];

etc... 等等...

The problem is that when I launch the app the text in label is with default font instead of using the specific one. 问题是,当我启动应用程序时,标签中的文本是使用默认字体而不是使用特定字体。

What I miss here ? 我想念的是什么?

EDITED EDITED

Well it seem works now, but the font applies only for characters like , ? 好吧它现在似乎有效,但字体仅适用于像? but not for letters. 但不是为了信件。 I'm using cyrillic btw. 我正在使用西里尔文btw。

Use this to get all the fonts available 使用此选项可获取所有可用字体

  // List all fonts on iPhone
  NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
  for(NSString *familyName in familyNames)
  {
    NSLog(@"family: %@", familyName);

    NSArray *fontNames = [[NSArray alloc] initWithArray:[UIFont fontNamesForFamilyName:familyName]];
    for(NSString *fontName in fontNames)
    {
      NSLog(@"    font: %@", fontName);
    }
  }

Look for the font you embedded and see if it is the name is the same. 查找嵌入的字体,看看它是否相同。

Font extension should be ttf instead of tiff 字体扩展名应为ttf而不是tiff

and then make an entry in plist 然后在plist中输入一个条目

Fonts provided by application Font.ttf 应用程序Font.ttf提供的字体

检查您的[UIFont fontWithName:@"My font" size:15]行是否返回非空的UIFont对象。

In your Info.plist file, add the key: Fonts provided by application with an array object, and set the items of the array to strings as follows: 在Info.plist文件中,添加密钥: Fonts provided by application使用数组对象Fonts provided by application ,并将数组项设置为字符串,如下所示:

MyFont.ttf

Where MyFont is the EXACT name as listed for the font in your Mac's app, "Font Book." 其中MyFont是Mac应用程序中字体列出的MyFont名称,“Font Book”。 Use Spotlight to launch font book and then find your font file in there to double check the name. 使用Spotlight启动字体书,然后在那里找到您的字体文件以仔细检查名称。

Then when you set the font to your label: 然后,当您将字体设置为标签时:

myLabel.font = [UIFont fontWithName: @"MyFont" size: someFloatValue];
  1. Make sure your fonts are in info.plist (Fonts provided by application) 确保您的字体在info.plist中(应用程序提供的字体)
  2. Select yor target, in the build phases tab, Copy Bundle Resources, add your fonts. 选择yor target,在构建阶段选项卡中,Copy Bundle Resources,添加字体。

m.tiff听起来不像支持的字体文件,即TrueType或OpenType。

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

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