我想在我的 UITableViewCell
中添加一个虚线底部边框。
目前,我正在使用以下代码,
CAShapeLayer *border = [CAShapeLayer layer];
border.strokeColor = [UIColor colorWithRed:67/255.0f green:37/255.0f blue:83/255.0f alpha:1].CGColor;
border.fillColor = nil;
border.lineDashPattern = @[@1, @1];
border.path = [UIBezierPath bezierPathWithRect:cell.bounds].CGPath;
border.frame = CGRectMake(cell.bounds.origin.x, cell.bounds.origin.y + cell.bounds.size.height, cell.bounds.size.width, 1);
[cell.layer addSublayer:border];
使用这段代码,我的单元格底部有一个虚线边框,但虚线边框的高度大了两倍。
但我不是很擅长操作 CAShapeLayer
,而且我没有找到任何可以帮助我的东西。
谢谢!
最佳答案
如果是笔触太宽,请尝试更改图层的 lineWidth
。像这样的东西就可以了:
border.lineWidth = 1. / [[UIScreen mainScreen] scale];
这将在非 Retina 设备上绘制一条 1px 的线,在 Retina 设备上绘制一条 1px 的线(0.5pt),从而产生超清晰的线。
此外,您可以在 UIBezierPath
上使用 moveToPoint
和 addLineToPoint
仅用一行而不是一个矩形来构建路径:
UIBezierPath *bPath = [UIBezierPath new];
[bPath moveToPoint:CGPointZero];
[bPath addLineToPoint:(CGPoint){100, 0}];
border.path = bPath.CGPath;
根据需要进行调整。
关于ios - 为 UITableViewCell 绘制虚线边框底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21854863/