如何在UIView.
UIView
像这样的东西
您可以使用图层和贝塞尔路径设置此图案的边框,如下例所示。
Objective-C
CAShapeLayer *yourViewBorder = [CAShapeLayer layer]; yourViewBorder.strokeColor = [UIColor blackColor].CGColor; yourViewBorder.fillColor = nil; yourViewBorder.lineDashPattern = @[@2, @2]; yourViewBorder.frame = yourView.bounds; yourViewBorder.path = [UIBezierPath bezierPathWithRect:yourView.bounds].CGPath; [yourView.layer addSublayer:yourViewBorder];
斯威夫特 3.1
var yourViewBorder = CAShapeLayer() yourViewBorder.strokeColor = UIColor.black.cgColor yourViewBorder.lineDashPattern = [2, 2] yourViewBorder.frame = yourView.bounds yourViewBorder.fillColor = nil yourViewBorder.path = UIBezierPath(rect: yourView.bounds).cgPath yourView.layer.addSublayer(yourViewBorder)
您还可以使用图案图像设置不同类型的设计,如下例所示。
[yourView.layer setBorderWidth:5.0]; [yourView.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"DotedImage.png"]] CGColor]];///just add image name and create image with dashed or doted drawing and add here
在这里,您必须<QuartzCore/QuartzCore>在项目中添加框架并在YourViewController.m文件中使用以下行将其导入。
<QuartzCore/QuartzCore>
YourViewController.m
#import <QuartzCore/QuartzCore.h>