使用CAShapeLayer绘制小人

上一篇讲解了CAShapeLayer的知识点和绘制圆角矩形,在这里实现一个小人的绘制,代码如下:

 UIView*bgv=[[UIView alloc]initWithFrame:self.view.bounds];

    bgv.backgroundColor=[UIColor lightGrayColor];

    [self.view addSubview:bgv];

      

    //绘制小人

     UIBezierPath*path=[[UIBezierPath alloc]init];

    [path addArcWithCenter:CGPointMake(200, 200) radius:50 startAngle:0 endAngle:2*M_PI clockwise:YES];

    [path moveToPoint:CGPointMake(100, 300)];

    [path addLineToPoint:CGPointMake(300, 300)];

    [path moveToPoint:CGPointMake(200, 250)];

    [path addLineToPoint:CGPointMake(200, 350)];

    [path addLineToPoint:CGPointMake(150, 400)];

    [path moveToPoint:CGPointMake(200, 350)];

    [path addLineToPoint:CGPointMake(250, 400)];

 

 

   CAShapeLayer*shapeLayer=[CAShapeLayer layer];

    shapeLayer.strokeColor=[UIColor redColor].CGColor;

    shapeLayer.fillColor=[UIColor clearColor].CGColor;

    shapeLayer.lineWidth=5;

    shapeLayer.path=path.CGPath;

    shapeLayer.lineJoin=kCALineJoinRound;

    shapeLayer.lineCap=kCALineCapRound;

    [bgv.layer addSublayer:shapeLayer];

 

最后介绍一下 使用CATextLayer绘制文本:

 CATextLayer*textLayer=[CATextLayer layer];

    textLayer.frame=CGRectMake(10, 400, self.view.bounds.size.width-20, self.view.bounds.size.height-400);

    [bgv.layer addSublayer:textLayer];

    

    textLayer.foregroundColor=[UIColor blackColor].CGColor;

    textLayer.alignmentMode=kCAAlignmentJustified;

    textLayer.wrapped=YES;

    

     NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing \ elit. Quisque massa arcu, eleifend vel varius in, facilisis pulvinar \ leo. Nunc quis nunc at mauris pharetra condimentum ut ac neque. Nunc elementum, libero ut porttitor dictum, diam odio congue lacus, vel \ fringilla sapien diam at purus. Etiam suscipit pretium nunc sit amet \ lobortis";

    textLayer.string=text;

    

    UIFont *font=[UIFont systemFontOfSize:15];

    CFStringRef fontName=(__bridge CFStringRef)(font.fontName);

    CGFontRef fontRef=CGFontCreateWithFontName(fontName);

    textLayer.font=fontRef;

    textLayer.fontSize=font.pointSize;

    CGFontRelease(fontRef);

 效果如下:

posted on 2015-12-08 18:14  envyhappy  阅读(173)  评论(0编辑  收藏  举报