您可以使用十六进制值和以下方法设置自定义颜色,将十六进制颜色值转换为 RGB 值并将其传递到 DHWorkBook 的 setupNewColors 中,您必须为此使用 DHxlsIOS SDK。为此,我创建了一种通用方法,在该方法中我传递 DHWorkBook、DHCell、colorID 和十六进制颜色字符串的对象,例如 EFEFEF、CACACA。您可以传递 9 到 63 之间的任何数字作为颜色 ID,您可以在“color.h”文件中检查颜色 ID。然后你需要使用 setupNewColors 设置 RGB 颜色并使用我创建的 DHCell 的 fillBGcolorUnsigned 填充到单元格中,以传递 unsigned8_t 颜色对象。
-(void)customColor:(DHWorkBook *)workbook cell:(DHCell *)cell customColorId:(unsigned int)customColorId hexString:(NSString *)hexString
{
unsigned int components[3];
[self rgbFromHexString:hexString
rgb:components];
[workbook setupNewColors:customColorId
red:components[0]
green:components[1]
blue:components[2]];
[cell fillBGcolorUnsigned:customColorId];
}
我使用下面的方法另一种方法从十六进制字符串中获取 RGB 值。您不需要使用像“#”或“0X”这样的前缀,因为它们已经在此方法中被截断以转换为 RGB。
-(void)rgbFromHexString:(NSString*)hex rgb:(unsigned int [3])components // New
{
NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6)
{
// Gray color
components[0] = 128; // r
components[1] = 128; // g
components[2] = 128; // b
}
// Truncate 0X if it appears
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
if ([cString length] != 6)
{
// Gray color
components[0] = 128; // r
components[1] = 128; // g
components[2] = 128; // b
}
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r = 0, g = 0, b = 0;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
components[0] = r;
components[1] = g;
components[2] = b;
NSLog(@"r - %u g - %u b - %u",components[0],components[1],components[2]);
}
我还在 DHCell 和 cell.cpp 中创建了两个自定义方法来传递 unsigned8_t 而不是 color_name_t。
-(void)fillBGcolorUnsigned:(unsigned8_t)color
{
CELL(aCell)->fillbgcolor(color);
}
void cell_t::fillbgcolor(unsigned8_t color)
{
xf_t * tempXF = xf_t::xfDup(pxf);
tempXF->SetFillBGColor(color);
pxf->UnMarkUsed();
pxf = m_GlobalRecords.findXF(tempXF);
pxf->MarkUsed();
}