2015-04-02 79 views
0

我產生一些NSButtons程序使用此代碼:NSButton顏色設定編程迅速

for i in 1...height { 
     for j in 1...width { 
      var but = NSButton(frame: NSRect(x: x, y: y + 78, width: 30, height: 30)) 
      but.tag = k 
      but.title = "" 
      but.action = Selector("buttonPressed:") 
      but.target = self 
      but.bezelStyle = NSBezelStyle(rawValue: 6)! 
      but.layer?.backgroundColor = NSColor.blackColor().CGColor 

      var ges = NSClickGestureRecognizer(target: self, action: Selector("addFlag:")) 
      ges.buttonMask = 0x2 
      ges.numberOfClicksRequired = 1 
      but.addGestureRecognizer(ges) 

      ar.append(but) 
      self.view.addSubview(but) 
      x += 30 
      k++ 
     } 
     y += 30 
     x = 0 
    } 

,我需要設置所有按鈕的背景顏色爲灰色或黑色(隨機)。我不知道如何獲取或設置NSButton背景顏色。我對swift編程並不陌生,因此我不太瞭解obj-c,所以如果你很快就寫出來,我會非常感激!

回答

1

我讀了throgh NSButton的參考指南,它似乎是改變它改變圖像的唯一方法。但我已經有了一種使用顏色創建圖像的方法。在類文件的頂部添加此extionsion

extension NSImage { 
class func swatchWithColor(color: NSColor, size: NSSize) -> NSImage { 
    let image = NSImage(size: size) 
    image.lockFocus() 
    color.drawSwatchInRect(NSMakeRect(0, 0, size.width, size.height)) 
    image.unlockFocus() 
    return image 
    } 
} 

這將允許我們使用UIColour以後創建NSImage中的。最後,當你想創建你的按鈕,使用我們所做的擴展名設置圖像。

myButton.image = NSImage.swatchWithColor(NSColor.blackColor(), size: NSMakeSize(100, 100)) 

對於尺寸參數,將其設置爲你的按鈕的大小。

UPDATE *如果你想隨機選擇的顏色,你可以做到這一點..

var randomIndex = arc4random_uniform(2) + 1 //Creates a random number between 1 and 2 
var colour = NSColor() //Empty colour 

    if randomIndex == 1 { 

     //If the random number is one then the colour is black 
     colour = NSColor.blackColor() 
    } 

    else { 

     //Else if it's not one it's grey 
     colour = NSColor.grayColor() 
    } 

    //set the button's image to the new colour 
    myButton.image = NSImage.swatchWithColor(colour, size: //My button size) 

爲了讓大小去你的故事板,點擊此按鈕,進入到標尺選項卡。它顯示你的按鈕的大小。

+0

非常感謝,我知道如何隨機挑選顏色,但第一部分太棒了! – 2015-04-02 19:05:10

+0

很高興幫助你:) – 2015-04-02 19:06:38

0

壞消息是,有恕我直言,沒有好的方式着色按鈕;他們都有優點和缺點。 大家誰和我一樣,感到好奇着色按鈕約塞米蒂下我所寫的內容可能不是最終引導着色按鈕,但絕對是一個強有力的競爭者:

http://www.extelligentcocoa.org/colouring-buttons/

這包括所有樣式的按鈕,並具有截圖幾乎任意組合,使用

  • 的backgroundColor
  • 的drawRect
  • 細胞的backgroundColor
  • 單色layerEffect
  • 最大光圈調整layerEffect
  • 圖像IB
  • 繼承NSButton並添加根據按鈕狀態

如果任何人能想到的顏色按鈕的另一種方式,我的圖像設置米有興趣聽到它!

+0

鏈接已死... – peterflynn 2016-04-17 17:02:13

+0

我的歉意 - 該網站被黑客攻擊,我不得不重新安裝內容;抱歉錯過了這一點。 – 2016-04-30 19:46:49

+0

鏈接仍然死亡。 – 2016-08-25 22:22:51

1

我這個代碼

final class MyButton: NSButton { 
    override func awakeFromNib() { 
     let layer = CALayer() 
     layer.backgroundColor = CGColorCreateGenericRGB(59.0/255, 52.0/255.0, 152.0/255.0, 1.0) 
     self.wantsLayer = true 
     self.layer = layer 
    } 
} 
+0

非常感謝!這個對我有用 – 2017-10-25 11:18:19

0

好做,這裏是我做過什麼,當我需要更改按鈕的外觀:繼承它。

// 
// LSButtonColor.swift 
// 
// Created by Lloyd Sargent on 8/26/16. 
// Copyright © 2016 Canna Software. All rights reserved. 
// License: Permission is hereby granted, free of charge, to any 
//   person obtaining a copy of this software and associated 
//   documentation files (the 「Software」), to deal in the 
//   Software without restriction, including without 
//   limitation the rights to use, copy, modify, merge, 
//   publish, distribute, sublicense, and/or sell copies of 
//   the Software, and to permit persons to whom the Software 
//   is furnished to do so, subject to the following 
//   conditions: 
//   
//   The above copyright notice and this permission notice 
//   shall be included in all copies or substantial portions 
//   of the Software. 
// 
//   THE SOFTWARE IS PROVIDED 「AS IS」, WITHOUT WARRANTY OF 
//   ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
//   TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
//   PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 
//   SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
//   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
//   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
//   IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
//   DEALINGS IN THE SOFTWARE. 
// 

import Cocoa 

class LSButtonColor: NSButton { 

fileprivate struct AssociatedKeys { 
    static var variableDictionary = "ls_variableDictionary" 
} 

fileprivate var variableDictionary: [String:AnyObject]! { 
    get { 
     var localVariableDictionary = objc_getAssociatedObject(self, &AssociatedKeys.variableDictionary) as! [String:AnyObject]! 
     if localVariableDictionary == nil { 
      localVariableDictionary = [String:AnyObject]() 
      objc_setAssociatedObject(self, &AssociatedKeys.variableDictionary, localVariableDictionary, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 
     } 
     return localVariableDictionary 
    } 
    set(updatedDictionary) { 
     objc_setAssociatedObject(self, &AssociatedKeys.variableDictionary, updatedDictionary, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 
    } 
} 

var backgroundColor: NSColor! { 
    get { 
     return variableDictionary["backgroundColor"] as? NSColor 
    } 
    set(newBackgroundColor) { 
     var localVariableDictionary = variableDictionary 
     localVariableDictionary?["backgroundColor"] = newBackgroundColor 
     variableDictionary = localVariableDictionary 
    } 
} 

var altBackgroundColor: NSColor! { 
    get { 
     return variableDictionary["altBackgroundColor"] as? NSColor 
    } 
    set(newAltBackgroundColor) { 
     var localVariableDictionary = variableDictionary 
     localVariableDictionary?["altBackgroundColor"] = newAltBackgroundColor 
     variableDictionary = localVariableDictionary 
    } 
} 

var borderColor: NSColor! { 
    get { 
     return variableDictionary["borderColor"] as? NSColor 
    } 
    set(newBorderColor) { 
     var localVariableDictionary = variableDictionary 
     localVariableDictionary?["borderColor"] = newBorderColor 
     variableDictionary = localVariableDictionary 

     self.layer?.borderColor = newBorderColor.cgColor 
    } 
} 

var cornerRadius: CGFloat { 
    get { 
     return variableDictionary["cornerRadius"] as! CGFloat 
    } 
    set(newCornerRadius) { 
     var localVariableDictionary = variableDictionary 
     localVariableDictionary?["cornerRadius"] = newCornerRadius as AnyObject? 
     variableDictionary = localVariableDictionary 

     self.layer?.cornerRadius = newCornerRadius 
    } 
} 

var borderWidth: CGFloat { 
    get { 
     return variableDictionary["borderWidth"] as! CGFloat 
    } 
    set(newBorderWidth) { 
     var localVariableDictionary = variableDictionary 
     localVariableDictionary?["borderWidth"] = newBorderWidth as AnyObject? 
     variableDictionary = localVariableDictionary 

     self.layer?.borderWidth = newBorderWidth 
    } 
} 

override func draw(_ dirtyRect: NSRect) { 
    super.draw(dirtyRect) 

    // Drawing code here. 
    if self.isHighlighted { 
     if self.layer != nil { 
      self.layer?.backgroundColor = self.altBackgroundColor.cgColor 
     } 
    } 
    else { 
     if self.layer != nil { 
      self.layer?.backgroundColor = self.backgroundColor.cgColor 
     } 
    } 
    } 
} 

好吧,你可能會令人費解的第一件事就是objc_getAssociatedObject - 我原本這是一個類別,它是增加了變量的categorys(一種易於peasy方式人人都說你不能做到這一點,但每個人都沒有意識到,是的,你可以 - 這只是不明顯)。

不幸的是,我遇到了一些問題,所以我只是做了它的類 - 並沒有理會採取了相關的對象(爲什麼成功爛攤子?)

無論如何,你應該能夠拉出相關的代碼來改變任何按鈕的背景。