[iOS] UILabel层,为什么不满?

时间:2015-07-15 06:49:24

标签: ios uilabel calayer

我定制了一个BWLabel来绘制标签的边框。

- (void)drawRect:(CGRect)rect {
    CGRect frame = self.frame;
    UIBezierPath *linePath = [UIBezierPath bezierPath];
    [linePath moveToPoint:CGPointMake(0,0)];
    [linePath addLineToPoint:CGPointMake(frame.size.width, 0)];
    [linePath addLineToPoint:CGPointMake(frame.size.width, frame.size.height)];
    [linePath addLineToPoint:CGPointMake(0, frame.size.height)];

    CAShapeLayer *lineLayer = [CAShapeLayer layer];
    lineLayer.lineWidth = 3.0f;
    lineLayer.strokeColor = [[UIColor whiteColor] CGColor];
    lineLayer.path = linePath.CGPath;

    [self.layer addSublayer:lineLayer];
}

我使用Debug View Hierarchy来查看BWLabel。 enter image description here

我有一个问题,为什么CAShapeLayer不满?

1 个答案:

答案 0 :(得分:0)

你的绘制矩形应该是这样的

- (void)drawRect:(CGRect)rect
{
     CGRect frame = self.frame;
    UIBezierPath *linePath = [UIBezierPath bezierPath];
    [linePath moveToPoint:CGPointMake(0,0)];
    [linePath addLineToPoint:CGPointMake(frame.size.width, 0)];
    [linePath addLineToPoint:CGPointMake(frame.size.width, frame.size.height)];
    [linePath addLineToPoint:CGPointMake(0, frame.size.height)];
    [linePath addLineToPoint:CGPointMake(0, 0)];

    CAShapeLayer *lineLayer = [CAShapeLayer layer];
    lineLayer.lineWidth = 3.0f;
    lineLayer.strokeColor = [[UIColor whiteColor] CGColor];
    lineLayer.path = linePath.CGPath;

    [self.layer addSublayer:lineLayer];    
}