4

有没有一种简单的方法来获得这个网格背景?还是我必须这样做[NSColor colorWithPatternImage:[NSImage ...]]

界面截图

我不想要完整的代码。我只是想知道是否有一种简单的方法可以做到这一点,如果是的话如何。

4

6 回答 6

7

这是我的解决方案:

- (void)drawRect:(NSRect)dirtyRect
{
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
[[NSColor whiteColor] setFill];
CGContextFillRect(context, dirtyRect);

for (int i = 1; i < [self bounds].size.height / 10; i++) {
    if (i % 10 == 0) {
        [[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.3] set];
    } else if (i % 5 == 0) {
        [[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.2] set];
    } else {
        [[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.1] set];
    }
    [NSBezierPath strokeLineFromPoint:NSMakePoint(0, i * 10 - 0.5) toPoint:NSMakePoint([self bounds].size.width, i * 10 - 0.5)];
}
for (int i = 1; i < [self bounds].size.width / 10; i++) {
    if (i % 10 == 0) {
        [[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.3] set];
    } else if (i % 5 == 0) {
        [[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.2] set];
    } else {
        [[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.1] set];
    }
    [NSBezierPath strokeLineFromPoint:NSMakePoint(i * 10 - 0.5, 0) toPoint:NSMakePoint(i * 10 - 0.5, [self bounds].size.height)];
}

}

网格

于 2013-04-19T22:52:52.913 回答
4

相同,但对于Swift 4

override func draw(_ dirtyRect: NSRect) {
    super.draw(dirtyRect)

    if let context = NSGraphicsContext.current?.cgContext {
        NSColor.white.setFill()
        context.fill(dirtyRect)
        context.flush()
    }

    for i in 1...(Int(self.bounds.size.height) / 10) {
        if i % 10 == 0 {
            NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.3).set()
        }else if i % 5 == 0 {
            NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.2).set()
        }else{
            NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.1).set()
        }            
        NSBezierPath.strokeLine(from: CGPoint(x: 0, y: CGFloat(i) * 10 - 0.5), to: CGPoint(x: self.bounds.size.width, y: CGFloat(i) * 10 - 0.5))
    }

    for i in 1...(Int(self.bounds.size.width) / 10) {
        if i % 10 == 0 {
            NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.3).set()
        }else if i % 5 == 0 {
            NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.2).set()
        }else{
            NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.1).set()
        }
        NSBezierPath.strokeLine(from: CGPoint(x: CGFloat(i) * 10 - 0.5, y:0), to: CGPoint(x: CGFloat(i) * 10 - 0.5, y: self.bounds.size.height))
    }
}
于 2017-06-11T11:13:59.043 回答
2

我认为图案颜色在这里不是一个好的解决方案,特别是因为您需要改变线条。而是对每条水平和垂直线使用 NSBezierPath 和 moveToPoint/lineToPoint 对。然后,您可以一次调用绘制网格。对那些具有不同颜色(alpha)和/或宽度的线条执行额外的步骤(即不要将较粗的线条添加到主网格路径,而是为它们创建一个单独的线条)。

于 2013-04-05T07:50:02.493 回答
1

只需查看每个 Xcode 安装附带的 Sketch 示例,也可以单独下载。

它具有网格实现(以及许多其他有用的 Cocoa 技术演示......)允许缩放等。

于 2013-04-05T07:50:40.883 回答
1

与“悲伤的金枪鱼”相同,但在Swift 3中

override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)
    if let context = NSGraphicsContext.currentContext()?.CGContext {
        NSColor.whiteColor().setFill()
        CGContextFillRect(context, dirtyRect)
        CGContextFlush(context)
    }

    for i in 1...(Int(self.bounds.size.height) / 10) {
        if i % 10 == 0 {
            NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.3).set()
        }else if i % 5 == 0 {
            NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.2).set()
        }else{
            NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.1).set()
        }

        NSBezierPath.strokeLineFromPoint(CGPointMake(0, CGFloat(i) * 10 - 0.5), toPoint: CGPointMake(self.bounds.size.width, CGFloat(i) * 10 - 0.5))
    }

    for i in 1...(Int(self.bounds.size.width) / 10) {
        if i % 10 == 0 {
            NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.3).set()
        }else if i % 5 == 0 {
            NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.2).set()
        }else{
            NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.1).set()
        }

        NSBezierPath.strokeLineFromPoint(CGPointMake(CGFloat(i) * 10 - 0.5, 0), toPoint: CGPointMake(CGFloat(i) * 10 - 0.5, self.bounds.size.height))
    }
}
于 2016-08-24T15:13:57.453 回答
1

在 MacOS 10.10 及更高版本中移植甲醇的解决方案以快速解决错误。

可以使用一个简单的静态函数并从 drawRect() 调用

static func makeGridBackground(dirtyRect: NSRect, view: NSView){
    //view.print("WMEditorUtils: initiated drawing")

    //Fill background with white color
    if let context = NSGraphicsContext.currentContext()?.CGContext {
        NSColor.whiteColor().setFill()
        CGContextFillRect(context, dirtyRect)
        CGContextFlush(context)
    }

    //Draw Lines: Horizontal
    for var i:Int = 1; i < (Int)(view.bounds.size.height / 10); i++ {
        if (i % 10 == 0) {
            NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.3).set()
        }
        else if (i % 5 == 0) {
            NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.2).set()
        }
        else{
            NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.1).set()
        }
        NSBezierPath.strokeLineFromPoint(NSMakePoint(0, (CGFloat)(i * 10) - 0.5), toPoint: NSMakePoint(view.bounds.size.width, (CGFloat)(i * 10) - 0.5))
    }

    //Draw Lines: Vertical
    for var i:Int = 1; i < (Int)(view.bounds.size.width / 10); i++ {
        if (i % 10 == 0) {
            NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.3).set()
        }
        else if (i % 5 == 0) {
            NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.2).set()
        }
        else{
            NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.1).set()
        }
        NSBezierPath.strokeLineFromPoint(NSMakePoint((CGFloat)(i * 10) - 0.5, 0), toPoint: NSMakePoint((CGFloat)(i * 10) - 0.5, view.bounds.size.width))
    }

}
于 2015-10-26T06:46:23.303 回答