ios – NSAttributedString颜色测试

ios – NSAttributedString颜色测试,第1张

概述比较或测试NSAttributed字符串的特定颜色属性的正确方法是什么? 作为一个例子,我想知道文本选择是否有红色文本.我已经尝试了几种方法,如下所示,但它们都没有产生匹配.我看到文本在屏幕上变为红色,并且记录该属性返回:UIDeviceRGBColorSpace 1 0 0 1 - (BOOL)isRedWithAttributes:(NSDictionary *)attributes{ 比较或测试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中定义所有颜色,这些颜色应该没有什么区别.

总结

以上是内存溢出为你收集整理的ios – NSAttributedString颜色测试全部内容,希望文章能够帮你解决ios – NSAttributedString颜色测试所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1095338.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存