利用CAShapeLayer和UIBezierPath结合画梯形图

1.在ViewController.m 中 初始化 画的图形的背景View view2

// 初始化父视图
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 100, 25)];// 坐标可以自行修改
    [self.view addSubview:view];
    [ViewController drawBackViewWithView:view BackColor:[UIColor colorWithRed:0.968 green:0.389 blue:0.037 alpha:1.000] LabelText:@"发信息"];//LabelText 是显示的文字
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(100, 300, 100, 25)];// 坐标可以自行修改
    [self.view addSubview:view2];
    [ViewController drawBackViewWithView:view2 BackColor:[UIColor colorWithRed:0.183 green:0.801 blue:0.931 alpha:1.000] LabelText:@"打电话"];


2.在ViewController.m 中  实现单利画图方法

#pragma mark 画不规则矩形(梯形)背景 部分尺寸大小可自行修改
+ (void)drawBackViewWithView:(UIView *)view BackColor:(UIColor *) color LabelText:(NSString *)string
{
    CGSize finalSize = CGSizeMake(CGRectGetWidth(view.bounds), CGRectGetHeight(view.bounds));
    CGFloat layerHeight = finalSize.height;
    CAShapeLayer *layer = [CAShapeLayer layer];
    UIBezierPath *bezier = [UIBezierPath bezierPath];
    [bezier moveToPoint:CGPointMake(0, finalSize.height - layerHeight)];
    [bezier addLineToPoint:CGPointMake(0, finalSize.height-1)];
    [bezier addLineToPoint:CGPointMake(finalSize.width, finalSize.height - 1)];
    [bezier addLineToPoint:CGPointMake(finalSize.width-7, finalSize.height - layerHeight)];
    [bezier addLineToPoint:CGPointMake(0,0)];
    layer.path = bezier.CGPath;
    layer.fillColor = color.CGColor;
    [view.layer addSublayer:layer];
    UILabel *labe = [[UILabel alloc]initWithFrame:CGRectMake(0, 2.5, CGRectGetWidth(view.bounds)-7, 15)];
    labe.text = string;
    labe.textColor = [UIColor whiteColor];
    labe.textAlignment = NSTextAlignmentCenter;
    labe.font = [UIFont systemFontOfSize:15];
    [view addSubview:labe];
}


实现效果如下:


转载请注明出处,谢谢!


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

启程Boy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值