首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSAttributedString颜色测试

NSAttributedString颜色测试
EN

Stack Overflow用户
提问于 2012-12-07 03:16:20
回答 3查看 11.1K关注 0票数 8

比较或测试NSAttributed字符串的特定颜色属性的正确方法是什么?

例如,我想知道文本选择是否有红色文本。我已经尝试了几种方法,正如你在下面看到的,但它们都没有匹配到结果。我看到屏幕上的文本变为红色,记录该属性将返回: UIDeviceRGBColorSpace 1 0 0 1

代码语言:javascript
复制
- (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;
}

下面是我传递测试属性的方法:

代码语言:javascript
复制
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];
            }
}

我不认为这很重要,但为了完整性,下面是我如何将文本设置为红色的。

代码语言:javascript
复制
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.synopsisTextView.attributedText];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:selectedRange];
self.synopsisTextView.attributedText = attributedString;
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-07 10:04:01

Objective-C是C的严格超集。Objective-C对象位于堆上,并通过指针进行跟踪。这样做的最终结果是测试:

代码语言:javascript
复制
[attributes objectForKey:NSForegroundColorAttributeName] == 
[UIColor colorWithRed:1 green:0 blue:0 alpha:1]

测试同一性而不是相等性。也就是说,不是测试两个对象是否具有相同的值,而是测试它们是否是相同的物理对象,位于内存中的相同地址。C不允许操作符重载,因此==操作符在Objective-C中的含义与C中完全相同。您正在比较两个指针。它们恰好指向Objective-C对象,这是无关紧要的。

您可能需要:

代码语言:javascript
复制
[[attributes objectForKey:NSForegroundColorAttributeName] isEqual:
    [UIColor colorWithRed:1 green:0 blue:0 alpha:1]]

这将允许UIColor对象应用它认为将建立相等的任何测试。

唯一可能的警告是,只有当颜色相对于相同的颜色空间定义并具有相同的系数时,UIColor才会认为颜色相等。假设你总是在RGBA中定义你所有的颜色,这不应该有什么不同。

票数 9
EN

Stack Overflow用户

发布于 2012-12-07 10:07:21

尝试测试属性字典中的NSForegroundColorAttributeName对象,而不是NSFontAttributeName,如下所示:

代码语言:javascript
复制
NSAttributedString *selectedString = [attributedString attributedSubstringFromRange:selectedRange];
[selectedString enumerateAttributesInRange:NSMakeRange(0, [selectedString length]) 
                                   options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired 
                                usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) {
    UIColor *fgColor = [attributes objectForKey:NSForegroundColorAttributeName];       
    if ([fgColor isEqual:[UIColor redColor]]) {
        NSLog(@"detected red. set selected");
        [self.redButton setSelected:YES];
    } else {
        NSLog(@"detected not red. do not set selected");
        [self.redButton setSelected:NO];
    }
}
票数 5
EN

Stack Overflow用户

发布于 2012-12-22 03:21:38

您必须为比较创建一个-isEqualToColor:方法。UIColor仅从NSObject继承-isEqual:,如果两个UIColor实例的哈希值相等,则将其视为相等。

这样的方法很容易构建,您只需获得与颜色空间无关的RGB+A值,如果所有这些值都相同,则颜色是相等的。

如果你很懒,你可以使用CGColorEqualToColor,但是如果一个是灰色(2个分量),而另一个是RGBA色(4个分量),那么就不会报告相等。

这可能是苹果没有实现-isEqualToColor:方法的原因,因为如果组件的数量应该起作用,还是仅仅是RGB值,人们的意见会有很大的不同。因此,我建议您找到自己的定义,并相应地实现比较。

PS:你可能会发现isEqual在某些情况下可以工作,因为UIColor保存了一些标准颜色的缓存,如果你请求红色,那么你会得到相同的实例。相同的实例意味着相同的散列,因此“是相等的”。但正如我之前所说的,依赖于实现并不是一个好的风格。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13750860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档