iOS開發 從16進製制顏色中獲取UIColor

2021-09-06 20:57:55 字數 1082 閱讀 8079

目前ios中設定uicolor只能使用其列舉值、rgb等方法,不能直接將常用的16進製制顏色值直接轉為uicolor物件,所以寫了點**,將16進製制顏色值轉為uicolor。

**如下,

//標頭檔案

#import

#import

@inte***ce

textservcie : nsobject

+(uicolor *) getcolorfromhex:(nsstring *)hex;

@end

.m@implementation textservcie

+(uicolor *)getcolorfromhex:(nsstring *)hex{

hex= [[hex uppercasestring] substringfromindex:1];

cgfloat valuearray[3];

nsarray *strarray=[nsarray arraywithobjects:[hex substringwithrange:nsmakerange(0, 2)],[hex substringwithrange:nsmakerange(2, 2)],[hex substringwithrange:nsmakerange(4, 2)] ,nil];

for( int i=0;ihex=strarray[i];

cgfloat value=([hex characteratindex:0]>'9'?[hex characteratindex:0]-'a'+10:[hex characteratindex:0]-'0')*16.0f+([hex characteratindex:1]>'9'?[hex characteratindex:1]-'a'+10:[hex characteratindex:1]-'0');

valuearray[i]=value;

return [uicolor colorwithred:valuearray[0]/255 green:valuearray[1]/255 blue:valuearray[2]/255 alpha:1];

@end

以上**,工作正常,下次不用再用工具手動轉rgb了

iOS開發 16進製制顏色轉換

專案中經常會用到顏色轉換,有的是通過十六進製制轉成數字轉顏色,想簡單的點直接通過字串轉一下,簡單擴充套件了一下分類uicolor,如下 uicolor colorwithhex nsstring hexcolor ios技術交流 228407086 uicolor colorwithhex nsst...

16進製制顏色

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

iOS 16進製制顏色的巨集

ios 16進製制顏色的巨集 引數格式為 0xffffff 16進製制 define kcolorwithrgb rgbvalue uicolor colorwithred float rgbvalue 0xff0000 16 255.0 green float rgbvalue 0xff00 8 ...