Reputation: 2198
How to display html data in UITextView or UILabel, There is some method this work, but take a lot of time to load and when scrolling
cell.textView.attributedText = NSAttributedString(
data: comments.commentBody.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil,
error: nil)
Upvotes: 2
Views: 4274
Reputation: 11
Dispatching a block to the main queue is done from a background queue to signal that some background processing has finished:
dispatch_queue_t backgroundQueue = dispatch_queue_create("YourQueueName", 0);
dispatch_async(backgroundQueue, ^{
;
//Do background `enter code here`process like forming attributed string from API parameter..
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI for textview ...
});
});
In this case, we are doing a lengthy calculation on a background queue and need to update our UI when the calculation is complete. Updating UI normally has to be done from the main queue so we 'signal' back to the main queue using a second nested dispatch_async
.
Upvotes: 0
Reputation: 442
If the issue is only the slow loading, try to do the network request asynchronously with dispatch_async in a side thread and then update the UI in the main thread (mandatory!)
let commentsQueue:dispatch_queue_t = dispatch_queue_create("comments queue", nil)
dispatch_async(commentsQueue, {
let commentsRequested = NSAttributedString(
data: comments.commentBody.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil,
error: nil)
dispatch_async(dispatch_get_main_queue(), {
cell.textView.attributedText = commentsRequested
})
})
Upvotes: 2
Reputation: 11197
This might be a problem with Apple's HTML to NSAttributedString
API, you can try NSAttributedString+HTML
.
Upvotes: 1