How*_*ear 15 core-graphics drawrect ios ios5 ios6
我想在我的UIView的drawRect方法中绘制一条非常细的线条粗线.我看到的CGContextSetLineWidth值为0.5的行与用于绘制边框CALayer的相同1.0宽度值不匹配.
您可以看到两者之间的差异 - 红线(宽度= 1)比紫/蓝线(宽度= 0.5)薄得多.

这是我绘制伪1.0宽度水平线的方式:
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
CGContextSetLineWidth(ctx, 0.5); // I expected a very thin line
CGContextMoveToPoint(ctx, 0, y);
CGContextAddLineToPoint(ctx, self.bounds.size.width, y);
CGContextStrokePath(ctx);
Run Code Online (Sandbox Code Playgroud)
这是相同视图的边框,这次使用1.0边框宽度:
UIView *myView = (UIView *)self;
CALayer *layer = myView.layer;
layer.borderColor = [UIColor redColor].CGColor;
layer.borderWidth = 1.0;
Run Code Online (Sandbox Code Playgroud)
我需要做些什么来绘制我自己的自定义行,其宽度与CALayer版本相同?
rob*_*off 38
当您描边路径时,笔划会跨越路径.换句话说,路径位于笔划的中心.
如果路径沿着两个像素之间的边缘延伸,则笔划将(部分地)覆盖该边缘两侧的像素.线宽为0.5时,水平笔划将0.25个点延伸到路径上方的像素中,并将0.25个点延伸到路径下方的像素中.
您需要移动路径,使其不沿像素边缘运行:
CGFloat lineWidth = 0.5f;
CGContextSetLineWidth(ctx, lineWidth);
// Move the path down by half of the line width so it doesn't straddle pixels.
CGContextMoveToPoint(ctx, 0, y + lineWidth * 0.5f);
CGContextAddLineToPoint(ctx, self.bounds.size.width, y + lineWidth * 0.5f);
Run Code Online (Sandbox Code Playgroud)
但由于您只是绘制一条水平线,因此使用起来更简单CGContextFillRect:
CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
CGContextFillRect(ctx, CGRectMake(0, y, self.bounds.size.width, 0.5f));
Run Code Online (Sandbox Code Playgroud)
Con*_*nor 16
当不在积分上绘制时,您需要关闭抗锯齿以获得细线.
CGContextSetShouldAntialias(ctx, NO)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14376 次 |
| 最近记录: |