2017-02-28 6 views
2

Comment afficher ci-dessous le texte dans UILabelAffichage des balises HTML à l'intérieur UILabel avec spécifié police

While <em>La La Land </em>is a film emphasising the problems 

J'ai essayé d'utiliser sous le code mais il n'affiche pas le format correct du texte qui est à l'intérieur balise <em> (rendu en italique) lorsque utilisé avec une police spécifiée.

NSMutableAttributedString *attrHTMLText = [[NSAttributedString alloc] initWithData:[yourHTMTextHere dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil]; 
[attrHTMLText addAttribute:NSFontAttributeName value:yourLabel.font range:NSMakeRange(0, attrHTMLText.length)]; 
[attrHTMLText addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, attrHTMLText.length)]; 
Label.attributedText = attrHTMLText; 
+0

C'est parce que l'effet italique est dans le NSFontAttributed. Ainsi, lorsque vous le remplacez, vous remplacez la police WHOLE par une police none italic. Vous devez l'itérer, et trouver la police correspondante (si elle est en italique, mettre votre italique personnalisé, etc.) – Larme

+0

Copie possible de [Rechercher les attributs de la chaîne attribuée que l'utilisateur a tapé] (http://stackoverflow.com/questions/ 23153156/find-attributes-from-attribute-string-that-user-taped) – Larme

Répondre

0

Si vous voulez faire une partie de texte itallic alors vous pouvez le faire en utilisant NSMutableAttributedString

NSMutableAttributedString *strText = [[NSMutableAttributedString alloc] initWithString:@"While La La Land is a film emphasising the problems"]; 
[strText addAttribute:NSFontAttributeName 
       value:[UIFont fontWithName:@"Helvetica-Italic" size:22] 
       range:NSMakeRange(7, 16)]; 
[label setAttributedText: strText];