【问题标题】:Can't change custom UIFont size on the UIButton无法更改 UIButton 上的自定义 UIFont 大小
【发布时间】:2020-04-22 05:27:50
【问题描述】:

我有一个按钮,我使用自定义 UIFont 来显示文本。字体正确加载并正确应用于按钮标题。我的问题是我似乎无法更改字体大小:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(loginButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Login" forState:UIControlStateNormal];
button.frame = CGRectMake(63.0, 200.0, 194.0, 60.0);
button.titleLabel.font = [UIFont fontWithName:@"My-Font" size:8.0f];
[self.view addSubview:button];

无论我输入什么字体大小,我都会得到一些默认大小。另一方面,如果我这样做:

button.titleLabel.font = [UIFont systemFontOfSize:32.0f];

我得到了 32 号字体,但是,当然,我没有得到我的自定义字体。 那么,如何设置自定义字体的大小呢?

(我使用 Xcode 4.6.3)

【问题讨论】:

  • 我猜fontWithName中的“MyFont”为空...简单快速的测试:UIFont *myFont = [UIFont fontWithName:@"MyFont" size:8.0f]; NSLog(@"MyFont: %@", myFont);
  • @Larme 是的,它是......嗯,为什么会这样?我可以在 Bundle Phase -> Copy Bundle Resources 下看到它。另外,标签上的字体看起来也发生了变化
  • 确保您的字体名称正确。字体名称并不总是字体的文件名。
  • @HAS 我仔细检查了一下(从 Finder -> 字体 -> 'space' [用于预览]),它与我正在寻找的匹配
  • @logixologist 怀疑... OS 3.2 及更高版本支持此功能。直接来自 iPhone OS 3.2 中的新增功能文档:自定义字体支持 想要使用自定义字体的应用程序现在可以在其应用程序包中包含这些字体,并通过在其 Info.plist 文件中包含 UIAppFonts 键向系统注册这些字体。该键的值是一个字符串数组,用于标识应用程序包中的字体文件。当系统看到该键时,它会加载指定的字体并使它们可供应用程序使用。

标签: ios uibutton uifont


【解决方案1】:

似乎您的 UIFont 对象没有正确创建并返回 nil

这是我找到的关于自定义字体的最佳教程。祝你好运;)

http://refactr.com/blog/2012/09/ios-tips-custom-fonts/ (Archived Version)

【讨论】:

    【解决方案2】:

    要获取您必须传入fontWithName:size: 的字体名称,请使用Font Book 应用打开您的字体,然后查找PostScript name 属性。确保您也已将其添加到 Info.plist 文件中。

    【讨论】:

    • 以上所有操作完成,字体始终出现,并在 FontBook 中检查...我认为我的问题实际上是字体类型不正确(网络字体与桌面字体)
    【解决方案3】:

    这样做:

        NSMutableDictionary *fontsByFamilyName = [NSMutableDictionary dictionary];
        NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES] autorelease]];
    
        NSArray *familyNames = [[UIFont familyNames] sortedArrayUsingDescriptors:sortDescriptors];
        for (NSString *aFamilyName in familyNames) {
            NSArray *fontNames = [[UIFont fontNamesForFamilyName:aFamilyName] sortedArrayUsingDescriptors:sortDescriptors];
    
            [fontsByFamilyName setObject:fontNames forKey:aFamilyName];
            NSLog(@"fonts :%@", fontsByFamilyName);           
        }
    

    查找字体的“iOS”名称...我用这种方法找到了我的自定义字体。如果你不这样做,让我们检查一下你是如何添加字体的(可能是忘记了一个动作)。 来源:http://forgecode.net/2010/08/uifont-name-grabber/

    【讨论】:

    • 是的,我没有在列表中看到我的字体
    • 好的。您在 Build Phases/Copy Bundle Ressources 和 MyAppl-Info.plist 文件中的应用程序提供的字体中的字体也是如此。
    • 您是否尝试重新安装字体?就像@Matt Mc 给出的链接一样?
    • 我认为我拥有的字体文件实际上是错误的......我需要阅读网络字体与桌面字体
    【解决方案4】:

    有趣的是,它竟然是我的 Mac。当我让我的朋友安装它们时,他没有任何问题。 我的 Mac 是旧的,所以也许这就是原因。以下是规格:

    **Mac:** MacBook Pro 15" (Late 2008)
    **Processor:**  2.4 GHz Intel Core 2 Duo
    **Memory:** 4 GB 1067 MHz DDR3
    **Graphics:** NVIDIA GeForce 9600M GT 256 MB
    

    【讨论】:

      【解决方案5】:

      为 'My-Font' 传入 'nil' 可以为默认字体调整文本大小 - 无需知道实际使用的字体是什么:

      button.titleLabel.font = [UIFont fontWithName:nil size:10.0f];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-11
        • 2016-05-15
        • 1970-01-01
        • 1970-01-01
        • 2018-06-06
        • 2015-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多