2012-01-14 14 views
5

Ho trovato questo piccolo frammento che permette di creare collegamenti da testo in un NSTextView:Creazione di un collegamento in-app in 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]; 
} 

sarebbe possibile avere il punto di collegamento per alcune risorse nella mia applicazione , per esempio a una specifica classe di gestori che è in grado di interpretare i collegamenti e inviare a una vista/controller corrispondente?

risposta

4

È possibile gestire i clic sui collegamenti nel delegato NSTextView, in particolare implementando il metodo textView:clickedOnLink:atIndex:.

Se avete bisogno di memorizzare più informazioni con ogni collegamento, si può fare questo, memorizzando un oggetto come un attributo personalizzato della stringa con il link:

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

Assicurarsi che se si sta salvando il attribuiti stringa che è use the NSCoding protocol e non i metodi RTF di NSAttributedString perché RTF non può memorizzare attributi personalizzati.

Problemi correlati