繁体   English   中英

我对如何绘制空心圆角的矩形感到困惑。 我怎样才能做到这一点?

[英]I'm pretty confused about how to draw a hollow and rounded rectangle. How can I do this?

我在Custom TableViewCell上有一个小视图。

UIView的宽度大约为120,高度大约为30。 我想绘制一个带有圆角的空心矩形,以基本上占据整个UIView,然后在该UIView中也将包含一些子视图,例如小的20x20 UIImageView和20height UILabel。

我该如何绘制?

我可以通过新的UIView图层绘制一个实心矩形,但是我猜那不是做到这一点的方法,我应该使用QuartzCore吗?

我在下面尝试了此代码,但不确定如何将其添加到UIVIew?

CGRect rectangle = CGRectMake(0, 0, 120, 22);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rectangle);

我也尝试过此代码,但没有任何显示。

CAShapeLayer *rectLayer = [CAShapeLayer layer];
[rectLayer setBounds:CGRectMake(0.0, 0.0, 120.0, 22.0)];
[rectLayer setPosition:CGPointMake(0.0, 0.0)];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0.0, 0.0, 20.0, 20.0)];
[rectLayer setPath:[path CGPath]];
[rectLayer setStrokeColor:[[UIColor blackColor] CGColor]];
[rectLayer setLineWidth:3.0];

[[self.respondView layer] addSublayer:rectLayer];

我该如何实现?

您可以简单地使用带有边框颜色的角半径

yourview.layer.cornerRadius = 5;
yourview.layer.masksToBounds = YES;
yourview.layer.borderColor = [UIColor blueColor].CGColor;
yourview.layer.borderWidth = 1.0;

截图

在此处输入图片说明

我不确定这是否是您想要的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM