【问题标题】:How to set UILabel non rectangular frame如何设置UILabel非矩形框
【发布时间】:2013-09-02 11:49:00
【问题描述】:

我正在尝试制作一个 UILabel,其边缘如下图所示。

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

-(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,所以请温柔:)]

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

【问题讨论】:

  • 你正在寻找的愚蠢的东西......你可以通过这样的方式来设置背景图像,该图像具有该切角,并在该图像视图上方添加 UILable 与您想要的文本......那里无法实现您的要求
  • 我总是可以用图像做到这一点,但这是我核心图形研究的一部分。
  • 你想让裁剪的部分透明吗?

标签: iphone ios objective-c cocoa-touch core-graphics


【解决方案1】:

试试这个:

- (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]];

【讨论】:

  • 谢谢 joao,正是我想要的。
  • 还有,[super drawRect:rect];
  • 确实,你需要它来绘制文本 :) 很高兴它有帮助
【解决方案2】:

一种方法是,

将标签的背景颜色设置为清除颜色,并使用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);

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2019-05-16
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    相关资源
    最近更新 更多