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 に答える