相关疑难解决方法(0)

核心文本在iOS中计算字母框架

我需要为NSAttributedString(Core Text)中的每个字符(字形)计算精确的边界框.在整理了一些用于解决类似问题的代码(核心文本选择等)之后,结果非常好,但只有少数帧(红色)正在被正确计算:

在此输入图像描述

大多数帧都是水平或垂直错位(微小位).这是什么原因?我怎样才能完善这段代码?:

-(void)recalculate{

    // get characters from NSString
    NSUInteger len = [_attributedString.string length];
    UniChar *characters = (UniChar *)malloc(sizeof(UniChar)*len);
    CFStringGetCharacters((__bridge CFStringRef)_attributedString.string, CFRangeMake(0, [_attributedString.string length]), characters);

    // allocate glyphs and bounding box arrays for holding the result
    // assuming that each character is only one glyph, which is wrong
    CGGlyph *glyphs = (CGGlyph *)malloc(sizeof(CGGlyph)*len);
    CTFontGetGlyphsForCharacters(_font, characters, glyphs, len);

    // get bounding boxes for glyphs
    CTFontGetBoundingRectsForGlyphs(_font, kCTFontDefaultOrientation, glyphs, _characterFrames, len);
    free(characters); free(glyphs);

    // Measure how mush specec will be needed for this …
Run Code Online (Sandbox Code Playgroud)

glyph objective-c ipad core-text ios

53
推荐指数
1
解决办法
5608
查看次数

标签 统计

core-text ×1

glyph ×1

ios ×1

ipad ×1

objective-c ×1