Body Text - Glyph Height

I have two attributed strings: "A" and "."

I need to calculate the height of each of these lines. Currently, the return height is the same for both, it seems to return the maximum possible height for the highest character in a given font (even if that character is not in the line).

I would like to get the exact pixel height for each of these characters, so that I can resize around them, which is perfect for a character (glyph). I tried using CTFramesetterSuggestFrameSizeWithConstraints () and CTLineGetTypographicBounds (), but it returns a number similar to the attribute method of assignable strings.

Would thank for any tips on how to do this!

+6
source share
1 answer

In the end, you can do it like 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. 
+7
source

Source: https://habr.com/ru/post/921762/


All Articles