本文介绍了如何使NSView的背景图片不重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想复制由以下CSS代码获得的效果:

I want replicate the effect obtained by the following CSS code:

background: white url(./img/background.png) no-repeat;

我写了一个NSView的子类,并覆盖 drawRect 这样:

I've written a subclass of NSView and override drawRect in this way:

- (void)drawRect:(NSRect)dirtyRect
{
    dirtyRect = [self bounds];

    [[NSColor whiteColor] setFill];
    NSRectFill(dirtyRect);

    [[NSColor colorWithPatternImage:[NSImage imageNamed:@"background.png"]] setFill];
    NSRectFill(dirtyRect);
}

(抱歉我的英文不好意思)

(I apologize for my bad english)

推荐答案

查看 NSImage 。图片可以使用以及。

Take a look at NSImage class reference. Image can be drawn with drawInRect:fromRect:operation:fraction: and also with drawAtPoint:fromRect:operation:fraction:.

因此,您可以使用:

[[NSImage imageNamed:@"background.png"] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1]; // Passing NSZeroRect causes the entire image to draw. 

而是:

[[NSColor colorWithPatternImage:[NSImage imageNamed:@"background.png"]] setFill];
    NSRectFill(dirtyRect);

这篇关于如何使NSView的背景图片不重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 16:23