Cómo configurar el texto de UiteXTVIEW para ser en negrita y no se hace clic en

StackOverflow https://stackoverflow.com//questions/22064380

  •  23-12-2019
  •  | 
  •  

Pregunta

Tengo el siguiente html en un UITextView y le gustaría hacerlo en un UITextView

es mi cuerpo para la nota

<a href="/arc/item/21">food item - more item stuff</a>;`

Déjame agregar: Actualmente se muestra como azul y subrayado y no se puede hacer clic.Me gustaría hacerlo en negrita y no se puede hacer clic.He leído los documentos con respecto a linkTextAttributes, pero no habiendo usado esto, es un poco más allá de mí y realmente no veo ninguna manera fácil de manipular esto.¿Cómo simplemente haría el enlace anterior en negrita y negro (no azul) y mantener la naturaleza que no es clickable?

¿Fue útil?

Solución

actualización (solución usando uitextview's linktextattributes )

self.testTextView.editable = NO;
self.testTextView.selectable = YES;
self.testTextView.userInteractionEnabled = NO;  // workaround to disable link - CAUTION: it also disables scrolling of UITextView content
self.testTextView.dataDetectorTypes = UIDataDetectorTypeLink;
self.testTextView.linkTextAttributes = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:14.0f], // NOT WORKING !?
                                         NSForegroundColorAttributeName : [UIColor redColor]};

...

self.testTextView.text = @"Lorem ipsum http://www.apple.com Lorem ipsum";

Como puede ver en comentarios, no pude establecer una nueva fuente a LinkTextAttributes, aunque el atributo de color estaba funcionando como se espera.

Si puede escapar con el atributo de color o algún otro atributo de texto para diseñar sus URL y no tiene que preocuparse por el desplazamiento de UitexTView deshabilitado, entonces esta puede ser su solución.


anterior (solución alternativa)

Si está utilizando Storyboard / XIB, asegúrese de que ha deseleccionado detección -> enlaces para su UiteXTVIEW. Puede hacer su enlace en negrita al configurar su fuente de contenedor a un tipo de letra en negrita. Si desea admitir diferentes estilos de texto / fuentes en un objeto de una cadena, realmente debe buscar nsattributedstring o nsmutableattributedstring .

Ver: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/nsattributedstring_class/reference/reference.html .

Ejemplo :

UIFont *linkFont = [UIFont fontWithName:@"SomeBoldTypeface" size:12];
NSString *link = @"food item - more item stuff";

NSMutableAttributedString *someString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"is my body for the note %@; let me ad", link]];
[someString addAttribute:NSFontAttributeName value:linkFont range:NSMakeRange(24, link.length)];

UITextView *textView = [[UITextView alloc] init];
textView.attributedText = someString;
...

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