2

我试图制作一个边缘看起来像下图的 UILabel。

在此处输入图像描述

这是drawRect:来自我的UILabel子类的。

-(void)drawRect:(CGRect)rect{

        [super drawRect:rect];
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);
        CGRect labelRect = self.bounds;

        CGPoint bottomPoint = CGPointMake(labelRect.size.width, rect.size.height);
        CGPoint topMinus30Point =CGPointMake(labelRect.size.width-30, 0);
        CGPoint topPoint = CGPointMake(labelRect.size.width, 0);

        CGContextBeginPath (context);
        //from bottom to -30 top
        CGContextMoveToPoint(context, bottomPoint.x,bottomPoint.y);
        CGContextAddLineToPoint(context, topMinus30Point.x,topMinus30Point.y);

        //from -30 to top
        CGContextMoveToPoint(context, topMinus30Point.x,topMinus30Point.y);
        CGContextAddLineToPoint(context, topPoint.x,topPoint.y);

        //from top to bottom
        CGContextMoveToPoint(context, topPoint.x,topPoint.y);
        CGContextAddLineToPoint(context, bottomPoint.x,bottomPoint.y);
        CGContextClosePath(context);

        CGContextStrokePath(context);
        CGContextRestoreGState(context);

}

如何从当前帧裁剪创建的路径?
[我刚刚开始使用 Core Graphics,所以请温柔 :)]

关于如何实现这一点的任何指示都会非常有帮助。
谢谢

4

2 回答 2

2

尝试这个:

- (void)drawRect:(CGRect)rect
{

    CGContextRef context = UIGraphicsGetCurrentContext();

    // clear drawing rect
    CGContextClearRect(context, rect);

    // save 1
    CGContextSaveGState(context);

    CGRect labelRect = self.bounds;
    CGPoint bottomPoint = CGPointMake(labelRect.size.width, rect.size.height);
    CGPoint topMinus30Point = CGPointMake(labelRect.size.width-30, 0);
    CGPoint topPoint = CGPointMake(labelRect.size.width, 0);

    CGContextBeginPath(context);

    CGContextMoveToPoint(context, 0.0f, 0.0f);
    CGContextAddLineToPoint(context, topMinus30Point.x, topMinus30Point.y);
    CGContextAddLineToPoint(context, bottomPoint.x, bottomPoint.y);
    CGContextAddLineToPoint(context, 0.0f, bottomPoint.y);
    CGContextAddLineToPoint(context, 0.0f, 0.0f);

    CGContextClosePath(context);

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextDrawPath(context, kCGPathFill);

    // restore 1
    CGContextRestoreGState(context);

}

把它放在你使用的 init 中:

[self setBackgroundColor:[UIColor clearColor]];
于 2013-09-02T12:24:13.677 回答
0

一种方法是,

将标签的背景颜色设置为清除颜色,并使用 CGContext 在 drawRect 方法中绘制所需的形状。

你的 drawRect 会变成这样

- (void)drawRect:(CGRect)rect{

[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect labelRect = self.bounds;


CGPoint bottomPoint = CGPointMake(labelRect.size.width, rect.size.height);
CGPoint topMinus30Point =CGPointMake(labelRect.size.width-30, 0);

//from bottom to -30 top

CGContextMoveToPoint(context, self.bounds.origin.x,self.bounds.origin.y);
CGContextAddLineToPoint(context, topMinus30Point.x,topMinus30Point.y);
CGContextAddLineToPoint(context, bottomPoint.x,bottomPoint.y);
CGContextAddLineToPoint(context, self.bounds.origin.x,self.bounds.origin.x + self.bounds.size.height);
CGContextAddLineToPoint(context, self.bounds.origin.x,self.bounds.origin.y);


CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextFillPath(context);

}

另一种方法是使用CGContextClip 这种方法你的drawRect会变成这样

CGContextMoveToPoint(context, self.bounds.origin.x,self.bounds.origin.y);
CGContextAddLineToPoint(context, topMinus30Point.x,topMinus30Point.y);
CGContextAddLineToPoint(context, bottomPoint.x,bottomPoint.y);
CGContextAddLineToPoint(context, self.bounds.origin.x,self.bounds.origin.x + self.bounds.size.height);
CGContextAddLineToPoint(context, self.bounds.origin.x,self.bounds.origin.y);
CGContextClip(context);

CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextFillRect(context, self.bounds);

希望这可以帮助

于 2013-09-02T12:27:59.720 回答