課程
/移動開發(fā)
/iOS
/玩兒轉(zhuǎn)Swift
請問老師在swift 語言中怎樣直接使用16進制的色值
2015-03-11
源自:玩兒轉(zhuǎn)Swift 9-3
正在回答
func UIColorFromRGB (hex:String) -> UIColor {
? ? ? ? var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString
?? ? ? ?
? ? ? ? if (cString.hasPrefix("#")) {
? ? ? ? ? ? cString = (cString as NSString).substringFromIndex(1)
? ? ? ? }
? ? ? ? let rString = (cString as NSString).substringToIndex(2)
? ? ? ? let gString = ((cString as NSString).substringFromIndex(2) as NSString).substringToIndex(2)
? ? ? ? let bString = ((cString as NSString).substringFromIndex(4) as NSString).substringToIndex(2)
? ? ? ? var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
? ? ? ? NSScanner(string: rString).scanHexInt(&r)
? ? ? ? NSScanner(string: gString).scanHexInt(&g)
? ? ? ? NSScanner(string: bString).scanHexInt(&b)
? ? ? ? return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
? ? }
eg:
self.setTitleColor(UIColorFromRGB("0xec8c00"), forState: .Selected)
恩格貝 提問者
已用粗體標(biāo)出。
func UIColorFromRGB (hex:String) -> UIColor {??????? ??????? var cString:String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)??????? if (cString.hasPrefix("#")) {???????? ??????????? cString = (cString as NSString).substring(from: 1)??????? }???? ??????? let rString = (cString as NSString).substring(to: 2) ??????? let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)??????? let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)??????? var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;??????? Scanner(string: rString).scanHexInt32(&r)??????? Scanner(string: gString).scanHexInt32(&g)??????? Scanner(string: bString).scanHexInt32(&b)??????? return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))? ??? }這些需要改動的。
親測,可以使用謝謝!不過swift3的語法略微有些變化,需要稍微修改下。
這個還需要轉(zhuǎn)換成UIColor里面三色才能使用
舉報
這套教程比任何的Swift中文教程深入,并且演示示例更豐富
3 回答Swift語言中的開方運算怎么寫
4 回答swift使用playground界面設(shè)置問題
2 回答老師你好,請問開發(fā)swift用macbook air 可以嗎?
1 回答Swift多維數(shù)組賦值的問題
2 回答Swift:數(shù)組和元組在應(yīng)用場景中的區(qū)別
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-04-06
func UIColorFromRGB (hex:String) -> UIColor {
? ? ? ? var cString:String = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString
?? ? ? ?
? ? ? ? if (cString.hasPrefix("#")) {
? ? ? ? ? ? cString = (cString as NSString).substringFromIndex(1)
? ? ? ? }
? ? ? ? let rString = (cString as NSString).substringToIndex(2)
? ? ? ? let gString = ((cString as NSString).substringFromIndex(2) as NSString).substringToIndex(2)
? ? ? ? let bString = ((cString as NSString).substringFromIndex(4) as NSString).substringToIndex(2)
?? ? ? ?
? ? ? ? var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
? ? ? ? NSScanner(string: rString).scanHexInt(&r)
? ? ? ? NSScanner(string: gString).scanHexInt(&g)
? ? ? ? NSScanner(string: bString).scanHexInt(&b)
? ? ? ? return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
? ? }
eg:
self.setTitleColor(UIColorFromRGB("0xec8c00"), forState: .Selected)
2016-11-23
已用粗體標(biāo)出。
2016-11-23
func UIColorFromRGB (hex:String) -> UIColor {???????
??????? var cString:String = hex.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
??????? if (cString.hasPrefix("#")) {????????
??????????? cString = (cString as NSString).substring(from: 1)
??????? }????
??????? let rString = (cString as NSString).substring(to: 2)
??????? let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
??????? let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)
??????? var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
??????? Scanner(string: rString).scanHexInt32(&r)
??????? Scanner(string: gString).scanHexInt32(&g)
??????? Scanner(string: bString).scanHexInt32(&b)
??????? return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))?
??? }
這些需要改動的。
2016-11-23
親測,可以使用謝謝!不過swift3的語法略微有些變化,需要稍微修改下。
2016-02-17
這個還需要轉(zhuǎn)換成UIColor里面三色才能使用