将String转换为其表示的路径画到屏幕上

关于这个问题,我已经在另一篇blog中有所提及:

CoreText精彩文字轮廓绘制动画的一点改进

不过原有的转换代码使用Obj-C写的,在这里我们尝试将其转换为Swift语言,然后利用它实现一个测试小程序.

首先贴出原来Objc的代码:

- (void) setupTextLayer
{
    if (self.pathLayer != nil) {
        [self.penLayer removeFromSuperlayer];
        [self.pathLayer removeFromSuperlayer];
        self.pathLayer = nil;
        self.penLayer = nil;
    }

    // Create path from text
    // See: http://www.codeproject.com/KB/iPhone/Glyph.aspx
    // License: The Code Project Open License (CPOL) 1.02 http://www.codeproject.com/info/cpol10.aspx
    CGMutablePathRef letters = CGPathCreateMutable();

    CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 72.0f, NULL);
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                           (id)font, kCTFontAttributeName,
                           nil];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"你好,大熊猫侯佩!"
    //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);

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointZero];
    [path appendPath:[UIBezierPath bezierPathWithCGPath:letters]];

    CGPathRelease(letters);
    CFRelease(font);

    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.frame = self.animationLayer.bounds;
    pathLayer.bounds = CGPathGetBoundingBox(path.CGPath);
    //pathLayer.backgroundColor = [[UIColor yellowColor] CGColor];
    pathLayer.geometryFlipped = YES;
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = [[UIColor blackColor] CGColor];
    pathLayer.fillColor = nil;
    pathLayer.lineWidth = 5.0f;
    //pathLayer.lineJoin = kCALineJoinBevel;
    pathLayer.lineJoin = kCALineJoinMiter;

    [self.animationLayer addSublayer:pathLayer];

    self.pathLayer = pathLayer;

    //happy commit
    NSLog(@"hello world!!!");

    //UIImage *penImage = [UIImage imageNamed:@"noun_project_347_2.png"];
    UIImage *penImage = [UIImage imageNamed:@"bee.png"];
    CALayer *penLayer = [CALayer layer];
    penLayer.contents = (id)penImage.CGImage;
    penLayer.anchorPoint = CGPointZero;
    penLayer.frame = CGRectMake(0.0f, 0.0f, penImage.size.width/5, penImage.size.height/5);
    [pathLayer addSublayer:penLayer];

    self.penLayer = penLayer;
}

看起来颇长啊!不过不要太在意,因为我们要用Swift重写的代码只提取其中中间的一部分,这样可以更好的重用.

新建一个项目,基于Swift语言.

在项目中新建一个Swift源代码文件,该文件扩展了String类,我们在其扩展中先写一个帮助方法的存根:

extension String{
    func toPath(font:CTFont)->CGPath{

    }
}

toPath方法用来实现任意String实例到CGPath路径的转换,在其中添加如下内容:

let letters:CGMutablePathRef = CGPathCreateMutable()
        let attrs = [kCTFontAttributeName as String:font]
        let attrString:NSAttributedString = NSAttributedString(string: self, attributes: attrs)

        let line:CTLine = CTLineCreateWithAttributedString(attrString)
        let runArray = CTLineGetGlyphRuns(line)

        for runIndex in 0..<CFArrayGetCount(runArray){
            let run = CFArrayGetValueAtIndex(runArray, runIndex)
            let runb = unsafeBitCast(run, CTRun.self)
            //let runFont:CTFont = CFDictionaryGetValue(CTRunGetAttributes(runb), kCTFontAttributeName as String) as! CTFont
            let CTFontName = unsafeBitCast(kCTFontAttributeName, UnsafePointer<Void>.self)
            let runFontC = CFDictionaryGetValue(CTRunGetAttributes(runb), CTFontName)
            let runFont = unsafeBitCast(runFontC, CTFont.self)

            //for each GLYPH in run
            for runGlyphIndex in 0..<CTRunGetGlyphCount(runb){
                //get Glyph & Glyph-data
                let glyphRange = CFRange(location: runGlyphIndex, length: 1)
                //let glyph:UnsafeMutablePointer<CGGlyph> = UnsafeMutablePointer<CGGlyph>.alloc(1)
                //glyph.initialize(0)
                var glyph:CGGlyph = 0
                let position:UnsafeMutablePointer<CGPoint> = UnsafeMutablePointer<CGPoint>.alloc(1)
                position.initialize(CGPoint.zero)
                CTRunGetGlyphs(runb, glyphRange, &glyph)
                CTRunGetPositions(runb, glyphRange, position)

                //Get PATH of outline
                //let letter = CTFontCreatePathForGlyph(runFont, glyph.memory, nil)
                let letter = CTFontCreatePathForGlyph(runFont, glyph, nil)
                var t = CGAffineTransformMakeTranslation(position.memory.x, position.memory.y)
                //let tx:UnsafeMutablePointer<CGAffineTransform> = UnsafeMutablePointer<CGAffineTransform>.alloc(1)
                //tx.initialize(t)
                CGPathAddPath(letters, &t, letter)
                //CGPathRelease(letter)
                position.destroy()
                position.dealloc(1)
            }
        }

        let path = UIBezierPath()
        path.moveToPoint(CGPoint.zero)
        path.appendPath(UIBezierPath(CGPath: letters))
        return path.CGPath

大家可以对照原来的Obj-c版本看一下,大致都是一一对应的,只有少数几个涉及操作C语言数据的地方有修改,大家可以参考我写的另一篇blog:

Swift中如何转换不同类型的Mutable指针

核心功能有了,下面就好办了!我们想要的是点击屏幕开始显示动画,于是重载如下方法:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if isAnimating { return }

        isAnimating = true
        flyerLayer.opacity = 0.8

        pathLayer.removeAllAnimations()
        flyerLayer.removeAllAnimations()

        let strokeAnimation = CABasicAnimation(keyPath: "strokeEnd")
        strokeAnimation.duration = 20.0
        strokeAnimation.fromValue = 0.0
        strokeAnimation.toValue = 1.0
        strokeAnimation.delegate = self
        pathLayer.addAnimation(strokeAnimation, forKey: nil)

        let flyAnimation = CAKeyframeAnimation(keyPath: "position")
        flyAnimation.duration = 20.0
        flyAnimation.path = pathLayer.path
        flyAnimation.calculationMode = kCAAnimationPaced

        flyerLayer.addAnimation(flyAnimation, forKey: nil)
    }

下面是App实际运行的效果:

时间: 2024-12-29 04:11:12

将String转换为其表示的路径画到屏幕上的相关文章

【原创】利用typeface实现不同字体的调用显示及String转换为Unicode

最近工作用到,就写个小demo demo实现从assets中利用typeface调用不同字体,并在editText中显示出来 1.layout中创建activity_main.xml文件 布局代码如下: 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 andr

android 将图片通过base64转换为String 将图片String转换为Bitmap

1.Bitmap转换为图片字符串 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //该方法用来压缩图片,第一个参数为图片格式,第二个参数为截取图片的保留率,如当前为90,则保留之前图片90%的区域 bitmap.compress(Bitmap

C#中使用Buffer.BlockCopy()方法将string转换为byte array的方法:

public static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count); 将指定数目的字节从起始于特定偏移量的源数组复制到起始于特定偏移量的目标数组. /// <summary> /// C#中使用Buffer.BlockCopy()方法将string转换为byte array的方法 /// </summary> /// <param name="str&

将String转换为Double并保留2位小数

? 1 2 3 4 5 6 7 8 9 10 11 12 13     //将money的分转化为元 public String coinToYuan(String coin) {     Double dd = Double.parseDouble(coin);     Double ddd = dd / 100;     if (dd % 100 == 0) {         int num = ddd.intValue();         return String.valueOf(n

(转)第04节:Fabric.js用路径画不规则图形

在Canvas上画方形.圆形.三角形都是很容易的,只要调用fabric对应的方法就可以了,但这些都是规则的图形,如果你想画一个不规则的图形,这时候你可以用fabric.js提供的路径绘图方法.所谓路径绘图就是用点和线的移动的方式进行绘图.通过对 线.曲线.弧的应用你可以非常复杂的图形. 我们先来看一段的代码: var canvas = new fabric.Canvas('canvas'); var path = new fabric.Path('M 0 0 L 200 100 L 170 20

dex2oat将dex转换为oat的执行路径概览

dex2oat是一个可执行文件,在源码中通过编译文件art\dex2oat\Dex2oat.cc生成. dex2oat的执行入口是: int main(int argc, char** argv) { return art::dex2oat(argc, argv); } 在函数dex2oat中调用了dex2oat->CreateOatFile函数,在CreateOatFile函数中执行了driver->CompileAll(class_loader, dex_files, timings);语

string转换为char*

把string转换为char* 有3中方法: 1.data() 1 string str="abc"; 2 char *p=(char*)str.data(); 2. c_str() 1 string str="gdfd"; 2 const char *p=str.c_str(); 3.copy() 1 string str="hello"; 2 char p[40]; 3 str.copy(p,5,0); //这里5,代表复制几个字符,0代表复

Java:String转换为date类型

public static Date stringToDate(String str) { DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { // Fri Feb 24 00:00:00 CST 2012 date = format.parse(str); } catch (ParseException e) { e.printStackTrace(); } // 2012-0

string转换为decimal

public decimal Change_StrToDecimal(string str) { str = str.Trim(); decimal value; str = Server.HtmlDecode(str); if (str.Trim() == "" || str.Trim() == " ") { value = 0; } else { if (str.Substring(0, 1) == "¥" || str.Substring(