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];
    }
  • 相关阅读:
    luogu 2478 [SDOI2010]城市规划 仙人掌上dp.
    bzoj 3782 上学路线 卢卡斯定理 容斥 中国剩余定理 dp
    bzoj 3790 神奇项链 回文串 manacher|PAM
    4.4 相交弧 容斥 平衡规划 二维数点
    4.4 省选模拟赛 拉格朗日计数 树状数组+容斥.
    4.4 省选模拟赛 修路 斯坦纳树
    带修改线性基
    CF R 630 div2 1332 F Independent Set
    4.3 省选模拟赛 石子游戏 树上博弈
    机器C盘临时区
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7064505.html
Copyright © 2011-2022 走看看