iOS直角矩形图片绘制
程序员文章站
2022-04-04 10:42:14
...
1、效果如下、本文仅展示黑色直角矩形背景图片绘制方式、文字为Label后来附加
2、使用自定义的UIImageView、绘制 image、通过贝塞尔曲线、四点连线填充颜色完成、
- 从(0,0) ---> (width,0) ---> (width-5,height) ---> (0,height) 四点、
- 连接上path、
- 填充上颜色、
- 获取上下文图片
3、实现代码
@implementation HSRectangeView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.image = [self getRectWithCorner:frame];
}
return self;
}
- (UIImage *)getRectWithCorner:(CGRect)frame {
UIImage *opaueImage;
UIGraphicsBeginImageContextWithOptions(frame.size,NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(frame.size.width, 0)];
[path addLineToPoint:CGPointMake(frame.size.width-5, frame.size.height)];
[path addLineToPoint:CGPointMake(0, frame.size.height)];
CGContextAddPath(context, path.CGPath);
[[UIColor blackColor] setFill];
CGContextFillPath(context);
opaueImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return opaueImage;
}
@end
4、调用方式
UIImageView *backView = [[HSRectangeView alloc]initWithFrame:CGRectMake(0, 10, 80, 20)];
[headImgView addSubview:backView];