比较或测试NSAttributed字符串的特定颜色属性的正确方法是什么?
作为一个例子,我想知道文本选择是否有红色文本.我已经尝试了几种方法,如下所示,但它们都没有产生匹配.我看到文本在屏幕上变为红色,并且记录该属性返回:UIDeviceRGBColorSpace 1 0 0 1
- (BOOL)isRedWithAttributes:(NSDictionary *)attributes
{
BOOL isRedWithAttributes = NO;
if (attributes != nil)
{
// if ( [attributes objectForKey:NSForegroundColorAttributeName] == [UIColor redColor] )
// if ( [attributes objectForKey:NSForegroundColorAttributeName] == @"UIDeviceRGBColorSpace 1 0 0 1" )
if ( [attributes objectForKey:NSForegroundColorAttributeName] == [UIColor colorWithRed:1 green:0 blue:0 alpha:1] )
{
isRedWithAttributes = YES;
}
else
{
isRedWithAttributes = NO;
}
}
return isRedWithAttributes;
}
以下是我为测试传递属性的方法:
NSAttributedString *selectedString = [attributedString attributedSubstringFromrange:selectedRange];
[selectedString enumerateAttributesInRange:NSMakeRange(0,[selectedString length])
options:NSAttributedStringEnumerationLongestEffectiveRangeNotrequired
usingBlock:^(NSDictionary *attributes,NSRange range,BOOL *stop)
{
UIFont *fontFace = [attributes objectForKey:NSFontAttributeName];
BOOL isRedWithAttributes = [fontFace isRedWithAttributes:attributes];
if (isRedWithAttributes)
{
NSLog(@"detected red. set selected");
[self.redButton setSelected:YES];
}
else
{
NSLog(@"detected not red. do not set selected");
[self.redButton setSelected:NO];
}
}
我认为这不重要,但为了完整起见,我将如何将文本设置为红色.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.synopsisTextView.attributedText]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:selectedRange]; self.synopsisTextView.attributedText = attributedString;
解决方法
Objective-C是C的严格超集.Objective-C对象存在于堆上并且跟踪via指针.这样的净效果就是测试:
[attributes objectForKey:NSForegroundColorAttributeName] == [UIColor colorWithRed:1 green:0 blue:0 alpha:1]
测试身份而不是平等.也就是说,不是测试两个对象是否具有相同的值,而是测试它们是否是相同的物理对象,位于内存中的同一地址. C不允许运算符重载,因此==运算符在Objective-C中的含义与C中的含义完全相同.您将比较两个指针.他们碰巧指向Objective-C对象既不在这里也不在那里.
你可能想要:
[[attributes objectForKey:NSForegroundColorAttributeName] isEqual:
[UIColor colorWithRed:1 green:0 blue:0 alpha:1]]
这将允许UIColor对象应用它认为将建立相等性的任何测试.
唯一可能的警告是,UIColor只有在相对于相同颜色空间定义并具有相同系数时才认为颜色相等.假设你总是在RGBA中定义所有颜色,这些颜色应该没有什么区别.