请问我们怎样才能得到特定的法语字母阿拉伯语的路径?我刚刚发现 CTFontCreatePathForGlyph 会给 CGPathRef 之类的,但它会是 text 的轮廓。
我需要这个真实的文本路径来显示文本绘制动画..
请有任何帮助
请问我们怎样才能得到特定的法语字母阿拉伯语的路径?我刚刚发现 CTFontCreatePathForGlyph 会给 CGPathRef 之类的,但它会是 text 的轮廓。
我需要这个真实的文本路径来显示文本绘制动画..
请有任何帮助
您根本不需要将您的路径转换为 NSString。
您可以按如下方式创建文本路径:
CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 72.0f, NULL);
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
(id)font, kCTFontAttributeName,
nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"Hello World!"
attributes:attrs];
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
CFArrayRef runArray = CTLineGetGlyphRuns(line);
// for each RUN
for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
{
// Get FONT for this run
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
// for each GLYPH in run
for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
{
// get Glyph & Glyph-data
CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
CGGlyph glyph;
CGPoint position;
CTRunGetGlyphs(run, thisGlyphRange, &glyph);
CTRunGetPositions(run, thisGlyphRange, &position);
// Get PATH of outline
{
CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL);
CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);
CGPathAddPath(letters, &t, letter);
CGPathRelease(letter);
}
}
}
CFRelease(line);
这是您创建路径的方式,示例代码请参考此链接。此代码是此示例项目的一部分。希望这可以帮助你
我在 Swift 中需要这个,但解决起来很痛苦。我希望这对其他人有用!我将其编写为对 String 和 NSAttributedString 的扩展,以使其更加通用。
根据您绘制路径的方式,您可能会看到这些字母是颠倒的。您可以通过在对 CTFontCreatePathForGlyph() 的调用中添加垂直翻转变换来解决此问题(垂直翻转只是 scaleY 为 -1 的 CGAffineTransform)。
public extension String {
func path(withFont font: UIFont) -> CGPath {
let attributedString = NSAttributedString(string: self, attributes: [.font: font])
let path = attributedString.path()
return path
}
}
public extension NSAttributedString {
func path() -> CGPath {
let path = CGMutablePath()
// Use CoreText to lay the string out as a line
let line = CTLineCreateWithAttributedString(self as CFAttributedString)
// Iterate the runs on the line
let runArray = CTLineGetGlyphRuns(line)
let numRuns = CFArrayGetCount(runArray)
for runIndex in 0..<numRuns {
// Get the font for this run
let run = unsafeBitCast(CFArrayGetValueAtIndex(runArray, runIndex), to: CTRun.self)
let runAttributes = CTRunGetAttributes(run) as Dictionary
let runFont = runAttributes[kCTFontAttributeName] as! CTFont
// Iterate the glyphs in this run
let numGlyphs = CTRunGetGlyphCount(run)
for glyphIndex in 0..<numGlyphs {
let glyphRange = CFRangeMake(glyphIndex, 1)
// Get the glyph
var glyph : CGGlyph = 0
withUnsafeMutablePointer(to: &glyph) { glyphPtr in
CTRunGetGlyphs(run, glyphRange, glyphPtr)
}
// Get the position
var position : CGPoint = .zero
withUnsafeMutablePointer(to: &position) {positionPtr in
CTRunGetPositions(run, glyphRange, positionPtr)
}
// Get a path for the glyph
guard let glyphPath = CTFontCreatePathForGlyph(runFont, glyph, nil) else {
continue
}
// Transform the glyph as it is added to the final path
let t = CGAffineTransform(translationX: position.x, y: position.y)
path.addPath(glyphPath, transform: t)
}
}
return path
}
}