gpt4 book ai didi

ios - NSMutableAttributedString 不会显示以非零索引开始的属性

转载 作者:可可西里 更新时间:2023-11-01 03:58:04 26 4
gpt4 key购买 nike

更新:

我创建了一个非常简单的独立项目来演示该错误。如果有人想拉同样的东西,看看他们是否能发现我哪里出错了,我一定会很感激。没有太多代码可以浏览。公共(public) repo 在这里: https://github.com/reidnez/NSAttributedStringBugDemo

我在这里遇到了一个非常奇怪的问题:我有一个表格 View 。每个单元格都有一个包含 1-3 个单词的 title 标签,以及一个包含多个 CSV 关键字的 keywords 标签。我还有一个搜索栏。要求是,当用户在搜索栏中键入内容时,每个单元格的标题和关键字的任何部分匹配都将突出显示。截图:

Everything looks as it should searching on "Fa"

"an" highlights keyword, but not title

第一张图片是好的。在第二张图片中,标题标签的“an”应该突出显示。但是,如您所见,并没有那么多...

正如您在上面看到的,这在“关键字”标签上工作得很好。这两个标签的属性字符串都是由我编写的类别(下面的代码)创建的。对两个字符串都调用了相同的方法,并且似乎与调试器告诉我的行为相同。用户界面讲述了一个不同的故事。

我已经多次调试调试器,在所有情况下,属性字符串似乎都已正确配置。我还验证了其他东西没有调用 [tableView reloadData] 并且我的代码中没有其他地方正在覆盖标签的值。在 cellForRowAtIndexPath 末尾返回单元格之前,这就是调试器中“Fang”的“an”匹配方式:

(lldb) po customCell.entryTitleLabel.attributedText
F{
}an{
NSBackgroundColor = "UIDeviceRGBColorSpace 0.533333 0.835294 0.156863 1";
}g{
}

我觉得不错……这正是我想要的。但是当单元格渲染时,看不到高光!更奇怪的是,作为一项实验,我尝试将标签设置为我在 cellForRow 中创建的完全任意的 attributedString:

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.
}

最佳答案

感谢您写这篇文章...我以为我也快疯了!

在我们等待 Apple 的官方消息时,我想出了一个解决方法(阅读:hack)。

NSDictionary *hackAttribute = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor clearColor], NSBackgroundColorAttributeName, nil];

NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:@"some text..."];
[attributedAddressText setAttributes:hackAttribute range:NSMakeRange(0, attributedText.length)];
// Then set your other attributes as per normal

希望对您有所帮助。

关于ios - NSMutableAttributedString 不会显示以非零索引开始的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25953780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com