ios UIFont 的点大小究竟是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11776767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:33:10  来源:igfitidea点击:

What exactly is UIFont's point size?

objective-ciosfontsuikituifont

提问by mattjgalloway

I am struggling to understand exactly what the point size in UIFontmeans. It's not pixels and it doesn't appear to be the standard definition of point which is that they relate to 1/72th inch.

我正在努力理解点大小的确切UIFont含义。它不是像素,它似乎不是点的标准定义,即它们与 1/72 英寸有关。

I worked out the pixel size using -[NSString sizeWithFont:]of fonts at various sizes and got the following:

我使用-[NSString sizeWithFont:]各种大小的字体计算出像素大小并得到以下结果:

| Point Size | Pixel Size |
| ---------- | ---------- |
| 10.0       | 13.0       |
| 20.0       | 24.0       |
| 30.0       | 36.0       |
| 40.0       | 47.0       |
| 50.0       | 59.0       |
| 72.0       | 84.0       |
| 99.0       | 115.0      |
| 100.0      | 116.0      |

(I did [@"A" sizeWithFont:[UIFont systemFontOfSize:theSize]])

(我做了[@"A" sizeWithFont:[UIFont systemFontOfSize:theSize]]

And looking at the 72.0point size, that is not 1-inch since this is on a device with a DPI of 163, so 1-inch would be 163.0 pixels, right?

查看72.0点大小,这不是 1 英寸,因为这是在 DPI 为 163 的设备上,所以 1 英寸将是 163.0 像素,对吗?

Can anyone explain what a "point" in UIFontterms is then? i.e. is my method above wrong and really if I used something else I'd see something about the font is 163 pixels at 72 point? Or is it purely that a point is defined from something else?

谁能解释一下什么是“点”UIFont呢?即我上面的方法是错误的,真的如果我使用其他东西,我会看到关于字体的一些东西是 72 点处的 163 像素?或者纯粹是从其他东西定义了一个点?

采纳答案by hooleyhoop

A font has an internal coordinate system, think of it as a unit square, within which a glyph's vector coordinates are specified at whatever arbitrary size accommodates all the glyphs in the font +- any amount of margin the font designer chooses.

字体有一个内部坐标系,可以把它想象成一个单位正方形,在其中一个字形的矢量坐标被指定为任意大小,可以容纳字体中的所有字形+ - 字体设计者选择的任意数量的边距。

At 72.0 points the font's unit square is one inch. Glyph xof font yhas an arbitrary size in relation to this inch square. Thus a font designer can make a font that appears large or small in relation to other fonts. This is part of the font's 'character'.

在 72.0 点时,字体的单位正方形是一英寸。字体y 的字形x相对于这个平方英寸具有任意大小。因此,字体设计者可以使字体相对于其他字体显得大或小。这是字体“字符”的一部分。

So, drawing an 'A' at 72 points tells you that it will be twice as high as an 'A' drawn at 36 points in the same font - and absolutely nothing else about what the actual bitmap size will be.

因此,在 72 点处绘制一个“A”告诉您,它将是在相同字体中在 36 个点处绘制的“A”的两倍 - 并且绝对没有关于实际位图大小的其他信息。

ie For a given font the only way to determine the relationship between point size and pixels is to measure it.

即对于给定的字体,确定点大小和像素之间关系的唯一方法是对其进行测量。

回答by Sulthan

I am not sure how -[NSString sizeWithFont:]measures the height. Does it use line height or the difference between the peaks of the beziers? What text did you use?

我不确定如何-[NSString sizeWithFont:]测量高度。它使用线高还是贝塞尔曲线峰之间的差异?你用的什么文字?

I believe -[UIFont lineHeight]would be better to measure the height.

我相信-[UIFont lineHeight]测量高度会更好。

Edit: Also, note that none of the measurement methods returns the size in pixels. It returns the size in points. You have to multiply the result by [UIScreen mainScreen].scale.

编辑:另外,请注意,没有任何测量方法返回以像素为单位的大小。它返回points. 您必须将结果乘以[UIScreen mainScreen].scale

Note the difference between typographic pointsused when constructing the font and pointsfrom iOS default logical coordinate space. Unfortunately, the difference is not explained very clearly in the documentation.

请注意typographic points在构建字体时使用和points从 iOS使用的区别default logical coordinate space。不幸的是,文档中没有很清楚地解释这种差异。

回答by rickster

I first wondered if this had something to do with the way [CSS pixels are defined at 96 per "inch"][1] while UI layout points are defined at 72 per "inch". (Where, of course, an "inch" has nothing to do with a physical inch.) Why would web standards factor into UIKit business? Well, you may note when examining stack traces in the debugger or crash reports that there's some WebKit code underlying a lot of UIKit, even when you're not using UIWebView. Actually, though, it's simpler than that.

我首先想知道这是否与 [CSS 像素定义为每“英寸”96 个][1] 而 UI 布局点定义为每“英寸”72 个的方式有关。(当然,“英寸”与物理英寸无关。)为什么 Web 标准会影响 UIKit 业务?好吧,您可能会在检查调试器中的堆栈跟踪或崩溃报告时注意到,即使您没有使用UIWebView. 但实际上,它比这更简单。

First, the font size is measured from the lowest descender to the highest ascender in regular Latin text-- e.g. from the bottom of the "j" to the top of the "k", or for convenient measure in a single character, the height of "?". (That's U+0192 "LATIN SMALL LETTER F WITH HOOK", easily typed with option-F on a US Mac keyboard. People used it to abbreviate "folder" way back when.) You'll notice that when measured with that scheme, the height in pixels (on a 1x display) matches the specified font size -- e.g. with [UIFont systemFontOfSize:14], "?" will be 14 pixels tall. (Measuring the capital "A" only accounts for an arbitrary portion of the space measured in the font size. This portion may change at smaller font sizes; when rendering font vectors to pixels, "hinting" modifies the results to produce more legible onscreen text.)

首先,字体大小是在常规拉丁文本中从最低的下降到最高的上升——例如从“j”的底部到“k”的顶部,或者为了方便测量单个字符的高度的 ”?”。(那是 U+0192“带钩的拉丁小写字母 F”,在美国 Mac 键盘上用选项-F 很容易输入。人们用它来缩写“文件夹”。)你会注意到,当使用该方案进行测量时,以像素为单位的高度(在 1x 显示器上)与指定的字体大小匹配——例如[UIFont systemFontOfSize:14],“?” 将是 14 像素高。(测量大写“A”仅占以字体大小测量的空间的任意部分。这部分可能会在较小的字体大小时发生变化;将字体矢量渲染为像素时,“提示”会修改结果以生成更清晰的屏幕文本.)

However, fonts contain all sorts of glyphs that don't fit into the space defined by that metric. There are letters with diacritics above an ascender in eastern European languages, and all kinds of punctuation marks and special characters that fit in a "layout box" much larger. (See the Math Symbols section in Mac OS X's Special Characters window for plenty of examples.)

但是,字体包含各种不适合该度量定义的空间的字形。在东欧语言中,有一些带有变音符号的字母,还有各种标点符号和特殊字符,可以放在一个更大的“布局框”中。(有关大量示例,请参阅 Mac OS X 的特殊字符窗口中的数学符号部分。)

In the CGSizereturned by -[NSString sizeWithFont:], the width accounts for the specific characters in the string, but the height only reflects the number of lines. Line height is a metric specified by the font, and related to the "layout box" encompassing the font's largest characters.

CGSize返回的 by 中-[NSString sizeWithFont:],宽度占字符串中特定字符,但高度仅反映行数。行高是字体指定的度量标准,与包含字体最大字符的“布局框”相关。

回答by Richard Bao

I agree this is very confusing. I'm trying to give you some basic explanation here to make the things clearer.

我同意这非常令人困惑。我试图在这里给你一些基本的解释,以使事情更清楚。

First, the DPI (dot-per-inch) thing comes from printing, on physical papers. So does font. The unit point was invented to discribe physical printing size of text, just because inch is too large for usual text sizes. Then people invented point, that is the length of 1/72 inch (actually evolved in the history), to describe text size easily. So yes, if you are writing a document in Word or other word processing software for printing, you will get absolutely one-inch-height text if you use 72pt font.

首先,DPI(每英寸点数)来自物理纸上的打印。字体也是。单位点被发明来描述文本的物理打印大小,只是因为英寸对于通常的文本大小来说太大了。后来人们发明了点,即1/72英寸的长度(实际上是历史上进化而来的),以方便地描述文字大小。所以是的,如果您在 Word 或其他文字处理软件中编写用于打印的文档,如果您使用 72pt 字体,您将获得绝对一英寸高的文本。

Second, the theoretical text height is usually different from the rendered strokes you can actually see by your eyes. The original text height idea came from the actual glyphs used for printing. All letters are engraved on glyph blocks, which share the same height – which matches the font point height. However, depending on different letters and different font design, the actual visible part of the text may a little bit shorter than the theoretical height. Helvetica Neue is actually very standard. If you measure the top of a letter "k" to the bottom of a letter "p", it will match the font height.

其次,理论文本高度通常与您肉眼实际看到的渲染笔划不同。原始文本高度的想法来自用于打印的实际字形。所有字母都刻在字形块上,它们具有相同的高度——与字体点高度相匹配。但是,根据不同的字母和不同的字体设计,文本的实际可见部分可能会比​​理论高度短一点。Helvetica Neue 其实很标准。如果您测量字母“k”的顶部到字母“p”的底部,它将匹配字体高度。

Third, computer display screwed up DPI, as well as the definition of point at the same time. The resolution of computer displays are described by their native pixels, such as 1024 x 768 or 1920 x 1080. Software actually doesn't care the physical size of your monitors, because everything would be very fuzzy if they scale screen content like printing on paper — just the physical resolution is not high enough to make everything smooth and legit. Software uses a very simple and dead way: Fixed DPI for whatever monitor you use. For Windows, it's 96DPI; for Mac, it's 72DPI. That's said, no matter how many pixels make an inch on your monitor, software just ignores it. When the operating system renders text in 72pt, it would be always 96px high on Windows and 72px high on Mac. (That's why Microsoft Word documents always look smaller on Mac and you usually need zoom to 125%.)

第三,电脑显示器搞砸了DPI,同时也搞砸了点的定义。计算机显示器的分辨率由它们的原始像素描述,例如 1024 x 768 或 1920 x 1080。软件实际上并不关心显示器的物理尺寸,因为如果它们缩放屏幕内容(例如在纸上打印),一切都会变得非常模糊— 只是物理分辨率不够高,无法让一切顺利合法。软件使用了一种非常简单而死的方式:固定 DPI 用于您使用的任何显示器。对于 Windows,它是 96DPI;对于 Mac,它是 72DPI。也就是说,无论您的显示器上有多少像素构成一英寸,软件都会忽略它。当操作系统以 72pt 呈现文本时,它在 Windows 上始终为 96px 高,在 Mac 上为 72px 高。(那'

Finally on iOS, it's very similar, no matter it's iPhone, iPod touch, iPad or Apple Watch, iOS uses the fixed 72DPI for non-retina screen, 144DPI for @2x retina display, and 216DPI for @3x retina display used on iPhone 6 Plus.

最后在iOS上,非常相似,无论是iPhone、iPod touch、iPad还是Apple Watch,iOS都使用固定的72DPI用于非视网膜屏幕,144DPI用于@2x视网膜显示,以及用于iPhone 6的@3x视网膜显示216DPI加。

Forget about the real inch. It only exists on actual printing, not for displaying. For software displaying text on your screen, it's just an artificial ratio to physical pixels.

忘记真正的英寸。它只存在于实际打印中,不用于显示。对于在屏幕上显示文本的软件,它只是与物理像素的人为比例。

回答by idz

The truth, as far as I have been able to ascertain, is that UIFontlies. All of UIKittakes liberties with fonts. If you want the truth you need to use CoreText, but in a lot of cases it will be slower! (So in the case of your pixel height table I think it was that it adds some sort of a + bx factor where x is point size.

据我所知,真相就是UIFont谎言。所有的UIKit字体都可以随意使用。如果你想要真相,你需要使用CoreText,但在很多情况下它会更慢!(所以在你的像素高度表的情况下,我认为它添加了某种 + bx 因子,其中 x 是点大小。

So why does it do this? Speed! UIKitrounds up stuff and fiddles with spacing so that it can cache bitmaps. Or at least that was my take away!

那么它为什么要这样做呢?速度!UIKit四舍五入的东西和摆弄间距,以便它可以缓存位图。或者至少那是我的收获!