Is it possible to have a pattern (say a grid) in a view that can be repeated x or y or both? Kind of like css:
background-image: url(pattern.png); background-re开发者_StackOverflowpeat: repeat; //or repeat-x, repeat-y, no repeat;
The following code will tile a background image:
- (void) viewDidLoad {
[[self view] setBackgroundColor: [[UIColor alloc] initWithPatternImage: [UIImage imageNamed: @"background.png"]]];
[super viewDidLoad];
}
Yes. You can load an image pattern as a color ([NSColor withPatternImage:[NSImage imageNamed:@"pattern"]]
) and then draw it as you would a regular color:
- (void)drawRect:(NSRect)dirtyRect {
...
// Load the image through NSImage and set it as the current color.
[[NSColor colorWithPatternImage:[NSImage imageNamed:@"pattern"]] set];
// Fill the entire view with the image.
[NSBezierPath fillRect:[self bounds]];
...
}
This code will repeat the pattern over the entirety of view, but you can have it simply x-repeat or y-repeat with a little modification.
Take a look at the NSColor Class Reference and the NSView Class Reference to learn more.
精彩评论