ios - 为 UITableViewCell 绘制虚线边框底部

标签 ios uitableview

我想在我的 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 上使用 moveToPointaddLineToPoint 仅用一行而不是一个矩形来构建路径:

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/

相关文章:

ios - 如何获取当前segue的标识符

ios - 用户使用滑动手势编辑时如何离开 UITableViewCell EditingStyle 删除模式

ios - fatal error : index out of range tableview with two sections

ios - 分组的 UITableView 和水平边距

Javascript 日期在 iOS 上无效

iOS 俄语可本地化字符串不起作用(但其他人可以)

ios - 在 ios/swift 中使用椭圆曲线加密来加密数据

ios - 在 iOS 上使用 Swift 迭代 NSArray 时崩溃

ios - 在uitableviewcell中异步加载图像

iphone - UITableView 单元格内的 UITextField