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];
}
实现效果如下:
转载请注明出处,谢谢!