需求如上图。
代码如下
//充值 UIButton *rechargeButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 80, 30)]; [rechargeButton setTitle:@"充值" forState:UIControlStateNormal]; [rechargeButton.titleLabel setFont:[UIFont systemFontOfSize:13]]; rechargeButton.backgroundColor = rgb(11, 157, 224); [bottomView addSubview:rechargeButton]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; CGRect rect = rechargeButton.bounds; CGSize radii = CGSizeMake(15, 15); UIRectCorner corners = UIRectCornerTopLeft | UIRectCornerBottomLeft; UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii]; maskLayer.path = path.CGPath; rechargeButton.layer.mask = maskLayer;
这样我们就可以根据路径,来裁剪到我们想要的形状。