UILabel y NSLinkAttributeName: No se puede hacer clic en el enlace


Quiero usar cadenas atribuidas con NSLinkAttributeName para crear enlaces clicables dentro de una instancia UILabel dentro de mi proyecto de iOS 7, que finalmente es posible sin usar bibliotecas externas.

NSURL *url = [NSURL URLWithString:@"http://www.google.com"];

NSDictionary *attr = [NSDictionary dictionaryWithObjectsAndKeys:
                          url, NSLinkAttributeName, nil];

La aplicación del atributo en una cadena muestra el texto en azul y subrayado, pero no sucede nada al hacer clic/tocar. La interacción del usuario está habilitada para la etiqueta. ¿Alguien sabe cómo hacer esto? ¡Gracias!

Author: max.mustermann, 2013-11-08

1 answers

Ahora puedo responder a mi propia pregunta: Ahora estoy usando UITextView en lugar de UILabel. He formateado el UITextView para que se vea y se comporte como mis etiquetas y he agregado:

UITextView *textView = [[UITextView alloc] init];
textView.scrollEnabled = NO;
textView.editable = NO;
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
textView.delegate = self;

No se olvide de establecer el delegado, usted tiene que implementar UITextViewDelegate! Ahora implementamos el método delegado:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange
{
     return YES;
}

Esto abre automáticamente la instancia NSURL-proporcionada de la cadena atribuida en mi pregunta al hacer clic/tocar.

Recuerde: Esto funciona solo en iOS 7, para el soporte heredado que necesita externo bibliotecas

ACTUALIZACIÓN:

Hacer que las UITextView se comporten igual que las etiquetas fue un desastre total al final y me topé con algunos comportamientos horribles de iOS. Terminé usando la biblioteca TTTAttributedLabel que es simplemente genial y me permitió usar etiquetas en lugar de UITextView s.

 46
Author: max.mustermann,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-12 09:49:46