0

可能重复:
iPhone 中的自定义字体

我正在尝试创建一个自定义标签以将其设置为titleViewmy的一个navigationItem,但我无法将标签字体设置为我的项目中包含的自定义字体。

我有一个名为“BADER_AL_GORDABIA-2_0.ttf”的自定义字体文件,FontBook 中显示的真实字体名称是“bader_al gordabia-2”我已按照以下步骤操作:

  1. 检查了我的应用程序的部署目标,它设置为 iOS 5.1
  2. 将字体文件添加到项目资源中,并在 Build Phases 的“Copy Bundle Resources”中验证是否已添加到构建目标中。
  3. 在我的 info.plist 文件中添加了一个新键作为“应用程序提供的字体”,并在其中添加了“BADER_AL_GORDABIA-2_0.ttf”。
  4. 在 fontWithName 方法中使用了真正的字体名称“bader_al gordabia-2”。

我能够使用加载字体

UIFont * customFont = [UIFont fontWithName:@"bader_al gordabia-2" size:18.0f];

并且我确保 customFont 不为空(通过打印其描述),但问题是我无法将此自定义字体应用于 UILabel。

我的代码是:

UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,10,200,24)];
    titleLabel.backgroundColor = [UIColor clearColor];
    UIFont * customFont = [UIFont fontWithName:@"bader_al gordabia-2" size:18.0f];
    [titleLabel setFont:customFont];
    titleLabel.text = @"أخبار الجمعية";
    //NSLog(@"%@", [customFont description]);

    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];

    [self.navigationItem setTitleView:titleLabel]; 

我试图搜索堆栈溢出,但找不到合适的答案。我还尝试使用以下链接中指定的自定义标签: http ://refactr.com/blog/2012/09/ios-tips-custom-fonts/

4

2 回答 2

1

我就是这样做的

UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 100)];

navLabel.backgroundColor = [UIColor clearColor];
navLabel.text = @"Set Up";
navLabel.textColor = [UIColor whiteColor];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:1.0];
navLabel.shadowOffset = CGSizeMake(0, -2);
navLabel.font = [UIFont fontWithName:@"Questrial" size:28.0];
navLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navLabel;
[navLabel release];

只有上面的代码不起作用......所以你需要去你的app plist文件并添加一个新行并将这个键添加到行中,

Fonts provided by application

并将字体名称添加到 item0 的值中,

根据我的代码,“Questrial-Regular.ttf”

还有一件事,

那就是您可能不正确知道字体名称。所以你需要找到正确的字体名称。将此代码添加到您的 ViewdidLoad 中,它会将姓氏记录到终端。

NSLog(@"%@",[UIFont familyNames]);

您可以根据您的字体找到正确的字体名称。

按照这个步骤,它对我有用,我认为它也会帮助你。

于 2013-01-31T09:25:47.810 回答
0

这是您应该做的 - 首先在任何地方添加并调用以下方法:

+(void)logAllFonts{
    NSArray *fontFamilies = [UIFont familyNames];

    for (int i = 0; i < [fontFamilies count]; i++)
    {
      NSString *fontFamily = [fontFamilies objectAtIndex:i];
      NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
      NSLog(@"%@: %@", fontFamily, fontNames);
    }
 }

这将记录您项目中存在的所有字体名称,包括您在项目和 plist 中添加的外部字体。

现在您在控制台中浏览该列表并找到您添加的字体的确切字体名称 (bader_al gordabia-2)。在 fontWithName 中使用这个名称:

希望这可以帮助

于 2013-01-30T20:31:42.723 回答