2010-04-03 9 views
5

estoy usando esta categoría (¿es correcto eso?) http://www.nightproductions.net/references/dsclickableurltextfield_reference.html#setAttributedStringValueTratar de crear vínculo con NSTextField

para implementar campos de texto puede hacer clic. He importado el archivo de cabecera en mi clase del controlador y la puse ha atribuido valor de cadena como esta

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

[str release]; 

campo El texto no es seleccionable y no se puede editar.

El valor del campo de texto está establecido pero no se puede hacer clic y no es un enlace.

Gracias de antemano.

+0

etiqueta que se añade cacao. – hallski

Respuesta

9

He encontrado otra manera fácil de mostrar enlaces en NSTextField. Puedes usar HTML. Aquí está un ejemplo corto:

-(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; 
} 

ya se puede llamar al método de campo de texto:

// @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

Esto es mejor porque el cursor cambia al cursor del dedo, y con el otro método la fuente cambia cuando se hace clic. –