iOS開發 16進製制顏色轉換

2021-09-07 00:19:05 字數 1063 閱讀 3933

專案中經常會用到顏色轉換,有的是通過十六進製制轉成數字轉顏色,想簡單的點直接通過字串轉一下,簡單擴充套件了一下分類uicolor,**如下:

+(uicolor *)colorwithhex:(nsstring *)hexcolor

// ios技術交流:228407086

+(uicolor *)colorwithhex:(nsstring *)hexcolor alpha:(float)alpha

//if ([colorstr hasprefix:@"#"])

nsrange range;

range.location = 0;

range.length = 2;

//red

nsstring *redstring = [colorstr substringwithrange:range];

//green

range.location = 2;

nsstring *greenstring = [colorstr substringwithrange:range];

//blue

range.location = 4;

nsstring *bluestring= [colorstr substringwithrange:range];

// scan values

unsigned int red, green, blue;

[[nsscanner scannerwithstring:redstring] scanhexint:&red];

[[nsscanner scannerwithstring:greenstring] scanhexint:&green];

[[nsscanner scannerwithstring:bluestring] scanhexint:&blue];

return [uicolor colorwithred:((float)red/ 255.0f) green:((float)green/ 255.0f) blue:((float)blue/ 255.0f) alpha:alpha];

}

iOS 開發之 16進製制轉換

方法一 ios 十進位制轉十六進製制 nsstring hexstring nsstring stringwithformat nsstring alloc initwithformat 1x 整形引數 方法二 16進製制轉換 void transpasstohex bol hexpassword ...

16進製制顏色

r g b 紅 綠 藍 0 0 0 偏向色系 顏色寫法一般有 background red background ff0000 background rgb 255,0,0 background red 是直接用對應的英語單詞 background ff0000 是用16進製制來表達顏色 也可以寫成...

16進製制顏色轉換RGB原理

colorname 直接用顏色的名稱來指定顏色,比如 red blue green yellow rgb 三通道顏色 hex 十六進製制顏色 使用演示 方式一 uicolor colorwithred 0xf86340 0xff00 8 255.0 green 0xf86340 0xff00 8 2...