【问题标题】:How to get the font-size or a bold-version of UIFont instance如何获取 UIFont 实例的字体大小或粗体版本
【发布时间】:2011-09-26 12:18:01
【问题描述】:

如何获取 UIFont 实例的字体大小?

或者,如果有人可以为 UIFont 实现此方法?

- (UIFont *)boldFont;

【问题讨论】:

  • 您正在寻找pointSize of UIFont
  • 收件人:@Deepak,pointSize 和 fontSize 一样吗?
  • @iwill:是的,pointSize 将等于您提供给 UIFont 构造函数的 size: 参数的值。如果您阅读 UIFont 的文档,您会看到 fontSize/fontOfSize 方法采用或返回以点为单位的值。
  • @Deepak:你确定吗?为什么命名为 pointSize 而不是 size 或 fontSize?
  • 是的,我确定。就像@Jason 提到的那样。

标签: iphone ios font-size uifont


【解决方案1】:

使用NSString+UIKit方法来测量具有特定字体的字符串的大小。

iPhone 将同名的普通字体和粗体字体视为完全独立的字体,因此没有简单的方法可以在它们之间进行转换。例如,ArialMT 和 Arial-BoldMT 被操作系统视为完全不同的字体。

编辑:我可能误解了你的问题。也许您正在寻找 UIFont 的 pointSize 属性?

【讨论】:

    【解决方案2】:

    要从 UIFont 实例访问字体大小,请使用

     @property(nonatomic, readonly) CGFloat pointSize
    

    还有更多的属性可以告诉字体大小(大写、小写等)

    capHeight,xHeight,pointSize 
    

    使用下面来访问粗体字

    UIFont* myboldFont = [UIFont boldSystemFontOfSize:11];
    

    【讨论】:

    • boldFont: 方法完全没用;它只是boldSystemFontOfSize: 的包装。很明显,@iwill 在询问如何将现有的 UIFont 转换为粗体。
    【解决方案3】:

    这是一个旧答案,不再是最佳解决方案,请改为查看已接受的答案。

    -(UIFont *)boldFont{
    
    //First get the name of the font (unnecessary, but used for clarity)
    NSString *fontName = self.fontName;
    
    //Some fonts having -Regular on their names. so we have to remove that before append -Bold / -BoldMT
    fontName = [[fontName componentsSeparatedByString:@"-"] firstObject];
    
    //Then append "-Bold" to it.
    NSString *boldFontName = [fontName stringByAppendingString:@"-Bold"];
    
    //Then see if it returns a valid font
    UIFont *boldFont = [UIFont fontWithName:boldFontName size:self.pointSize];
    
    //If it's valid, return it
    if(boldFont) return boldFont;
    
    //Seems like in some cases, you have to append "-BoldMT"
    boldFontName = [fontName stringByAppendingString:@"-BoldMT"];
    boldFont = [UIFont fontWithName:boldFontName size:self.pointSize];
    
    //Here you can check if it was successful, if it wasn't, then you can throw an exception or something.
    return boldFont;
    
    }
    

    【讨论】:

    • 最后:if (boldFont) return boldFont;返回自我;
    • 和:NSString *fontName = self.fontName;应该是 NSString *fontName = self.familyName; // 那就完美了,谢谢!
    • 附加-BoldMT 不适用于ArialMT 等字体;它的粗体等价物是Arial-BoldMT,而不是ArialMT-BoldMT
    【解决方案4】:

    您可以使用 Core Text 检查粗体。使用kCTFontBoldTrait

    【讨论】:

      【解决方案5】:

      这是查找字体大小的最简单方法

      UIFont *anyFont;
      //initialization and other stuff here
      

      现在你想找到它的大小

       CGFloat fontSize= [anyFont pointSize];
      

      【讨论】:

        【解决方案6】:

        UIFontDescriptor 是一个非常强大的类,可以用来获取你想要的字体的bold 版本。它应该是完全安全且面向未来的,因为它只需要使用公共 API 并且不依赖于任何字符串修改。

        这是 Swift 3 中的代码:

        extension UIFont {
        
            func bold() -> UIFont? {
                 let fontDescriptorSymbolicTraits: UIFontDescriptorSymbolicTraits = [fontDescriptor.symbolicTraits, .traitBold]
                 let bondFontDescriptor = fontDescriptor.withSymbolicTraits(fontDescriptorSymbolicTraits)
                 return bondFontDescriptor.flatMap { UIFont(descriptor: $0, size: pointSize) }
            }
        
        }
        

        这是objective-c中的代码:

        @implementation UIFont (RABoldFont)
        
        - (UIFont *)ra_boldFont
        {
            UIFontDescriptor *fontDescriptor = [self fontDescriptor];
            UIFontDescriptorSymbolicTraits traits = fontDescriptor.symbolicTraits;
            traits = traits | UIFontDescriptorTraitBold;
            UIFontDescriptor *boldFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:traits];
            return [UIFont fontWithDescriptor:boldFontDescriptor size:self.pointSize];
        }
        
        @end
        

        【讨论】:

        • 这是自 iOS 7 以来的最佳答案。
        • [fontDescriptor.symbolicTraits, .traitBold] 是否等同于 fontDescriptor.symbolicTraits.union(.traitBold)
        【解决方案7】:

        Swift 3 版本的rafał-augustyniakanswer

        extension UIFont {
        
        
            func boldFont() -> UIFont? {
                let fontDescriptor = self.fontDescriptor
                let fontDescriptorSymbolicTraits: UIFontDescriptorSymbolicTraits = [ fontDescriptor.symbolicTraits, .traitBold ]
        
                guard let boldFontDesc = fontDescriptor.withSymbolicTraits(fontDescriptorSymbolicTraits) else {
                    return nil
                }
        
                return UIFont(descriptor: boldFontDesc, size: pointSize)
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2012-01-21
          • 2011-10-21
          • 1970-01-01
          • 1970-01-01
          • 2012-02-11
          • 2014-02-13
          • 1970-01-01
          • 2013-04-07
          • 2015-10-24
          相关资源
          最近更新 更多