主页 Kerning文字排版计算
Post
Cancel

Kerning文字排版计算

前言

本文具有强烈的个人感情色彩,如有观看不适,请尽快关闭. 本文仅作为个人学习记录使用,也欢迎在许可协议范围内转载或分享,请尊重版权并且保留原文链接,谢谢您的理解合作. 如果您觉得本站对您能有帮助,您可以使用RSS方式订阅本站,感谢支持!

记录一段代码用于解决iOS文字排版的技术问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
CFAttributedStringRef attributedString;
CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString(attributedString);
CTLineRef line = CTTypesetterCreateLine(typesetter, CFRangeMake(0, 0));
NSArray *runs = (__bridge NSArray *)CTLineGetGlyphRuns(line);

CTRunRef run = (__bridge CTRunRef)runs[runIdx];

const CFIndex glyphCount = CTRunGetGlyphCount(run);
CGPoint *glyphPositions = malloc(sizeof(CGPoint) * glyphCount);
CTRunGetPositions(run, CFRangeMake(0, 0), glyphPositions);

CGRect bounds = CTRunGetImageBounds(run, NULL, CFRangeMake(0, 0));

CGGlyph *glyphs = malloc(sizeof(CGGlyph) * glyphCount);
CTRunGetGlyphs(run, CFRangeMake(0, 0), glyphs);

NSDictionary *runAttributes = (__bridge NSDictionary *)CTRunGetAttributes(run);
CTFontRef font = (__bridge CTFontRef)runAttributes[NSFontAttributeName];

CGSize size;
CTFontGetAdvancesForGlyphs(font, 0, &glyphs[glyphIdx], &size, 1);
            
CTFontGetBoundingRectsForGlyphs()

CTRunGetPositions(run, CFRangeMake(0, 0), glyphPositions);//这个应该拿到的就是带Kerning信息的position

以上代码需要写demo验证.

该博客文章由作者通过 CC BY 4.0 进行授权。

hitTest标准写法

使用UIViewRepresentable 在SwiftUI中桥接 UIKit 视图