我有一個像「rgb(0,0,0)」格式的服務器的字符串,應該在某些UI元素上使用。如何從像「rgb(0,0,0)」這樣的字符串中創建UIColor?
的主要問題是各種顏色,如「RGB(20,120,4)」等
有沒有一種方法來分析它像UIColor(red: 0, green: 0, blue: 0, alpha: 1)
?
我有一個像「rgb(0,0,0)」格式的服務器的字符串,應該在某些UI元素上使用。如何從像「rgb(0,0,0)」這樣的字符串中創建UIColor?
的主要問題是各種顏色,如「RGB(20,120,4)」等
有沒有一種方法來分析它像UIColor(red: 0, green: 0, blue: 0, alpha: 1)
?
有這麼多的可能的解決方案!下面是一個使用 NSScanner
:
extension UIColor {
convenience init?(rgbString : String) {
var red = 0.0
var green = 0.0
var blue = 0.0
let scanner = NSScanner(string : rgbString)
guard scanner.scanString("rgb(", intoString: nil)
&& scanner.scanDouble(&red)
&& scanner.scanString(",", intoString: nil)
&& scanner.scanDouble(&green)
&& scanner.scanString(",", intoString: nil)
&& scanner.scanDouble(&blue)
&& scanner.scanString(")", intoString: nil) else {
return nil
}
self.init(red: CGFloat(red/255.0), green: CGFloat(green/255.0), blue: CGFloat(blue/255.0), alpha: 1.0)
}
}
用例:
if let col = UIColor(rgbString: "rgb(20,120.5, 4)") {
print(col)
} else {
print("invalid color specification")
}
更新斯威夫特3/4:
extension UIColor {
convenience init?(rgbString: String) {
var red = 0.0
var green = 0.0
var blue = 0.0
let scanner = Scanner(string: rgbString)
guard scanner.scanString("rgb(", into: nil)
&& scanner.scanDouble(&red)
&& scanner.scanString(",", into: nil)
&& scanner.scanDouble(&green)
&& scanner.scanString(",", into: nil)
&& scanner.scanDouble(&blue)
&& scanner.scanString(")", into: nil) else {
return nil
}
self.init(red: CGFloat(red/255.0), green: CGFloat(green/255.0), blue: CGFloat(blue/255.0), alpha: 1.0)
}
}
你可以試試這個。
import UIKit
let string1 = "rgb(25.00,100.0,100)"
let indexToRemoveTheLast = string1.endIndex.advancedBy(-1)
let substring1 = string1.substringToIndex(indexToRemoveTheLast)
let indexToRemoveTheFirst = string1.startIndex.advancedBy(4)
let substring2 = substring1.substringFromIndex(indexToRemoveTheFirst)
var colorsString = substring2.characters.split{$0 == ","}.map(String.init)
let red = (Float(colorsString[0])!)/255
let green = (Float(colorsString[1])!)/255
let blue = (Float(colorsString[2])!)/255
let myColor = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: 1)
的結果在這裏:
這裏是將您的字符串轉換爲UIColor
的方法。可能有正則表達式更好的解決方案,但我在正則表達式中很糟糕。
func colorFromString(colorString: String) -> UIColor? {
// strip down rgb and parenthesis
let combinedCharacterSet = NSMutableCharacterSet.letterCharacterSet()
combinedCharacterSet.addCharactersInString("()")
let trimmedString = colorString.stringByTrimmingCharactersInSet(combinedCharacterSet)
let rgbValueStrings = trimmedString.componentsSeparatedByString(",")
if rgbValueStrings.count != 3 {
return nil
}
if let r = Double(rgbValueStrings[0]) where r >= 0 && r <= 255,
let g = Double(rgbValueStrings[1]) where g >= 0 && g <= 255,
let b = Double(rgbValueStrings[2]) where b >= 0 && b <= 255 {
return UIColor(red: CGFloat(r/255), green: CGFloat(g/255), blue: CGFloat(b/255), alpha: 1.0)
} else {
return nil
}
}
和操場可以downloaded here
您可能還需要在分割爲單個字符串之後修剪空格字符,因爲數字周圍的空格不會被正確解析。 – OgreSwamp
什麼格式的服務器返回的代碼?串?請張貼更多的代碼,以便我可以幫你 – Eric
@Eric是的,絃樂。 –
我會去掉第一個字符,但是這不是很好做,我應該告訴你如何做到這一點? – Eric