Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 来自字符串的CGPathRef_Iphone_Objective C_Ios_Core Text_Cgpath - Fatal编程技术网

Iphone 来自字符串的CGPathRef

Iphone 来自字符串的CGPathRef,iphone,objective-c,ios,core-text,cgpath,Iphone,Objective C,Ios,Core Text,Cgpath,请问我们怎样才能得到特定的阿拉伯语或法语字母的路径?我刚刚发现CTFontCreatePathForGlyph会给CGPathRef一个类似的值,但它将是文本的大纲 我需要这个真实的文本路径来显示文本绘图动画 请提供任何帮助您根本不需要将您的路径转换为NSString 您可以按如下方式创建文本路径: CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 72.0f, NULL); NSDictionary *attr

请问我们怎样才能得到特定的阿拉伯语或法语字母的路径?我刚刚发现CTFontCreatePathForGlyph会给CGPathRef一个类似的值,但它将是文本的大纲

我需要这个真实的文本路径来显示文本绘图动画


请提供任何帮助

您根本不需要将您的路径转换为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);
CTFontRef font=CTFontCreateWithName(CFSTR(“Helvetica Bold”),72.0f,NULL);
NSDictionary*attrs=[NSDictionary Dictionary WithObjectsAndKeys:
(id)字体,kCTFontAttributeName,
零];
NSAttributedString*attrString=[[NSAttributedString alloc]initWithString:@“你好,世界!”
属性:attrs];
CTLineRef line=CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
CFArrayRef runArray=CTLineGetGlyphRuns(行);
//每次跑步
对于(CFIndex runIndex=0;runIndex
这是如何创建路径的,有关示例代码,请参阅。此代码是此示例项目的一部分。
希望这对您有所帮助

我在Swift中需要它,但它让我很痛苦。我希望这对其他人有用!我将其作为String和NSAttributedString的扩展编写,以使其更加通用

根据绘制路径的方式,您可能会看到字母颠倒。您可以通过将垂直翻转变换添加到对CTFontCreatePathForGlyph()的调用中来修复此问题(垂直翻转只是一个缩放比例为-1的CGAffineTransform)

公共扩展字符串{
func路径(withFont:UIFont)->CGPath{
让attributedString=NSAttributedString(字符串:self,属性:[.font:font])
let path=attributedString.path()
返回路径
}
}
公共扩展NSAttributedString{
func path()->CGPath{
let path=CGMutablePath()
//使用CoreText将字符串作为一条线进行布局
let line=CTLineCreateWithAttributedString(自身为CFAttributedString)
//在线路上迭代运行
让runArray=CTLineGetGlyphRuns(行)
让numRuns=CFArrayGetCount(runArray)

对于0中的runIndex..为什么要将路径转换为字符串?你可以直接在返回的路径上设置任务的动画:)我不会转换我想获得特定字符串的路径我的目标是为我的项目学校(如字母学校或我写的学校)创建一个应用程序解释查看此应用程序我想和他们编写应用程序时一样思考信“对不起我的床英语”谢谢:)但是使用这种方法我们将得到大纲路径。是的,我知道。但是这是你完成工作的方式。显然你需要更改代码来满足你的要求。我如何更改代码以获得正确的结果例如,字母A和你的方法由12行组成,但实际上是3行对不起,我的英语不好拯救我的生命!真是太棒了。
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
    }
}