zoukankan      html  css  js  c++  java
  • 由文字生成path后制作写字的动画

    在看以下这个开源组件的时候,发现一个非常棒的方法,能够将文字生成path,这样就能够作出用笔写字的效果了。

    关键代码:
    -(CGPathRef)pathRefFromText
    {
        NSAttributedString *attributed = self.attributedText;
        
        CGMutablePathRef letters = CGPathCreateMutable();
        CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)attributed);
        CFArrayRef runArray = CTLineGetGlyphRuns(line);
        for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
        {
            CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
            CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
            
            for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
            {
                CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
                CGGlyph glyph;
                CGPoint position;
                CTRunGetGlyphs(run, thisGlyphRange, &glyph);
                CTRunGetPositions(run, thisGlyphRange, &position);
                
                CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL);
                CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);
                CGPathAddPath(letters, &t, letter);
                CGPathRelease(letter);
            }
        }
        
        UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:letters];
        CGRect boundingBox = CGPathGetBoundingBox(letters);
        CGPathRelease(letters);
        CFRelease(line);
        
        
        [path applyTransform:CGAffineTransformMakeScale(1.0, -1.0)];
        [path applyTransform:CGAffineTransformMakeTranslation(0.0, boundingBox.size.height)];
        
        
        if (self.reversedAnimation) {
            return [[path bezierPathByReversingPath] CGPath];
        }
        
        return [path CGPath];
    }
  • 相关阅读:
    shi_tomasi特征点,GFTTDetector
    特征点总结(按features2d.hpp源码由上至下总结)
    经过一年时间的沉淀 再次回首 TCP Socket服务器编程 (二)
    构建嵌入式小型Linux系统
    老赵书托(3):深入理解计算机系统
    #define与typedef区别
    Linux下快速静态编译Qt以及Qt动态/静态版本共存
    tcpdump示例
    linux绑定多个ip(转载)
    Linux下eclipse及mysql安装,c++访问mysql数据库
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7064505.html
Copyright © 2011-2022 走看看