如何使NSView的背景图片不重复? [英] How to make NSView's background image not repeat?

查看:353
本文介绍了如何使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 类引用。图片可以使用 drawInRect:fromRect:operation:fraction:以及 drawAtPoint: fromRect:operation:fraction:

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的背景图片不重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆