有沒有簡單的方法來獲得這個網格背景?或者我必須這樣做[NSColor colorWithPatternImage:[NSImage ...]]
?如何爲NSView製作網格背景圖案?
我不想完整的代碼。我只想知道是否有一個簡單的方法來做到這一點,如果是的話。
有沒有簡單的方法來獲得這個網格背景?或者我必須這樣做[NSColor colorWithPatternImage:[NSImage ...]]
?如何爲NSView製作網格背景圖案?
我不想完整的代碼。我只想知道是否有一個簡單的方法來做到這一點,如果是的話。
我不認爲圖案顏色是一個很好的解決方案,特別是因爲你需要改變線條。對每條水平線和垂直線使用NSBezierPath和moveToPoint/lineToPoint對。然後您可以在一次通話中繪製網格。爲具有不同顏色(阿爾法)和/或寬度的線條執行額外的步驟(即,不要將較粗的線條添加到主網格路徑,而是爲它們創建單獨的線條)。
這是變化的線寬不是一個問題,真的。您只需將圖案製成大(厚)框中的一種,而不是其中一個小框。由於這些方塊可能會重複出現,所以這可能比Quartz實際計算和消除所有行的混淆更耗費CPU資源。 – uliwitness 2014-03-08 13:13:10
只需看看每個Xcode安裝附帶的the Sketch example,或者也可以單獨下載。
,設有電網實現(和可可技術的許多其他有用的示威遊行..)允許縮放等
這是我的解決方案:
- (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)];
}
}
移植甲醇的解決方案以迅速解決MacOS 10.10以上的錯誤。
一個簡單的靜態函數可以用來從drawRect中()
static func makeGridBackground(dirtyRect: NSRect, view: NSView){
//view.print("WMEditorUtils: initiated drawing")
//Fill background with white color
if let context = NSGraphicsContext.currentContext()?.CGContext {
NSColor.whiteColor().setFill()
CGContextFillRect(context, dirtyRect)
CGContextFlush(context)
}
//Draw Lines: Horizontal
for var i:Int = 1; i < (Int)(view.bounds.size.height/10); i++ {
if (i % 10 == 0) {
NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.3).set()
}
else if (i % 5 == 0) {
NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.2).set()
}
else{
NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.1).set()
}
NSBezierPath.strokeLineFromPoint(NSMakePoint(0, (CGFloat)(i * 10) - 0.5), toPoint: NSMakePoint(view.bounds.size.width, (CGFloat)(i * 10) - 0.5))
}
//Draw Lines: Vertical
for var i:Int = 1; i < (Int)(view.bounds.size.width/10); i++ {
if (i % 10 == 0) {
NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.3).set()
}
else if (i % 5 == 0) {
NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.2).set()
}
else{
NSColor(deviceRed: 100.0/255.0, green: 149.0/255.0, blue: 237.0/255.0, alpha: 0.1).set()
}
NSBezierPath.strokeLineFromPoint(NSMakePoint((CGFloat)(i * 10) - 0.5, 0), toPoint: NSMakePoint((CGFloat)(i * 10) - 0.5, view.bounds.size.width))
}
}
相同的 「悲傷金槍魚」,但在夫特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))
}
}
相同的,但對於夫特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))
}
}
不適用於mac,但適用於iOS,但它可能會開始:[適用於iOS開發人員的實用繪圖](https://developer.apple.com/videos/wwdc/2011/?id=129) – vikingosegundo 2013-04-04 22:16:35
@methanol,有沒有答案給你什麼你想知道?如果是這樣,請接受該答案以允許爲其分配點數。 – 2013-04-15 13:31:45
@MikeLischke如果你的意思是「積分」,那麼聲譽是 - 我已經做到了。我提出了兩個答案。沒有一個是我正在尋找的。 – tuna 2013-04-15 19:48:01