Using NSLayoutManager to calculate frames for each glyph

后端 未结 1 1526
南旧
南旧 2021-02-13 03:15

On this thread, Core Text calculate letter frame in iOS, they were able to calculate the frame of each glyph very precisely using Core Text. The final rects hug the actual draw

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

    I've found a solution for this issue or at least sort of

    Problem: boundingRectForGlyphRange results is off in case of RTL text.

    So in case of RTL text is detected only:

    Using NSLayoutManager method locationForGlyphAtIndex, for each letter in the range. These are the starting points of each glyph in the range. Using these points I adjust the boundingRect correctly.

    Here is an outline of it:

    CGPoint endPoint = ((NSValue *)pointsOfGlyps.firstObject).CGPointValue;
    CGPoint startPoint = ((NSValue *)pointsOfGlyps.lastObject).CGPointValue;
    boundingRect.origin.x =  startPoint.x;
    boundingRect.size.width = endPoint.x - startPoint.x + letterWidth;
    

    letterWidth is an approximation of a letter width calculated as the delta between two consecutive starting points.

    0 讨论(0)
提交回复
热议问题