【发布时间】:2011-06-06 01:48:09
【问题描述】:
视图中是否可以有一个可以重复 x 或 y 或两者的模式(比如网格)?有点像css:
背景图片: url(pattern.png); 背景重复:重复; //或repeat-x,repeat-y,不重复;
【问题讨论】:
标签: objective-c view background
视图中是否可以有一个可以重复 x 或 y 或两者的模式(比如网格)?有点像css:
背景图片: url(pattern.png); 背景重复:重复; //或repeat-x,repeat-y,不重复;
【问题讨论】:
标签: objective-c view background
以下代码将平铺背景图像:
- (void) viewDidLoad {
[[self view] setBackgroundColor: [[UIColor alloc] initWithPatternImage: [UIImage imageNamed: @"background.png"]]];
[super viewDidLoad];
}
【讨论】:
是的。您可以将图像模式加载为颜色 ([NSColor withPatternImage:[NSImage imageNamed:@"pattern"]]),然后像绘制常规颜色一样绘制它:
- (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]];
...
}
此代码将在整个视图中重复该模式,但您可以简单地对其进行 x-repeat 或 y-repeat 并稍作修改。
查看NSColor Class Reference 和NSView Class Reference 了解更多信息。
【讨论】: