2017-04-03 21 views
1

我知道如何做到這一點編程方式使用這樣的回答: Saving UIColor to and loading from NSUserDefaults如何將顏色輸入到不以編程方式創建的plist中?

但什麼是我的選擇時,我希望能夠預先加載plist我在編輯器(創建plist文件,並填補其領域創造 - 而不是編碼)? Xcode只會給你幾個下拉菜單選項,如stringdictionaryarraynumber

有沒有辦法在編輯器中做到這一點? 我可以創建R/G/B 3場,並用數字設定,然後但它似乎是一個不好的設計

+0

您可以在plist中添加十六進制顏色代碼,它可以通過編程方式讀取並進行處理。 –

+0

爲什麼不是一個靜態類/屬性與​​你的顏色? –

+0

@AnandSuthar這是個好主意!你能詳細解答一個答案嗎?十六進制應該是一個字符串或數字? – Curnelious

回答

1

步驟是

  1. 創建字典的的.plist排列如下圖像 enter image description here

  2. 讀的.plist值

    var myDict: NSDictionary? 
    if let path = NSBundle.mainBundle().pathForResource("colors", ofType: "plist") { 
    myDict = NSDictionary(contentsOfFile: path) 
    } 
    if let dict = myDict { 
    // Use your dict here 
    } 
    
  3. 一旦你得到十六進制顏色,那麼你可以將其轉換的UIColor:Hex-to-UIColor

1

如果你真的需要抽象,從你的代碼信息,這是不夠好供您使用一些靜態常量,你可以嘗試使用NSColor擴展來解析字符串值並將其存儲在plist上。

事情是這樣的工作對我來說:

import AppKit 

extension NSColor { 
    convenience init(hex:String) { 

     var cString:String = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() 

     if (cString.hasPrefix("#")) { 
      cString = cString.substring(from: cString.characters.index(cString.startIndex, offsetBy: 1)) 
     } 

     if ((cString.characters.count) != 6) { 
      self.init (red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0) 
     } else { 

      var rgbValue:UInt32 = 0 
      Scanner(string: cString).scanHexInt32(&rgbValue) 

      self.init(red:CGFloat((rgbValue & 0xFF0000) >> 16)/255.0, green: CGFloat((rgbValue & 0x00FF00) >> 8)/255.0, blue: CGFloat(rgbValue & 0x0000FF)/255.0, alpha: CGFloat(1.0)) 
     } 

    } 
} 
0

而不是存儲在一個plist中你的顏色,可以考慮將它們放置在一個結構:「的UIColor + Styling.swift」

public extension UIColor { 
    struct AppName { 
     static let white = UIColor(hex: "#FFFFFF") 
     static let black = UIColor(hex: "#3D2C2A") 
     static let red = UIColor(hex: "#D84C3A") 
     static let darkRed = UIColor(hex: "#B84132") 
     static let green = UIColor(hex: "#6C9491") 
    } 
} 

然後你就可以使用它們像這樣:

backgroundColor = UIColor.AppName.green 

要啓動的UIColor對象與十六進制ç Ode實施此擴展名:'UIColor + Hex.swift'

public extension UIColor { 

    /// You can pass a hex with or without # and with or without alpha 
    convenience init(hex: String) { 
     var red: CGFloat = 0.0 
     var green: CGFloat = 0.0 
     var blue: CGFloat = 0.0 
     var alpha: CGFloat = 1.0 

     let cleanHex = hex.replacingOccurrences(of: "#", with: "") 

     let scanner = Scanner(string: cleanHex) 
     var hexValue: CUnsignedLongLong = 0 
     if scanner.scanHexInt64(&hexValue) { 
      if cleanHex.characters.count == 6 { 
       red = CGFloat((hexValue & 0xFF0000) >> 16)/255.0 
       green = CGFloat((hexValue & 0x00FF00) >> 8)/255.0 
       blue = CGFloat(hexValue & 0x0000FF)/255.0 
      } else if cleanHex.characters.count == 8 { 
       alpha = CGFloat((hexValue & 0xFF000000) >> 24)/255.0 
       red = CGFloat((hexValue & 0x00FF0000) >> 16)/255.0 
       green = CGFloat((hexValue & 0x0000FF00) >> 8)/255.0 
       blue = CGFloat(hexValue & 0x000000FF)  /255.0 
      } else { 
       print("invalid rgb string, length should be 6 or 8", terminator: "") 
      } 
     } else { 
      print("scan hex error") 
     } 

     self.init(red:red, green:green, blue:blue, alpha:alpha) 
    } 
} 
相關問題