【问题标题】:How do I create a bold UIFont from a regular UIFont?如何从常规 UIFont 创建粗体 UIFont?
【发布时间】:2013-04-07 14:39:39
【问题描述】:

如果我有一个UIFont 对象,是否可以将其转换为粗体?我不知道字体名称,我只有一个 UIFont 对象。我想要的是这样的功能

UIFont *boldFontFromFont(UIFont *input)
{
    return [input derivedFontWithFontWeight:UIFontWeightBold];
}

如何更改代码以使其正常工作。 (上面的代码不起作用,我只是为了说明这一点而编造的。)

提前致谢。

【问题讨论】:

  • @BB9z:真的吗?这个问题来自 2013 年。我们是否应该在 5 年后现在将其作为重复项关闭,因为其他人再次提出了类似的问题?
  • 我的错误。应该关闭新的。
  • @BB9z:或者,我们让这两个问题都存在? UIFont.systemFont(ofSize:weight:) 方法在 2013 年还不存在。技术一直在变化。我的问题是“生活在 2013 年,如何从常规 UIFont 在 Objective C 中创建一个粗体 UIFont?” - 另一个问题是“Living in 2018, How do I Set a specific Font Weight for UILabel in Swift” - 如果你这样表述,就不一样了。
  • @BB9z:另外,从字体名称和字体粗细创建具有特定粗细的字体对象与想要创建与现有字体对象具有不同粗细的字体是不同的。当问题过于急切地重复关闭时,这让我很困扰,只是因为它们有 90% 相似。它对任何人都没有帮助。

标签: ios ios5 uikit


【解决方案1】:

iOS 7 引入了一个新的UIFontDescriptor 类,这让它变得更加容易:

UIFont *font = [UIFont fontWithName:@"Helvetica Neue" size:12];
NSLog(@"plain font: %@", font.fontName); // “HelveticaNeue”

UIFont *boldFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold] size:font.pointSize];
NSLog(@"bold version: %@", boldFont.fontName); // “HelveticaNeue-Bold”

UIFont *italicFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic] size:font.pointSize];
NSLog(@"italic version: %@", italicFont.fontName); // “HelveticaNeue-Italic”

UIFont *boldItalicFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic] size:font.pointSize];
NSLog(@"bold & italic version: %@", boldItalicFont.fontName); // “HelveticaNeue-BoldItalic”

对于来这里寻找 Cocoa (macOS) 等价物的人,UIFontDescriptor 来自 NSFontDescriptor,自 10.3 起可用。

【讨论】:

    【解决方案2】:

    如果您正在寻找 swift 实施

    let normalFont = UIFont(name: "FONT_NAME", size: CGFloat(20))!
    let boldFont = UIFont(descriptor: normalFont.fontDescriptor.withSymbolicTraits(.traitBold)!, size: normalFont.pointSize)
    

    希望这会有所帮助!干杯!

    【讨论】:

      【解决方案3】:

      要获得粗体字体,您需要从字体系列中传递特定的字体名称。您可以从给定字体中获取字体系列名称,然后列出该系列中的所有字体。通常,粗体字体的名称中会包含“粗体”,但格式并不严格,并且可能存在诸如“Helvetica-BoldOblique”之类的变体。你可以从这段代码开始:

      - (UIFont *)boldFontFromFont:(UIFont *)font
      {
          NSString *familyName = [font familyName];
          NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
          for (NSString *fontName in fontNames)
          {
              if ([fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location != NSNotFound)
              {
                  UIFont *boldFont = [UIFont fontWithName:fontName size:font.pointSize];
                  return boldFont;
              }
          }
          return nil;
      }
      

      【讨论】:

      • hmm,有一个问题:对于 systemFont,你有 familyName == ".Helvetica NeueUI" 并且 fontNames 数组是一个空数组:-/
      • @Michael 我试过UIFont *boldFont = [self boldFontFromFont:[UIFont systemFontOfSize:12.0]];。 NSLog 输出为:<UICFFont: 0x758b320> font-family: "Helvetica-BoldOblique"; font-weight: bold; font-style: italic; font-size: 12px
      • 好的,你用的是ios6吗?因为我使用的是 ios 5。也许 ios6 中的字体系统发生了变化?
      • 我在 iOS5 和 iOS6 模拟器上得到了相同的结果。
      • 在模拟器上它也适用于我。但在设备上却没有。你也在设备上测试过吗?
      【解决方案4】:

      这是一个非常古老的线程,但现在有人可能对如何在 Swift 5 中执行此操作感兴趣。

      像这样简单:

      var font: UIFont = UIFont.systemFont(ofSize: 18)
      if let newDescriptor = font.fontDescriptor.withSymbolicTraits(.traitBold) {
          font = UIFont(descriptor: newDescriptor, size: font.pointSize)
      }
      

      【讨论】:

        【解决方案5】:

        从 iOS 7 开始,由于系统字体的字体系列名称不明确,尝试使用字体或系列名称派生粗体/斜体字体不再正常工作。下面是一个使用 UIFontDescriptor 类派生粗体/斜体字体的简单扩展。

        +(UIFont *) font:(UIFont *)font bold:(BOOL)bold italic:(BOOL)italic
        {
            NSUInteger traits = 0;
            if (bold)
            {
                traits |= UIFontDescriptorTraitBold;
            }
            if (italic)
            {
                traits |= UIFontDescriptorTraitItalic;
            }
            return [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:traits] size:font.pointSize];
        }
        

        【讨论】:

        • 这里指出的要点:系统字体与其他字体的工作方式不同。
        【解决方案6】:

        你可以使用

        [UIFont boldSystemFontOfSize:12].
        

        如果您使用自定义字体,则必须直接使用名称

        [UIFont fontWithName:@"Helvetica-Bold" size:17.0].
        

        您可以使用

        查找可能的字体名称
        [UIFont fontNamesForFamilyName:@"American Typewriter"].
        

        在这篇文章中:https://stackoverflow.com/a/15388946/436818Ben M 知道如何动态获取粗体版本。但是扩展该方法以确保获得粗体版本(如果存在),因为还有其他粗体版本,例如 CondensedBold。

        【讨论】:

          【解决方案7】:

          由于在 Swift 中搜索粗体 UIFonts 时会弹出此问题,因此这里有一个全新的答案:

          extension UIFont {
              /// Returns a new font in the same family with the given symbolic traits,
              /// or `nil` if none found in the system.
              func withSymbolicTraits(_ traits: UIFontDescriptor.SymbolicTraits) -> UIFont? {
                  guard let descriptorWithTraits = fontDescriptor.withSymbolicTraits(traits)
                      else { return nil }
                  return UIFont(descriptor: descriptorWithTraits, size: 0)
              }
          }
          

          例子:

          myFont.withSymbolicTraits(.taitBold) // returns the bold version of myFont
          

          【讨论】:

            【解决方案8】:

            没有人发布解决方案:

            • 适用于 Swift
            • UIFont 的扩展
            • 不强制展开
            • 使用与源字体相同的磅值
            • 并且只执行问题中提出的问题(根据现有的 UIFont 创建粗体 UIFont

            所以我正在这样做:

            import UIKit
            
            extension UIFont {
                func boldFont() -> UIFont? {
                    guard let boldDescriptor = fontDescriptor.withSymbolicTraits(.traitBold) else {
                        return nil
                    }
            
                    return UIFont(descriptor: boldDescriptor, size: pointSize)
                }
            }
            

            随意复制粘贴!

            【讨论】:

              猜你喜欢
              • 2012-02-11
              • 1970-01-01
              • 2012-01-21
              • 2021-12-12
              • 1970-01-01
              • 1970-01-01
              • 2011-09-26
              • 1970-01-01
              • 2019-03-02
              相关资源
              最近更新 更多