Hea*_*ers 4 cocoa cocoa-touch nsattributedstring core-text
我在没有kCTFontAttributeName范围的现有NSAttributedString上调用以下选择器:
[attributedString enumerateAttribute:(NSString *) kCTFontAttributeName
                             inRange:NSMakeRange(0, [attributedString length])
                             options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
                          usingBlock:^(id value, NSRange range, BOOL *stop) {
    NSLog(@"Attribute: %@, %@", value, NSStringFromRange(range));
}];
我得到下面的输出,但我希望没有输出.建议?
Attribute: (null), {0, 27}
Attribute: (null), {27, 1}
Attribute: (null), {28, 1}
Attribute: (null), {29, 1}
Attribute: (null), {30, 1}
简短的回答?-enumerateAttribute:inRange:options:usingBlock:不会做你(或我原先)认为它做的事情.
从名称中,您可以假设它只枚举包含给定属性的接收器的范围.不是这种情况.它总是枚举整个字符串.它为遇到的每次运行调用块.将value传递到块被设置为值该运行给定的属性.如果当前运行不包含给定的属性,它传递nil的value.
因此,对于不包含给定属性的字符串,它仍将触发块,但value始终是nil.对于完全被给定属性覆盖的字符串(具有相同的值),您可以期望块value在字符串中等于该属性的值时触发一次.对于部分是由给定的属性所覆盖的字符串,你会期望块射击多次,有时带value的nil有,有时value等于属性.
希望有所帮助.我也花了一些时间从正确的方向看它.