Advertisement
lcolli98

ContentItemView.swift

Mar 21st, 2020
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.34 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 videoURL: String?
  21.     var contentList: [Content]!
  22.    
  23.     @objc func buttonAction(sender: UIButton!) {
  24.         let videoURL2 = URL(string: videoURL!)
  25.         let player = AVPlayer(url: videoURL2!)
  26.         let playerLayer = AVPlayerLayer(player: player)
  27.         playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
  28.         playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
  29.         self.view.layer.addSublayer(playerLayer)
  30.         player.play()
  31.     }
  32.    
  33.    
  34.     func reload() {
  35.         // Set the title
  36.         ContentTitle.text = contentList[currentContent].title
  37.        
  38.         // Set the content text
  39.         let htmlString = contentList[currentContent].content
  40.  
  41.         do {
  42.             // Parse HTML
  43.             guard let doc: Document = try? SwiftSoup.parse(htmlString!) else { return }
  44.             // Create an NSMutableAttributedString that we'll append everything to
  45.             let fullString = NSMutableAttributedString(string: "")
  46.  
  47.             // Get all images and videos from HTML content, adding them to the "full string"
  48.             for element in try doc.select(".content").array() {
  49.                 if (element.tagName() == "img") {
  50.                     // It's an image
  51.                     let image1Attachment = NSTextAttachment()
  52.                     image1Attachment.image = convertBase64ToImage(imageString: try element.attr("src"))
  53.                     let image1String = NSAttributedString(attachment: image1Attachment)
  54.                     fullString.append(image1String)
  55.                 } else if (element.tagName() == "abv") {
  56.                     // It's a video
  57.                    
  58.                     // Insert a play symbol
  59.                     let videoAttachment = NSTextAttachment()
  60.                     videoAttachment.image = UIImage(named: "first.png")
  61.                     var videoString = NSAttributedString(attachment: videoAttachment)
  62.                     fullString.append(videoString)
  63.                    
  64.                     videoURL = try element.attr("src")
  65.                 } else {
  66.                     // It's not an image or video
  67.                     let div_data = try element.html().data(using: String.Encoding.unicode)!
  68.                     let formattedString = try? NSAttributedString(
  69.                     data: div_data,
  70.                     options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
  71.                     documentAttributes: nil)
  72.                     fullString.append(formattedString!)
  73.                 }
  74.             }
  75.             ContentText.attributedText = fullString
  76.             if !videoURL!.isEmpty {
  77.                 // Make button and add to contenttext
  78.                 let buttonHeight: CGFloat = 44
  79.                 let contentInset: CGFloat = 8
  80.  
  81.                 //inset the textView
  82.                 ContentText.textContainerInset = UIEdgeInsets(top: contentInset, left: contentInset, bottom: (buttonHeight+contentInset*2), right: contentInset)
  83.  
  84.                 let button = UIButton(frame: CGRect(x: contentInset, y: ContentText.contentSize.height - buttonHeight - contentInset, width: ContentText.contentSize.width-contentInset*2, height: buttonHeight))
  85.  
  86.                 button.backgroundColor = .green
  87.                 button.setTitle("Test Button", for: .normal)
  88.                 button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
  89.  
  90.                 ContentText.addSubview(button)
  91.             }
  92.             print(ContentTitle!, " ATTR STRING: ", ContentText.attributedText!)
  93.         } catch {
  94.             print("There was an error parsing the HTML")
  95.         }
  96.     }
  97. }
  98.  
  99. func convertBase64ToImage(imageString: String) -> UIImage {
  100.     let imageString2 = String( imageString.dropFirst(22))
  101.     let imageData = Data(base64Encoded: imageString2, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
  102.     return UIImage(data: imageData)!
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement