zoukankan      html  css  js  c++  java
  • iOS之String动态书写

    /**
     String动画书写出来
    
     @param string 要写的字
     @param view 父视图
     @param ui_font 字体大小
     @param color 字体颜色
     */
    - (void)createAnimationLayerWithString:(NSString*)string andView:(UIView *)view andFont:(UIFont*)ui_font andStrokeColor:(UIColor*)color{
        CTFontRef font =CTFontCreateWithName((CFStringRef)ui_font.fontName,
                                             ui_font.pointSize,
                                             NULL);
        CGMutablePathRef letters = CGPathCreateMutable();
        
        //这里设置画线的字体和大小
        NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                               (__bridge id)font, kCTFontAttributeName,
                               nil];
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:string
                                                                         attributes:attrs];
        CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
        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);
            }
        }
        
        CAShapeLayer *pathLayer = [CAShapeLayer layer];
        pathLayer.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height );
        pathLayer.bounds = CGPathGetBoundingBox(letters);
        pathLayer.geometryFlipped = YES;
        pathLayer.path = letters;
        pathLayer.strokeColor = [color CGColor];
        pathLayer.fillColor = nil;
        pathLayer.lineWidth = 1.0f;
        pathLayer.lineJoin = kCALineJoinBevel;
        [view.layer addSublayer:pathLayer];
        
        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration = 5.0;
        pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
        pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
        [pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
        
        CGPathRelease(letters);
        CFRelease(font);
        CFRelease(line);
    }

    效果图

  • 相关阅读:
    轻松实现WCF服务的构造函数依赖注入
    终于找到在Visual Studio 2010中进行“项目重命名”的有效工具
    让Entity Framework不再私闯sys.databases
    AutoMapper使用笔记
    遭遇IE8下的JavaScript兼容问题
    WCF异步调用中客户端关闭带来的性能问题
    Chrome “False Start” 引起的 Error 7 (net::ERR_TIMED_OUT): The operation timed out
    实战ASP.NET访问共享文件夹(含详细操作步骤)
    Entity Framework 理清关系 基于外键关联的单向一对一关系
    在Firefox中通过JavaScript复制到剪贴板(Copy to Clipboard)
  • 原文地址:https://www.cnblogs.com/xianfeng-zhang/p/7699499.html
Copyright © 2011-2022 走看看