2012-01-14 26 views
5

me encontré con este pequeño fragmento de código que permite a uno crear enlaces de texto en un NSTextView:Creación de un enlace dentro de la aplicación en NSTextView

-(void)setHyperlinkWithTextView:(NSTextView*)inTextView 
{ 
    // create the attributed string 
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init]; 

    // create the url and use it for our attributed string 
    NSURL* url = [NSURL URLWithString: @"http://www.apple.com"]; 
    [string appendAttributedString:[NSAttributedString hyperlinkFromString:@"Apple Computer" withURL:url]]; 

    // apply it to the NSTextView's text storage 
    [[inTextView textStorage] setAttributedString: string]; 
} 

¿Sería posible tener el punto de enlace a algún recurso en mi solicitud , por ejemplo, a una clase de controlador específico que es capaz de interpretar los enlaces y enviarlos a una vista/controlador correspondiente.

Respuesta

4

Puede manejar los clics en los enlaces en su delegado NSTextView, específicamente implementando el método textView:clickedOnLink:atIndex:.

Si necesita almacenar más información con cada enlace, se puede hacer esto mediante el almacenamiento de un objeto como un atributo personalizado de la cadena con el enlace:

NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
          yourObject, @"YourCustomAttributeName", 
          @"link", NSLinkAttributeName, 
          nil]; 
NSAttributedString* string = [[[NSAttributedString alloc] initWithString:@"Your string" attributes:attributes] autorelease]; 

Asegúrese de que si usted está ahorrando el atribuirán cadena que use the NSCoding protocol y no los métodos RTF de NSAttributedString porque RTF no puede almacenar atributos personalizados.

Cuestiones relacionadas