1

Ich verwende NSAttributedString zum Konvertieren von HTML-Zeichenfolge in attributedString. Ich habe es aber label in Zelle umgewandelt, also habe ich unter Code in cellForRowAtIndex schreiben, wenn ich diesen Code tableview anwende, der nicht glatt glättet. Wenn ich dies mit einfachem Text entferne, scrollt es sanft.UITableView Scrollen nicht glatt wegen NSAttributedString

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

Ich habe konvertieren html string zu 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 
    } 

} 

Mein html string ist

<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

Antwort von NeverHopeless ist der perfekte Weg, um Ihre Anforderung zu implementieren. @NeverHopeless netter !!! +1 – Shardul

+0

@hardul, danke. – NeverHopeless

Antwort

2

verwenden Warteschlange Was dies bedeutet html2AttributedString tun? Konvertiert es HTML im Handumdrehen in die attributierte Zeichenfolge cellForRowAtIndexPath? Wenn ja, braucht das tatsächlich Zeit. Können wir die Speicherkapazität kompromittieren, um die äquivalente attributierte Zeichenfolge des HTML-Codes in einer anderen Variablen im Modell zwischenzuspeichern? Auf diese Weise wird beim erneuten Laden die vorbereitete attributierte Zeichenfolge während der Zellenerstellung oder Wiederverwendung abgerufen.

z.B. 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. 
    ... 
} 

Hoffnung, das hilft!

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

Bitte überprüfen Sie die bearbeitete Frage @NaverHopeless –

+1

@VinodKumar, ok, was soll ich sagen, bereiten Sie Ihre attributierte Zeichenfolge nicht zur Zeit der Anzeige in der Zelle vor, sondern bereiten Sie sie gleichzeitig vor, wenn Sie den Wert von 'model.strItineraryDescription erhalten ', und rufen Sie' cell.lblDescription.yourPropertyInModelObjectThatContainsAttributedString' auf. Ich habs ? – NeverHopeless

+0

habe ich auch auf diese Weise gemacht aber nicht funktionieren. Bitte helfen Sie mir @NeverHopeless –

1

Sie benötigen Versand

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

Jetzt erhöht sich meine Zellenhöhe nicht, ich verwende geschätzte Höhe. @KKRocks 'tblView.estimatedRowHeight = 185.0 tblView.rowHeight = UITableViewAutomaticDimension' –

+0

Können Sie bitte den Code in Haupt-Thread schreiben und sehen. – iPeter

Verwandte Themen