2010-04-03 11 views
5

sto usando questa categoria (è giusto?) http://www.nightproductions.net/references/dsclickableurltextfield_reference.html#setAttributedStringValueCercando di creare un link con NSTextField

per attuare textfields cliccabili. Ho importato il file di intestazione nella mia classe di controllo e impostare il suo valore di stringa attribuito come questo

NSAttributedString* str = [[NSAttributedString alloc] initWithString:@"http://www.yahoo.com"]; 
[url setAttributedStringValue:(NSAttributedString *)str]; 

[str release]; 

campo Il testo non è selezionabile e non modificabile.

Il valore del campo di testo è impostato ma non è selezionabile e non è un collegamento.

Grazie in anticipo.

+0

tag Aggiunto cacao. – hallski

risposta

9

Ho trovato un altro modo semplice per mostrare i collegamenti in NSTextField. Puoi usare l'HTML. Ecco un breve esempio:

-(NSAttributedString *)stringFromHTML:(NSString *)html withFont:(NSFont *)font 
{ 
    if (!font) font = [NSFont systemFontOfSize:0.0]; // Default font 
    html = [NSString stringWithFormat:@"<span style=\"font-family:'%@'; font-size:%dpx;\">%@</span>", [font fontName], (int)[font pointSize], html]; 
    NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding]; 
    NSAttributedString* string = [[NSAttributedString alloc] initWithHTML:data documentAttributes:nil]; 
    return string; 
} 

Ora è possibile chiamare il metodo per il campo di testo:

// @property IBOutlet NSTextField *tf; 
[tf setAllowsEditingTextAttributes: YES]; 
[tf setSelectable:YES]; 
NSString *credits = @"Visit our <a href=\"http://www.webpage.com\">webpage</a>"; 
[tf setAttributedStringValue:[self stringFromHTML:credits withFont:[tf font]]]; 
+0

Ciò è migliore perché il cursore si trasforma nel cursore del dito e con l'altro metodo il carattere cambia quando si fa clic. –

Problemi correlati