【问题标题】:Font size in pixels字体大小(以像素为单位)
【发布时间】:2010-11-06 17:50:23
【问题描述】:

在 iPhone 上,如何计算给定点大小的字符大小(以像素为单位)?

【问题讨论】:

    标签: iphone cocoa font-size typography


    【解决方案1】:

    您无法可靠地将点转换为像素,因为 ppi(每英寸点数)会因显示器而异。读一读;

    http://hsivonen.iki.fi/units/

    Convert Pixels to Points

    也就是说,有些人整理了一些参考表和计算器,可以帮助您入门;

    http://www.unitconversion.org/typography/postscript-points-to-pixels-x-conversion.html

    http://sureshjain.wordpress.com/2007/07/06/53/

    【讨论】:

    • 我很确定人们不会将显示器插入他们的 iPhone。
    • 如果苹果在不改变物理屏幕尺寸的情况下改变 iPhone 的分辨率会怎样? PPI 会发生变化,因此在旧 PPI 上进行的任何点到像素的转换都是多余的。
    • MatW - 我同意这是需要考虑的事情,但在这种情况下,我很可能不得不重新测试我的应用程序。我想很多应用都会有问题。
    • 迈克丹尼尔斯:没关系。并非所有 iPhone-OS 设备都具有相同的分辨率; iPod touch 的分辨率略低。此外,有朝一日,Apple 可能会增加应用程序寻址电视或显示器的能力,该设备通过扩展坞连接到该设备。
    • Peter Hosey 不知道他在说什么。所有 iPhone OS 设备在像素方面都有相同的底层指标。如果未来的 iOS 设备不支持,我会吃掉我的帽子。
    【解决方案2】:

    点大小为defined as 1/72 of an inch。也就是说,72 磅字体从最低下降到最高上升大约 1 英寸。因此,72pt 字体中字形的最大高度约为 1 英寸。

    Apple's iphone tech specs page 声称 iPhone目前的分辨率为每英寸 163 像素。所以 72 个点是 163 个像素,或者每个点大约 2.2639 个像素。请记住,每个字形的高度和宽度都不同,因此这是对大小的非常粗略的估计。一般来说,baselines 之间的距离会比字体的磅值大一点,这样文本行就不会相互碰撞。

    如果您需要精确测量(并且您可能确实需要),那么您需要使用字体度量信息实际测量字体字形。您可以使用NSString's UIKit additions 来做到这一点,它可以让您测量特定字符串在屏幕上呈现时的大小。

    【讨论】:

    • 另外值得注意的是,iPhone 并不是唯一的 iPhone-OS 设备。 iPod touch 是 160 dpi。因此,并非所有 iPhone-OS 设备都具有相同的分辨率。如果 Apple 将其翻倍,推出平板电脑,推出手表或其他任何东西,并且您假设像素数量是固定的,那么您就完蛋了。独立于分辨率,因为如果你不这样做,你的应用总有一天会崩溃。
    • 嘿,猜猜看 - Apple 推出了一款具有不同 dpi 的平板电脑 :-)
    • 另外,Apple 制造了具有更高 dpi 的手机和 ipod...您的应用肯定会崩溃 :)
    • 嘿,猜猜看 - Apple 为他们的 iPod Nano 引入了表带,因此他们在技术上也推出了手表。 :)
    • @philsquared 没关系,因为系统假定您始终编码为 72 dpi,然后在运行时自动升级。
    【解决方案3】:

    我相信您正在寻找 UIFont NSString 扩展,它允许您计算给定 UIFont 的字符串的大小。

    这里是Link

    特别是sizeWithFont 方法。

    【讨论】:

    • 链接已损坏。
    【解决方案4】:

    我会在我弄清楚它们时添加它们:

    具有 12pt systemFont 的 sizedToFit UILabel 高 15px。

    【讨论】:

      【解决方案5】:

      要将 iPhone4 上的字体大小(以磅为单位)与 Photoshop 中的字体大小(以磅为单位)匹配,您必须将 Photoshop 文档设置为 144dpi。我已经进行了许多测试,这就是产生 1:1 结果的分辨率。

      步骤:

      • 在 iPhone4 上截取“设置 » 常规 » 辅助功能 » 大文本”
      • 在 Photoshop 中打开屏幕截图
      • “重新采样图像”关闭的情况下将分辨率从 72dpi 更改为 144dpi
      • 在 Photoshop 中重新键入文本(以磅为单位)以匹配屏幕截图中的大小

      我经历了许多不同的分辨率,包括上面答案中提到的 163dpi,我发现 144dpi 产生 1:1 的结果。我还针对我知道点大小和 144dpi 的本地应用程序对此进行了测试。

      【讨论】:

      • 每个空格的 125% 似乎都非常合适
      • 不确定它是否回答了这个问题,但这正是我想要的,您最终应该将此答案移至“如何在 Photoshop 和 iOS/xcode 中匹配字体大小”
      • 这对我很有用。当然,它适用于 1x 而不是 2x (Retina) 的分辨率。因此,例如,在 iPad 布局上,使用 144 dpi 的 768x1024 分辨率来匹配字体大小。
      • 144 适用于 iPhone 4s 和 5,但似乎要匹配 iPhone 6 和 6+ 需要 Photoshop 中的 216dpi 分辨率。
      • 链接已关闭。
      【解决方案6】:

      我们的图形艺术家在某些设备上非常具体地使用像素大小而不是磅值大小。 下面的函数将根据像素大小返回字体。 它使用蛮力方法来查找壁橱字体,然后缓存结果,因此下次返回会非常快。我一直很欣赏 cmets 关于如何使这段代码变得更好的问题。我将此函数用作名为 utils 的类中的静态类成员。 您可以轻松粘贴到您正在使用的任何类中。 希望对您有所帮助。

      /** return a font as close to a pixel size as possible
      example:
          UIFont *font = [Utils fontWithName:@"HelveticaNeue-Medium" sizeInPixels:33];
       @param fontName name of font same as UIFont fontWithName
       @param sizeInPixels size in pixels for font
       */
      +(UIFont *) fontWithName:(NSString *) fontName sizeInPixels:(CGFloat) pixels {
          static NSMutableDictionary *fontDict; // to hold the font dictionary
          if ( fontName == nil ) {
              // we default to @"HelveticaNeue-Medium" for our default font
              fontName = @"HelveticaNeue-Medium";
          }
          if ( fontDict == nil ) {
              fontDict = [ @{} mutableCopy ];
          }
          // create a key string to see if font has already been created
          //
          NSString *strFontHash = [NSString stringWithFormat:@"%@-%f", fontName , pixels];
          UIFont *fnt = fontDict[strFontHash];
          if ( fnt != nil ) {
              return fnt; // we have already created this font
          }
          // lets play around and create a font that falls near the point size needed
          CGFloat pointStart = pixels/4;
          CGFloat lastHeight = -1;
          UIFont * lastFont = [UIFont fontWithName:fontName size:.5];\
      
          NSMutableDictionary * dictAttrs = [ @{ } mutableCopy ];
          NSString *fontCompareString = @"Mgj^";
          for ( CGFloat pnt = pointStart ; pnt < 1000 ; pnt += .5 ) {
              UIFont *font = [UIFont fontWithName:fontName size:pnt];
              if ( font == nil ) {
                  NSLog(@"Unable to create font %@" , fontName );
                  NSAssert(font == nil, @"font name not found in fontWithName:sizeInPixels" ); // correct the font being past in
              }
              dictAttrs[NSFontAttributeName] = font;
              CGSize cs = [fontCompareString sizeWithAttributes:dictAttrs];
              CGFloat fheight =  cs.height;
              if ( fheight == pixels  ) {
                  // that will be rare but we found it
                  fontDict[strFontHash] = font;
                  return font;
              }
              if ( fheight > pixels ) {
                  if ( lastFont == nil ) {
                      fontDict[strFontHash] = font;
                      return font;
                  }
                  // check which one is closer last height or this one
                  // and return the user
                  CGFloat fc1 = fabs( fheight - pixels );
                  CGFloat fc2 = fabs( lastHeight  - pixels );
                  // return the smallest differential
                  if ( fc1 < fc2 ) {
                      fontDict[strFontHash] = font;
                      return font;
                  } else {
                      fontDict[strFontHash] = lastFont;
                      return lastFont;
                  }
              }
              lastFont = font;
              lastHeight = fheight;
          }
          NSAssert( false, @"Hopefully should never get here");
          return nil;
      }
      

      【讨论】:

        【解决方案7】:

        获取给定字体和大小的像素高度的最简单方法是在 NSString 上使用 boundingRect 方法。 (我在这里使用@"Ap" 来确保它包含一个下降器和一个上升器。)

        - (CGFloat)heightForFont:(UIFont *)font
        {
            NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
            CGRect boundingRect = [@"Ap" boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:context];
            return boundingRect.size.height;
        }
        

        【讨论】:

          【解决方案8】:

          您可以通过渲染NSAttributedString 并获取其大小来动态计算像素大小。

          extension UIFont {
              var pixelSize: CGFloat {
                  let string = "AWZgjpq"
                  let attributedString = NSMutableAttributedString(string: string)
                  attributedString.setAttributes([.font: self], range: NSRange(location: 0, length: string.count))
                  return attributedString.size().height
              }
          }
          

          进一步的优化可能是添加查找字典和缓存结果和/或使测试字符串不是变量(但调用本身非常快)。此外,如果您的字体有一些非常不规则的字形,您也可以将它们添加到测试字符串中。

          用法:let pixelSize = label.font.pixelSize

          【讨论】:

            猜你喜欢
            • 2011-08-17
            • 2013-04-26
            • 2016-06-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-07-04
            • 2010-12-16
            相关资源
            最近更新 更多