简体   繁体   English

如何比较NSAttributedString的属性?

[英]How to compare attributes of a NSAttributedString?

I'm filtering attributed text depending on certain attributes. 我正在根据某些属性过滤属性文本。 Is there a way to compare if ([attrString.attributes isEqualToDictionary:myAttributes]) ? 有没有办法比较if ([attrString.attributes isEqualToDictionary:myAttributes])

You can use -enumerateAttributesInRange:options:usingBlock: 您可以使用-enumerateAttributesInRange:options:usingBlock:

Within the block, you can test whether the attributes of a run are exactly equal to your desired set of attributes using [attrs isEqualToDictionary:myAttributes] . 在块中,您可以使用[attrs isEqualToDictionary:myAttributes]测试运行的属性是否与所需的属性集完全相同。 Be warned: This will return NO if the run contains any attributes you weren't looking for. 警告:如果运行包含您不需要的任何属性,则返回NO

If you only want to see whether the attributes in myAttributes are set (and compare their values), without rejecting other attributes, use [[attrs dictionaryWithValuesForKeys:[myAttributes allKeys]] isEqualToDictionary:myAttributes] . 如果您只想查看myAttributes中的属性是否已设置(并比较它们的值),而不拒绝其他属性,请使用[[attrs dictionaryWithValuesForKeys:[myAttributes allKeys]] isEqualToDictionary:myAttributes]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM