Advertisement
lcolli98

ContentItemView.swift

Feb 11th, 2020
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.66 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.  
  27.         do {
  28.             // Parse HTML
  29.             guard let doc: Document = try? SwiftSoup.parse(htmlString!) else { return }
  30.             // create an NSMutableAttributedString that we'll append everything to
  31.             let fullString = NSMutableAttributedString(string: "")
  32.  
  33.             // Get all images from HTML content
  34.             for element in try doc.select(".content").array() {
  35.                 print("ELEMENT: ", try element.html())
  36.                 print("-elem tag: ", element.tagName())
  37.                 if (element.tagName() == "img") {
  38.                     // It's an image
  39.                     print("ATTR SRC :", try element.attr("src"))
  40.                     let image1Attachment = NSTextAttachment()
  41.                     image1Attachment.image = convertBase64ToImage(imageString: try element.attr("src"))
  42.                     let image1String = NSAttributedString(attachment: image1Attachment)
  43.                     fullString.append(image1String)
  44.                     print("in the do")
  45.                 } else {
  46.                     print("in the catch")
  47.                     // It's not an image
  48.                     let div_data = try element.html().data(using: String.Encoding.unicode)!
  49.                     let formattedString = try? NSAttributedString(
  50.                     data: div_data,
  51.                     options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
  52.                     documentAttributes: nil)
  53.                     fullString.append(formattedString!)
  54.                 }
  55.             }
  56.             fullString.append(NSAttributedString(string: "End of text"))
  57.             ContentText.attributedText = fullString
  58.         } catch {
  59.             print("There was an error parsing the HTML")
  60.         }
  61.     }
  62. }
  63.  
  64. func convertBase64ToImage(imageString: String) -> UIImage {
  65.     let imageString2 = String( imageString.dropFirst( 22 ) )
  66.     let imageData = Data(base64Encoded: imageString2, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
  67.     return UIImage(data: imageData)!
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement