Bin*_*Guy 8 cocoa core-graphics core-text ios
我有2个属性字符串说'A'和'.'
我需要计算每个字符串的高度.目前返回的高度对于两者都是相同的,它似乎返回给定字体中最高字符的最大可能高度(即使字符串中不存在该字符).
我想得到每个这些字符的确切像素高度,这样我就可以调整它们周围的视图,使其符合角色(字形).我尝试过使用CTFramesetterSuggestFrameSizeWithConstraints()和CTLineGetTypographicBounds(),但它返回一个类似于属性字符串大小方法的数字.
非常感谢有关如何做到这一点的任何提示!
Bin*_*Guy 11
到最后,你可以这样做:
// Create an attributed string
CTLineRef line = CTLineCreateWithAttributedString(_string);
// Get an array of glyph runs from the line
CFArrayRef runArray = CTLineGetGlyphRuns(line);
// loop through each run in the array
CTRunRef run = ....
// Get the range of the run
CFRange range = CFRangeMake...
// Use CTRunGetImageBounds
CGRect glyphRect = CTRunGetImageBounds(run, context, range);
// glyphRect now contains the bounds of the glyph run, if the string is just 1 character you have the correct dimensions of that character.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1563 次 |
| 最近记录: |