【问题标题】:Core graphics draw rectangle with border in one line核心图形在一行中绘制带边框的矩形
【发布时间】:2012-12-04 19:13:24
【问题描述】:

如何在一行中绘制一个有边框的矩形?

有不同的方法,例如:

CGContextStrokeRect(context, someRectangle);

CGContextFillRect(context, someRectangle);

但是有什么东西可以同时做吗?

【问题讨论】:

    标签: objective-c ios core-graphics


    【解决方案1】:
    - (void)drawRect:(CGRect)rect {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGPathRef path = CGPathCreateWithRect(rect, NULL);
        [[UIColor redColor] setFill];
        [[UIColor greenColor] setStroke];
        CGContextAddPath(context, path);
        CGContextDrawPath(context, kCGPathFillStroke);
        CGPathRelease(path);
    }
    

    虽然,我不能说它比中风和填写单独的调用更冗长......

    【讨论】:

    • 嗯...非常正确,我想我会单独调用。谢谢。
    【解决方案2】:

    如果您只是想节省行空间,您可以定义自己的方法来进行两次调用并将其放在实用程序类中。

    void strokeAndFill(CGContextRef c, CGRect rect)
    {
        CGContextFillRect(c, rect);
        CGContextStrokeRect(c, rect);
    }
    

    【讨论】:

      【解决方案3】:

      如果你事先设置了填充和描边颜色,CGContextDrawPath 会一劳永逸。

      CGContextRef context = UIGraphicsGetCurrentContext();
      CGContextSetStrokeColor(context, a);
      CGContextSetFillColor(context, b);
      CGContextDrawPath(context, rect)
      

      【讨论】:

      • 嗯……我在头文件中看到CGContextDrawPath的签名是:CGContextDrawPath(CGContextRef c, CGPathDrawingMode mode)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 2017-10-31
      相关资源
      最近更新 更多