Pregunta

Me gustaría tener una cadena atribuida en un NSTextView. La cadena atribuida tiene 3 líneas, cada una de un color diferente y sin subrayar. Quiero poder hacer clic (o hacer doble clic) en cada línea, lo que imprimiría el número de línea.

¿Fue útil?

Solución

Puede usar AddAttribute: Value: Rango de NSMUTableAtTributEdring para asignar el comportamiento de clic a su cadena atribuida.

Según la documentación: Cadena atribuida. Cuando se hace clic en su cadena, debe llamar a ClickedonLink: AtIndex: de TextView Clase o TextView: ClickedonLink: AtIndex: en el Delegado de TextView.

Como este (codificado en el navegador, tenga cuidado con los errores)

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];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top