我正在尝试检查属性字符串中特定单词的颜色。我可以访问属性,但不能将此属性字典转换为uicolor。
首先,我对我的属性字符串枚举,这是返回的,对于每个不同属性的属性字典。然后,我可以仔细观察每个。
NSAttributedString * attributes = self.textView.attributedText;
[attributes enumerateAttributesInRange:NSMakeRange(0, attributes.length) options:0 usingBlock:^(NSDictionary<NSString *,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
}];
当我返回attrs字典时,我会得到这个(hkwmentionattributename是我正在使用的子类):
{
HKWMentionAttributeName = "<0x610000243450> (HKWMentionsAttribute) text: ABC name 1, id: uY5Vv8QzBxhPoB8jTxUBeBmTaeD3, no range";
NSColor = "UIExtendedSRGBColorSpace 1 0 0 1";
NSFont = "<UICTFont: 0x7fd388558ce0> font-family: ".SFUIText"; font-weight: normal; font-style: normal; font-size: 19.00pt";
}
i然后使用 attrs[@"NSColor"];
访问颜色,然后将其返回如下:
UIExtendedSRGBColorSpace 1 0 0 1
我一生无法弄清楚如何将其变成uicolor,甚至开始使用此对象。
我知道1 0 0 1
是红色,绿色,蓝色和alpha,但我不知道如何访问它们以使用colorWithRed
功能。
此链接似乎暗示我需要更改色彩空间,但它并没有提供更多的解释。
此链接使我觉得我不应该尝试导入NScolor类,因为它是OSX类
我尝试了所有通常的颜色go-tos:尝试以CGColor
,CIColor
,加载CGColorGetComponents
加载,将其加载为NSString
,将其加载为id
,以查看这是否会在此问题上发出更大的光芒。
我觉得这是我对UIColor
和NSColor
对象的理解的问题
@"NSColor"
,尽管偶然地共享AppKit类的名称,但这只是NSForegroundColorAttributeName
常数的原始值。在iOS上,这被记录为UIColor
。因此,您应该只能服用attrs[NSForegroundColorAttributeName]
,将其施放到UIColor
,然后使用它。