开发者

Objective-c: view background pattern

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜