Advertisement
lcolli98

Untitled

Jan 29th, 2020
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.20 KB | None | 0 0
  1. //
  2. //  ContentItemView.swift
  3. //  AeroBuddy
  4. //
  5. //  Created by Kieran Samuel Cross on 25/12/2019.
  6. //  Copyright © 2019 Luke Collister. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import SwiftSoup
  11.  
  12. class ContentItemView: UIView {
  13.    
  14.     @IBOutlet weak var ContentTitle: UILabel!
  15.     @IBOutlet weak var ContentText: UILabel!
  16.    
  17.     var currentContent = 0
  18.     var contentList: [Content]!
  19.    
  20.     func reload() {
  21.         // Set the title
  22.         ContentTitle.text = contentList[currentContent].title
  23.        
  24.         // Set the content text
  25.         let htmlString = contentList[currentContent].content
  26.         let data = htmlString!.data(using: String.Encoding.unicode)!
  27.         let attrStr = try? NSAttributedString(
  28.             data: data,
  29.             options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
  30.             documentAttributes: nil)
  31.            
  32.         // Pull all HTML content
  33.         do {
  34.             let html = contentList[currentContent].content!
  35.            
  36.             // Parse HTML
  37.             guard let doc: Document = try? SwiftSoup.parse(html) else { return }
  38.            
  39.             // Get all images from HTML content
  40.             for element in try doc.select("img").array(){
  41.                 try print(element.attr("src"))
  42.             }
  43.            
  44.             // create an NSMutableAttributedString that we'll append everything to
  45.             let fullString = NSMutableAttributedString(string: "")
  46.  
  47.             // create our NSTextAttachment
  48.             let image1Attachment = NSTextAttachment()
  49.             image1Attachment.image = UIImage(named: "")
  50.  
  51.             // wrap the attachment in its own attributed string so we can append it
  52.             let image1String = NSAttributedString(attachment: image1Attachment)
  53.  
  54.             // Add images and text to fullString
  55.             fullString.append(attrStr!)
  56.             fullString.append(image1String)
  57.             fullString.append(NSAttributedString(string: "End of text"))
  58.  
  59.             // draw the result in a label
  60.             ContentText.attributedText = fullString
  61.         } catch {
  62.             print("There was an error parsing the HTML")
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement