NSAttributedString 点击次数 [英] NSAttributedString clicks
问题描述
我想在 NSTextView
中有一个属性字符串.属性字符串有 3 行,每行颜色不同,没有下划线.我希望能够在每一行上单击(或双击),这将打印出行号.
I would like to have an attributed string in a NSTextView
. The attributed string has 3 lines, each one a different color and no underline. I want to be able to click (or double click) on each line, which would print out the line number.
推荐答案
您可以使用 NSMutableAttributeString 的 addAttribute:value:range 将点击行为分配给您的属性字符串.
You can use the addAttribute:value:range of NSMutableAttributeString to assign the click behaviour to your attributed string.
根据文档:属性字符串.当您的字符串被点击时,它应该调用 TextView 类的 clickedOnLink:atIndex: 或 TextView 委托中的 textView:clickedOnLink:atIndex: .
According to the documentation : Attributed String. When your string is clicked, it should call clickedOnLink:atIndex: of TextView class or textView:clickedOnLink:atIndex: in the TextView Delegate.
像这样(在浏览器上编码,注意错误)
Like this (coded on the browser, beware of errors )
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];
这篇关于NSAttributedString 点击次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!