Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a grid background pattern for NSView?

is there an easy way to get this grid background? Or do I have to do it something like this [NSColor colorWithPatternImage:[NSImage ...]]?

Screenshot of interface

I don't want complete code. I just want to know if there is an easy way to do that and if yes how.

like image 618
tuna Avatar asked Dec 05 '22 11:12

tuna


1 Answers

This is my solution:

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

}

Grid

like image 99
tuna Avatar answered Dec 25 '22 03:12

tuna