是否有一种简单的方法来获得此网格背景?或者我必须这样做[NSColor colorWithPatternImage:[NSImage ...]]
?
我不想要完整的代码。我只是想知道是否有一种简单的方法可以做到这一点,如果是的话。
答案 0 :(得分:7)
这是我的解决方案:
- (void)drawRect:(NSRect)dirtyRect
{
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
[[NSColor whiteColor] setFill];
CGContextFillRect(context, dirtyRect);
for (int i = 1; i < [self bounds].size.height / 10; i++) {
if (i % 10 == 0) {
[[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.3] set];
} else if (i % 5 == 0) {
[[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.2] set];
} else {
[[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.1] set];
}
[NSBezierPath strokeLineFromPoint:NSMakePoint(0, i * 10 - 0.5) toPoint:NSMakePoint([self bounds].size.width, i * 10 - 0.5)];
}
for (int i = 1; i < [self bounds].size.width / 10; i++) {
if (i % 10 == 0) {
[[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.3] set];
} else if (i % 5 == 0) {
[[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.2] set];
} else {
[[NSColor colorWithSRGBRed:100/255.0 green:149/255.0 blue:237/255.0 alpha:0.1] set];
}
[NSBezierPath strokeLineFromPoint:NSMakePoint(i * 10 - 0.5, 0) toPoint:NSMakePoint(i * 10 - 0.5, [self bounds].size.height)];
}
}
答案 1 :(得分:2)
我不认为图案颜色在这里是一个很好的解决方案,特别是因为你需要改变线条。而是为每条水平和垂直线使用NSBezierPath和moveToPoint / lineToPoint对。然后,您可以在一次调用中绘制网格。为具有不同颜色(alpha)和/或宽度的线条执行额外的步骤(即,不要将较粗的线条添加到主网格路径,而是为它们创建单独的线条。)
答案 2 :(得分:2)
同样适用于 Swift 4
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
if let context = NSGraphicsContext.current?.cgContext {
NSColor.white.setFill()
context.fill(dirtyRect)
context.flush()
}
for i in 1...(Int(self.bounds.size.height) / 10) {
if i % 10 == 0 {
NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.3).set()
}else if i % 5 == 0 {
NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.2).set()
}else{
NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.1).set()
}
NSBezierPath.strokeLine(from: CGPoint(x: 0, y: CGFloat(i) * 10 - 0.5), to: CGPoint(x: self.bounds.size.width, y: CGFloat(i) * 10 - 0.5))
}
for i in 1...(Int(self.bounds.size.width) / 10) {
if i % 10 == 0 {
NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.3).set()
}else if i % 5 == 0 {
NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.2).set()
}else{
NSColor.init(red: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.1).set()
}
NSBezierPath.strokeLine(from: CGPoint(x: CGFloat(i) * 10 - 0.5, y:0), to: CGPoint(x: CGFloat(i) * 10 - 0.5, y: self.bounds.size.height))
}
}
答案 3 :(得分:1)
只需查看每个Xcode安装附带的the Sketch example,或者也可以单独下载。
它具有网格实现(以及许多其他有用的Cocoa技术演示......),允许缩放等。
答案 4 :(得分:1)
移植甲醇的解决方案,以便在MacOS 10.10及更高版本中快速解决错误。
可以使用简单的静态函数,并从drawRect()
调用public void serveResource( ResourceRequest request, ResourceResponse response)
throws IOException, PortletException {
String action = ParamUtil.getString(request , "action", "");
if(action.equals("updateRatAjax")){
String article = ParamUtil.getString(request , "article", "");
System.out.println(" updateRatAjax");
}
}
答案 5 :(得分:1)
与“伤心金枪鱼”相同,但在 Swift 3
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
if let context = NSGraphicsContext.currentContext()?.CGContext {
NSColor.whiteColor().setFill()
CGContextFillRect(context, dirtyRect)
CGContextFlush(context)
}
for i in 1...(Int(self.bounds.size.height) / 10) {
if i % 10 == 0 {
NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.3).set()
}else if i % 5 == 0 {
NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.2).set()
}else{
NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.1).set()
}
NSBezierPath.strokeLineFromPoint(CGPointMake(0, CGFloat(i) * 10 - 0.5), toPoint: CGPointMake(self.bounds.size.width, CGFloat(i) * 10 - 0.5))
}
for i in 1...(Int(self.bounds.size.width) / 10) {
if i % 10 == 0 {
NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.3).set()
}else if i % 5 == 0 {
NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.2).set()
}else{
NSColor(SRGBRed: 100/255.0, green: 149/255.0, blue: 237/255.0, alpha: 0.1).set()
}
NSBezierPath.strokeLineFromPoint(CGPointMake(CGFloat(i) * 10 - 0.5, 0), toPoint: CGPointMake(CGFloat(i) * 10 - 0.5, self.bounds.size.height))
}
}