测量CGFont中最大的人物尺寸 [英] Measure dimensions of biggest character in a CGFont
问题描述
CATextLayer
,它将只包含给定大小的给定字体的一个字符。为了让它更漂亮,我想设置图层的边界,使字符适合整齐。 问题是我必须确定最大的尺寸字符在 CGFont
这将是文本图层界限计算的基础。
在这个问题上因此,测量字符串中每个字符的大小如何获得每个角色的大小。所以一个解决方案是遍历字体的字符,并找出最大的字符。
但是我发现 CGFontGetFontBBox
函数。 文档说明
返回值
字体的边界框。
讨论
边界框是>字体中所有字形的所有边界框的联合。这个值是以字形空间单位指定的。
这听起来对我来说这正是我想要的。一个问题仍然是我必须将字形空间单位转换回像素。我用下面的代码尝试了它,但是它给出了奇怪的结果:
/ *计算字体中最大字符的边界框(cGFontRef)aFont withSize:(CGFloat)aSize
{
if(!aFont)bb字体大小
* /
- (CGSize)boundingBoxForFont: {
CFStringRef fontName = CFStringCreateWithCString(NULL,Helvetica,CFStringGetSystemEncoding());
aFont = CGFontCreateWithFontName(fontName);
CFRelease(fontName);
}
CGRect bbox = CGFontGetFontBBox(aFont);
int units = CGFontGetUnitsPerEm(aFont);
CGFloat maxHeight =(CGRectGetHeight(bbox)/(CGFloat)units)* aSize;
CGFloat maxWidth =(CGRectGetWidth(bbox)/(CGFloat)units)* aSize;
return CGSizeMake(maxWidth,maxHeight);
$ / code>
奇怪的是CGRect bbox
比高更宽,这对我来说是没有意义的,因为字体中的字符通常比宽度更高,但也许我错误地使用了它。
还有另外一个如何使用这个函数的方法吗?
编辑
是我混淆字符和字形的情况?是的,这个字体包含了一个宽的字形,代表了几个字符?
解析方案是的,我认为你在正确的轨道上。例如,连字符(如ffl)被视为单个字形,并且很可能比较高。另外,时间冲刺 - 应该和首都的高度一样宽。
I am using CoreAnimation to build a view consisting of several layer which will be animated later on. There are also CATextLayer
s which will contain only one character of a given font in a given size. To make it pretty I would like to set the bounds of the layer so that the character fits in neatly.
The problem is that I have to determine the size of the biggest character in a CGFont
which will be the basis of the bounds calculation for the text layer.
In this question on SO measure size of each character in a string it is explained how to get the size of each character. So one solution would be to iterate through the characters of the font and find out the biggest character.
However I have found the CGFontGetFontBBox
function. The documentation says
Return Value
The bounding box of the font.
Discussion
The font bounding box is the union of all of the bounding boxes for all the glyphs in a >font. The value is specified in glyph space units.
This sounds to me that this is exactly what I want. One proble remains that I have to convert the glyph space units back to pixels. I tried it with the following code but it gives strange results:
/* calculate the bounding box of the biggest character in the font with a given
* font size
*/
- (CGSize) boundingBoxForFont:(CGFontRef)aFont withSize:(CGFloat)aSize
{
if (!aFont) {
CFStringRef fontName = CFStringCreateWithCString(NULL, "Helvetica", CFStringGetSystemEncoding());
aFont = CGFontCreateWithFontName(fontName);
CFRelease(fontName);
}
CGRect bbox = CGFontGetFontBBox(aFont);
int units = CGFontGetUnitsPerEm(aFont);
CGFloat maxHeight = ( CGRectGetHeight(bbox) / (CGFloat) units ) * aSize;
CGFloat maxWidth = ( CGRectGetWidth(bbox) / (CGFloat) units ) * aSize;
return CGSizeMake(maxWidth, maxHeight);
}
It is strange that the CGRect bbox
is wider than taller, which makes no sense to me as characters in a font are usually taller than wider, but maybe I am using it wrong.
Is there an alternative of how to use this function?
EDIT
Could it be the case that I am mixing up characters and glyphs? Could it be that the font contains a wide glyph which represents several characters?
Yes, I think you are on the right track. For example, ligatures such as ffl count as a single glyph and are most likely wider than tall. Also, the em dash — is supposed to be as wide as the capital height.
这篇关于测量CGFont中最大的人物尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!