1

J'utilise NSAttributedString pour convertir chaîne html à attributedString. Je l'ai converti mais label dans la cellule ainsi j'ai écrit le code ci-dessous dans cellForRowAtIndex, quand j'applique ce codeview de table ne défilant pas lisse. Si je supprime ceci avec un simple texte, il défile doucement.UITableView défilement pas lisse en raison de NSAttributedString

cell.lblDescription.setHTMLFromString(htmlText: model.strItineraryDescription) 

je convertir html string à attributed string

extension UILabel { 

    func setHTMLFromString(htmlText: String) { 
     let modifiedFont = NSString(format:"<span style=\"font-family: '-apple-system', 'HelveticaNeue'; font-size: \(self.font!.pointSize)\">%@</span>" as NSString, htmlText) as String 


     //process collection values 
     let attrStr = try! NSAttributedString(
      data: modifiedFont.data(using: .unicode, allowLossyConversion: true)!, 
      options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], 
      documentAttributes: nil) 


     self.attributedText = attrStr 
    } 

} 

Mon html string est

<strong><u>Featured Program</u></strong></p> 
\n<ol> 
\n <li>Arrival at Delhi airport. You will receive by our representative.</li> 
\n <li>Driving to Agra and exploring the city.</li> 
\n <li>Back to Hotel for Overnight stay.</li> 
\n</ol> 
\n<p><strong>City Features</strong></p> 
\n<ol> 
\n <li><strong>Visit to Agra Fort \U2013 </strong>Former Name Badalgarh, Shah-Jahan spent his last eight years here. You can watch the mesmerizing view of Taj Mahal from this fort just like Shah Jahan.</li> 
\n <li><strong>Visit to Taj Mahal \U2013</strong> It took 21 years to build and stood in 1653. The palace is considered as the symbol of love. Shah Jahan built this wonder in the memory of his wife Mumtaj Mahal.</li> 
\n</ol> 
\n<p><strong>(Both Agra Fort and Taj Mahal are UNESCO Declared world heritage site)</strong> 
+0

Réponse de NeverHopeless est le moyen idéal pour mettre en œuvre vos besoins. @NeverHopeless bien !!! +1 – Shardul

+0

@Shardul, merci. – NeverHopeless

Répondre

2

Qu'est-ce que cela html2AttributedString faire? Convertit-il HTML en chaîne attribuée à la volée sur cellForRowAtIndexPath? Si oui, c'est ce qui prend du temps. Pouvons-nous faire des compromis sur la mémoire pour accélérer la mise en cache de la chaîne d'attributs équivalente du HTML dans une autre variable du modèle. Ainsi, lors du rechargement, la chaîne d'attributs préparée sera récupérée lors de la création ou de la réutilisation de la cellule.

par exemple, psuedocode:

class MyModel 
{ 
    var myHTML: String = "" 
    var myHTML2AttributedString: String = "" 
} 


class ModelMapping 
{ 
    ... 
    myModel.myHTML = responseFromJSON["htmlText"] 
    myModel.myHTML2AttributedString = customFuncToConvertHTMLToAttributed(myModel.myHTML) 
    ... 
} 

class ViewController 
{ 
    ... 
    cell.lblDescription.attributedText = myModel.myHTML2AttributedString // This one would be cached Attributed string equivalent of HTML string. 
    ... 
} 

Hope that helps!

EDIT:

class MyModel 
{ 
    var myAttributedHTML: NSMutableAttributedString = "" 
    var strItineraryDescription: String = "" 

    func prepareHTMLFromString() { 
     let modifiedFont = NSString(format:"<span style=\"font-family: '-apple-system', 'HelveticaNeue'; font-size: \(self.font!.pointSize)\">%@</span>" as NSString, self.strItineraryDescription) as String 


     //process collection values 
     let attrStr = try! NSAttributedString(
      data: modifiedFont.data(using: .unicode, allowLossyConversion: true)!, 
      options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], 
      documentAttributes: nil) 


     myAttributedHTML = attrStr.mutableCopy() 
    } 
} 

class MyViewController 
{ 
    ... 
    cell.lblDescription.attributedText = myModel.myAttributedHTML 
    ... 
} 

class ResponseHandler 
{ 
    ... 
    myModel.strItineraryDescription = responseFromServer["myHTML"] 
    myModel.prepareHTMLFromString() 
    ... 
} 
+0

S'il vous plaît vérifier la question éditée @NaverHopeless –

+1

@VinodKumar, ok, ce que je dis ici est de ne pas préparer votre chaîne attribuée à un moment d'affichage dans la cellule, au lieu de le préparer en même temps lorsque vous obtenez la valeur de 'model.strItineraryDescription ', et sur l'affichage appel' cell.lblDescription.yourPropertyInModelObjectThatContainsAttributedString'. Je l'ai ? – NeverHopeless

+0

J'ai aussi fait de cette façon mais ne fonctionne pas. S'il vous plaît aidez-moi @NeverHopeless –

1

Vous devez utiliser la file d'attente Dispatch

DispatchQueue.global(qos: .background).async { 
    print("This is run on the background queue") 
    let strmy = model.strItineraryDescription.html2AttributedString    

    DispatchQueue.main.async { 
     cell.lblDescription.attributedText = strmy 

    } 
} 
+0

maintenant ma taille de cellule n'augmente pas, j'utilise la hauteur estimée. @KKRocks 'tblView.estimatedRowHeight = 185.0 tblView.rowHeight = UITableViewAutomaticDimension' –

+0

Pouvez-vous s'il vous plaît écrire le code dans le fil principal et voir. – iPeter