如何为NSView制作网格背景图案?

tun*_*una 4 cocoa objective-c

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

界面截图

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

tun*_*una 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)];
}

}
Run Code Online (Sandbox Code Playgroud)

格