我想在 a 中有一个属性字符串NSTextView
。属性字符串有 3 行,每行颜色不同,没有下划线。我希望能够单击(或双击)每一行,这将打印出行号。
问问题
4061 次
1 回答
3
您可以使用 NSMutableAttributeString 的 addAttribute:value:range 将点击行为分配给您的属性字符串。
根据文档:Attributed String。当你的字符串被点击时,它应该调用 TextView 类的 clickedOnLink:atIndex: 或 TextView 委托中的 textView:clickedOnLink:atIndex:。
像这样(在浏览器上编码,小心错误)
NSMutableAttributedString* str = [[NSMutableAttributedString alloc] initWithString: @"Clickable String"];
NSRange range = NSMakeRange(0, [str length]);
[str beginEditing];
[str addAttribute:NSLinkAttributeName value:@"The value of your attr String" range:range];
[str endEditing];
[textBox setAllowsEditingTextAttributes: YES];
[textBox setSelectable: YES];
[textBox setAttributedStringValue: str];
于 2011-06-10T12:13:44.677 回答