Advertisement
lcolli98

ContentItemView.swift

Mar 21st, 2020
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.62 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. import AVKit
  12. import AVFoundation
  13.  
  14. class ContentItemView: UIView {
  15.    
  16.     @IBOutlet weak var ContentTitle: UILabel!
  17.     @IBOutlet weak var ContentText: UITextView!
  18.    
  19.     var currentContent = 0
  20.     var contentList: [Content]!
  21.    
  22.     func reload() {
  23.         // Set the title
  24.         ContentTitle.text = contentList[currentContent].title
  25.        
  26.         // Set the content text
  27.         let htmlString = contentList[currentContent].content
  28.  
  29.         do {
  30.             // Parse HTML
  31.             guard let doc: Document = try? SwiftSoup.parse(htmlString!) else { return }
  32.             // Create an NSMutableAttributedString that we'll append everything to
  33.             let fullString = NSMutableAttributedString(string: "")
  34.  
  35.             // Get all images and videos from HTML content, adding them to the "full string"
  36.             for element in try doc.select(".content").array() {
  37.                 if (element.tagName() == "img") {
  38.                     // It's an image
  39.                     let image1Attachment = NSTextAttachment()
  40.                     image1Attachment.image = convertBase64ToImage(imageString: try element.attr("src"))
  41.                     let image1String = NSAttributedString(attachment: image1Attachment)
  42.                     fullString.append(image1String)
  43.                 } else if (element.tagName() == "video") {
  44.                     // It's a video
  45.                     print("It is a video")
  46.                     // Insert a play symbol
  47.                     let videoAttachment = NSTextAttachment()
  48.                     videoAttachment.image = UIImage(named: "first.png")
  49.                     let videoString = NSAttributedString(attachment: videoAttachment)
  50.                     fullString.append(videoString)
  51.                     print("I printed a video play symbol")
  52.                    
  53.                     // Set the play symbol to the video
  54.                     /*
  55.                      let videoURL = URL(string: try element.attr("src"))
  56.                      let player = AVPlayer(URL: videoURL!)
  57.                      let playerLayer = AVPlayerLayer(player: player)
  58.                      playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
  59.                      playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
  60.                      self.view.layer.addSublayer(playerLayer)
  61.                      player.play()
  62.                      */
  63.                 } else {
  64.                     // It's not an image or video
  65.                     let div_data = try element.html().data(using: String.Encoding.unicode)!
  66.                     let formattedString = try? NSAttributedString(
  67.                     data: div_data,
  68.                     options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
  69.                     documentAttributes: nil)
  70.                     fullString.append(formattedString!)
  71.                 }
  72.             }
  73.             ContentText.attributedText = fullString
  74.             print(ContentTitle!, " ATTR STRING: ", ContentText.attributedText!)
  75.         } catch {
  76.             print("There was an error parsing the HTML")
  77.         }
  78.     }
  79. }
  80.  
  81. func convertBase64ToImage(imageString: String) -> UIImage {
  82.     let imageString2 = String( imageString.dropFirst( 22 ) )
  83.     let imageData = Data(base64Encoded: imageString2, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
  84.     return UIImage(data: imageData)!
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement