Height of glyph

I have 2 attributed strings say 'A' and '.'

I need to calculate the height of each of these strings. Currently the height returned is the same for both, it seems to return the maximum possible height for the tallest character in a given font (even if that character isn't present in the string).

I would like to get the exact pixel height for each of these characters, so that I can resize a view around them that fits the character (glyph) snugly. I've tried using CTFramesetterSuggestFrameSizeWithConstraints() and CTLineGetTypographicBounds() but it returns a number similar to the attributed strings size method.

Would appreciate any tips on how to go about doing this!


到最后,你可以这样做:

// 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.
链接地址: http://www.djcxy.com/p/81924.html

上一篇: iOS打印界面导致自定义视图出现转换问题

下一篇: 字形的高度