【问题标题】:custom font with UILabel in navigation bar [duplicate]导航栏中带有UILabel的自定义字体[重复]
【发布时间】:2013-01-14 18:50:33
【问题描述】:

可能重复:
Custom font in iPhone

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

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

  1. 检查了我的应用的部署目标,设置为 iOS 5.1
  2. 将字体文件添加到项目资源中,并在构建阶段的“复制捆绑资源”中验证已添加到构建目标。
  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/

【问题讨论】:

    标签: iphone objective-c uilabel uinavigationitem uifont


    【解决方案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];
    

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

    Fonts provided by application
    

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

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

    还有一件事,

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

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

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

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

    【讨论】:

      【解决方案2】:

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

      +(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 中使用此名称:

      希望对你有帮助

      【讨论】:

      • 抱歉,我使用 loggAllFonts 找到了字体的确切名称,但标签仍然拒绝将字体应用于其文本。
      猜你喜欢
      • 2013-09-25
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多