Pregunta

Quiero usar cadenas atribuidas con NSLinkAttributeName Para crear enlaces haciendo clic dentro de un UILabel instancia dentro de mi proyecto iOS 7, que ahora 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 como azul y subrayado, pero no pasa nada en el clic/toque. La interacción del usuario está habilitada para la etiqueta. ¿Alguien sabe cómo hacer esto? ¡Gracias!

¿Fue útil?

Solución

Puedo responder a mi propia pregunta ahora: estoy usando UITextView en vez de UILabel ahora. He formateado el UITextView Mirarse y comportarse como mis etiquetas y agregar:

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 olvide establecer el delegado, debe implementar UITextViewDelegate! Ahora implementamos el método delegado:

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

Esto abre automáticamente el proporcionado NSURL-Instance de la cadena atribuida en mi pregunta en clic/toque.

Recuerde: esto funciona solo en iOS 7, para el soporte heredado necesita bibliotecas externas

ACTUALIZAR:

Haciendo el UITextViewS se comportó al igual que las etiquetas fue un desastre total al final y me topé con algunos horribles iOS-Behaviours. Terminé usando el Tttattributedlabel biblioteca que es simplemente genial y me permitió usar etiquetas en lugar de UITextViews.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top