使用CAShapeLayer绘制小人

本文通过UIBezierPath和CAShapeLayer实现了一个小人的绘制,并介绍了如何使用CATextLayer来显示多行文本。

上一篇讲解了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);

 效果如下:

转载于:https://www.cnblogs.com/envyhappy/p/5029946.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值