Core Text - Height of glyph

后端 未结 1 1705
清歌不尽
清歌不尽 2021-02-03 12:52

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

相关标签:
1条回答
  • 2021-02-03 13:32

    Got there 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.
    
    0 讨论(0)
提交回复
热议问题