• 由文字生成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];
    }
  • 相关阅读:
    PHP实现对站点内容外部链接的过滤方法
    PHP常用技术文之文件操作和目录操作总结
    PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享
    PHP如何快速读取大文件
    php使用json_encode后出现中文乱码的解决方法
    redis在PHP中的基本使用
    等差数列的概念和性质
    构造数列中的常见变形总结
    用几何画板制作函数图像的动态伸缩效果
    用几何画板制作函数图像的动态平移效果
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7064505.html
Copyright © 2020-2023  润新知