十六進製制顏色
#import ns_assume_nonnull_begin
@inte***ce uicolor (hexcolor)
+ (uicolor *)wh_colorwithhexstring:(nsstring *)color alpha:(cgfloat)alpha;
@end
ns_assume_nonnull_end
#import "uicolor+hexcolor.h"
@implementation uicolor (hexcolor)
+ (uicolor *)wh_colorwithhexstring:(nsstring *)color alpha:(cgfloat)alpha
if ([cstring hasprefix:@"0x"])
if ([cstring hasprefix:@"#"])
if ([cstring length] != 6)
nsrange range;
range.location = 0;
range.length = 2;
//rnsstring *rstring = [cstring substringwithrange:range];
//grange.location = 2;
nsstring *gstring = [cstring substringwithrange:range];
//brange.location = 4;
nsstring *bstring = [cstring substringwithrange:range];
unsigned int r, g, b;
[[nsscanner scannerwithstring:rstring] scanhexint:&r];
[[nsscanner scannerwithstring:gstring] scanhexint:&g];
[[nsscanner scannerwithstring:bstring] scanhexint:&b];
return [uicolor colorwithred:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}@end
//顯示16進製制顏色
+ (uicolor *)colorwithhexstring:(nsstring *)color alpha:(cgfloat)alpha
//如果是0x開頭的,那麼擷取字串,字串從索引為2的位置開始,一直到末尾
if ([cstring hasprefix:@"0x"])
//如果是#開頭的,那麼擷取字串,字串從索引為1的位置開始,一直到末尾
if ([cstring hasprefix:@"#"])
if ([cstring length] != 6)
// separate into r, g, b substrings
nsrange range;
range.location = 0;
range.length = 2;
//rnsstring *rstring = [cstring substringwithrange:range];
= 2;
nsstring *gstring = [cstring substringwithrange:range];
= 4;
nsstring *bstring = [cstring substringwithrange:range];
// scan values
unsigned int r, g, b;
[[nsscanner scannerwithstring:rstring] scanhexint:&r];
[[nsscanner scannerwithstring:gstring] scanhexint:&g];
[[nsscanner scannerwithstring:bstring] scanhexint:&b];
return [uicolor colorwithred:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}//預設alpha值為1
+ (uicolor *)colorwithhexstring:(nsstring *)color
=
**********====顯示漸變色**********
//16進製制顏色、rgb顏色、漸變色
#import @inte***ce uicolor (hex)
+ (uicolor *)colorwithhex:(uint32)hex;
+ (uicolor *)colorwithhex:(uint32)hex andalpha:(cgfloat)alpha;
+ (uicolor *)colorwithhexstring:(nsstring *)hexstring;
- (nsstring *)hexstring;
+ (uicolor *)colorwithwholered:(cgfloat)red
green:(cgfloat)green
blue:(cgfloat)blue
alpha:(cgfloat)alpha;
+ (uicolor *)colorwithwholered:(cgfloat)red
green:(cgfloat)green
blue:(cgfloat)blue;
/** 漸變色
*/+ (uicolor*)gradientfromcolor:(uicolor*)c1 tocolor:(uicolor*)c2 withheight:(int)height;
+ (uicolor*)gradientfromcolor:(uicolor*)c1 tocolor:(uicolor*)c2 withwidth:(int)width;
+ (uicolor*)gradientfromcolor:(uicolor*)c1 tocolor:(uicolor*)c2 withpoint:(cgpoint)point;
@end
#import "uicolor+hex.h"
cgfloat colorcomponentfrom(nsstring *string, nsuinteger start, nsuinteger length)
@implementation uicolor (hex)
+ (uicolor *)colorwithhex:(uint32)hex
+ (uicolor *)colorwithhex:(uint32)hex andalpha:(cgfloat)alpha
+ (uicolor *)colorwithhexstring:(nsstring *)hexstring
return [uicolor colorwithred:red green:green blue:blue alpha:alpha];
}- (nsstring *)hexstring
if (cgcolorspacegetmodel(cgcolorgetcolorspace(color.cgcolor)) != kcgcolorspacemodelrgb)
return [nsstring stringwithformat:@"#%02x%02x%02x", (int)((cgcolorgetcomponents(color.cgcolor))[0]*255.0),
(int)((cgcolorgetcomponents(color.cgcolor))[1]*255.0),
(int)((cgcolorgetcomponents(color.cgcolor))[2]*255.0)];
}+ (uicolor *)colorwithwholered:(cgfloat)red
green:(cgfloat)green
blue:(cgfloat)blue
alpha:(cgfloat)alpha
+ (uicolor *)colorwithwholered:(cgfloat)red
green:(cgfloat)green
blue:(cgfloat)blue
/** * @brief 漸變顏色
* * @param c1 開始顏色
* @param c2 結束顏色
* @param height 漸變高度
* * @return 漸變顏色
*/+ (uicolor*)gradientfromcolor:(uicolor*)c1 tocolor:(uicolor*)c2 withheight:(int)height
+ (uicolor*)gradientfromcolor:(uicolor*)c1 tocolor:(uicolor*)c2 withwidth:(int)width
+ (uicolor*)gradientfromcolor:(uicolor*)c1 tocolor:(uicolor*)c2 withpoint:(cgpoint )point
@end
iOS開發 16進製制顏色轉換
專案中經常會用到顏色轉換,有的是通過十六進製制轉成數字轉顏色,想簡單的點直接通過字串轉一下,簡單擴充套件了一下分類uicolor,如下 uicolor colorwithhex nsstring hexcolor ios技術交流 228407086 uicolor colorwithhex nsst...
iOS 16進製制顏色的巨集
ios 16進製制顏色的巨集 引數格式為 0xffffff 16進製制 define kcolorwithrgb rgbvalue uicolor colorwithred float rgbvalue 0xff0000 16 255.0 green float rgbvalue 0xff00 8 ...
iOS 16進製制顏色和UIcolor的轉換
各種顏色之間的轉換,會陸續更新,實現了 16進製制顏色 hex rgba hsba uicolor之間的 相互轉換 使用示例 加號方法,類名呼叫 1 uicolor 轉 rgb hsb 2 rgbacolor colora colorconversion uicolortorgba uicolor ...