- (void)viewDidLoad {
[super viewDidLoad];
[self getWeekDay];
_lineImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 20)];
// 调用方法 返回的iamge就是虚线
_lineImg.image = [self drawLineByImageView:_lineImg];
// 添加到控制器的view上
[self.view addSubview:_lineImg];
_lineImg.backgroundColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor grayColor];
}
// 返回虚线image的方法
- (UIImage *)drawLineByImageView:(UIImageView *)imageView{
UIGraphicsBeginImageContext(imageView.frame.size); //开始画线 划线的frame
[imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
//设置线条终点形状
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
// 5是每个虚线的长度 4是两个虚线之间的间隔距离
CGFloat length[] = {5,4};
// 或者
// CGFloat length[] = {3 10,20,15}; //3和20是虚线的长度,10和15 是间距的长度,
CGContextRef line = UIGraphicsGetCurrentContext();
// 设置颜色
CGContextSetStrokeColorWithColor(line, [UIColor redColor].CGColor);
CGContextSetLineDash(line, 0, length, 2); //画虚线 2是length数组的元素个数
CGContextMoveToPoint(line, 0.0, 10.0); //开始画线
CGContextAddLineToPoint(line, kScreenWidth , 10.0);
CGContextStrokePath(line);
// UIGraphicsGetImageFromCurrentImageContext()返回的就是image
return UIGraphicsGetImageFromCurrentImageContext();
}