更新:
我创建了一个非常简单的独立项目来演示该错误。如果有人想拉同样的东西,看看他们是否能发现我哪里出错了,我一定会很感激的。没有太多代码可以查看。这里的公共回购: https ://github.com/reidnez/NSAttributedStringBugDemo
我在这里遇到了一个非常奇怪的问题:我有一个表格视图。每个单元格都有一个包含 1-3 个单词的标题标签和一个包含多个 CSV 关键字的关键字标签。我也有一个搜索栏。要求是当用户在搜索栏中键入时,每个单元格的标题和关键字的任何部分匹配都会突出显示。截图:
第一张图是好的。在第二张图片中,应突出显示标题标签的“an”。但是,正如你所看到的,没有那么多......
正如您在上面看到的,这在“关键字”标签上非常有效。这两个标签的属性字符串都是由我编写的类别创建的(代码如下)。在两个字符串上调用相同的方法,并且与调试器告诉我的行为相同。UI 讲述了一个不同的故事。
我已经多次通过调试器,并且在所有情况下,属性字符串似乎都已正确配置。我还验证了其他东西没有调用[tableView reloadData]
,并且我的代码中没有其他地方覆盖标签的值。这是在调试器中匹配“Fang”的“an”的样子,就在单元格在末尾返回之前cellForRowAtIndexPath
:
(lldb) po customCell.entryTitleLabel.attributedText
F{
}an{
NSBackgroundColor = "UIDeviceRGBColorSpace 0.533333 0.835294 0.156863 1";
}g{
}
对我来说看起来不错……这正是我想要的。但是当单元格渲染时,看不到任何亮点!更奇怪的是,作为一个实验,我尝试将标签设置为我在 cellForRow 中创建的完全任意的属性字符串:
NSMutableAttributedString *fake = [[NSMutableAttributedString alloc] initWithString:@"Fang"];
[fake addAttribute:NSBackgroundColorAttributeName value:MATCH_TEXT_HILIGHT_COLOR range:NSMakeRange(1, 2)];
customCell.entryTitleLabel.attributedText = fake;
这也失败了。根本没有突出显示......但我可以突出显示 {0, 1} 到 {0, fake.length} 范围内的任何子字符串,并且它的行为符合预期。同样,它似乎拒绝突出显示任何不从索引 0 开始的子字符串——但仅用于标题标签。
我失去理智了吗?我错过了什么?
下面是我的类别...但我相当有信心问题不在于这里,因为它对关键字字符串非常有效,并且(再次)在单元格返回之前似乎正确设置了属性:
-(void)hilightMatchingSubstring:(NSString*)substring color:(UIColor*)hilightColor range:(NSRange)range
{
if ([self.string compare:substring options:NSCaseInsensitiveSearch] == NSOrderedSame) {
[self addAttribute:NSBackgroundColorAttributeName value:hilightColor range:NSMakeRange(0, self.length)];
return;
}
// Sanity check. Make sure a valid range has been passed so that we don't get out-of-bounds crashes. Default to return self wrapped in an attributed string with no attributes.
NSRange selfRange = NSMakeRange(0, self.length);
if (NSIntersectionRange(selfRange, range).length == 0) {
NSLog(@" \n\n\n*** Match range {%lu, %lu} does not intersect main string's range {%lu, %lu}. Aborting *** \n\n\n", (unsigned long)range.location, (unsigned long)range.length, (unsigned long)selfRange.location, (unsigned long)selfRange.length);
return;
}
if (substring.length > 0) {
NSRange movingRange = NSMakeRange(range.location, substring.length);
if (NSMaxRange(movingRange) > self.length) {
return;
}
NSString *movingString = [self.string substringWithRange:movingRange];
while (NSMaxRange(movingRange) < NSMaxRange(range)) {
if ([movingString compare:substring options:NSCaseInsensitiveSearch] == NSOrderedSame) {
[self addAttribute:NSBackgroundColorAttributeName value:hilightColor range:movingRange];
}
movingRange = NSMakeRange(movingRange.location + 1, substring.length);
movingString = [self.string substringWithRange:movingRange];
}
} // This is fine...string leaves properly attributed.
}