另一种可能性是在要隐藏的文本上使用自定义属性,然后在一个类别中编写自己的方法NSAttributedString
,创建一个新的属性字符串,排除标记为隐藏的文本。
- (NSAttributedString *)attributedStringWithoutHiddenText {
NSMutableAttributedString *result = [[[NSMutableString alloc] init] autorelease];
NSRange fullRange = NSMakeRange(0, [self length]);
NSRange range = NSZeroRange;
while (NSMaxRange(range) < [self length]) {
NSDictionary *attributes = [self attributesAtIndex:range.location longestEffectiveRange:&range inRange:fullRange];
if ([[attributes objectForKey:MyHiddenTextAttribute] boolValue])
continue;
NSAttributedString *substring = [[NSAttributedString alloc] initWithString:[[self string] substringWithRange:range] attributes:attributes];
[result appendAttributedString:substring];
[substring release];
}
return result;
}
警告:我完全只是在脑海中写下这个,并且不能保证编译,工作,点燃你的硬盘驱动器,不踢你的狗等。
这将生成一个适合绘图的字符串,但您仍然需要原始字符串来访问任何隐藏的文本。根据字符串的大小,这可能是一个很大的内存开销。