Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ContentItemView.swift
- // AeroBuddy
- //
- // Created by Kieran Samuel Cross on 25/12/2019.
- // Copyright © 2019 Luke Collister. All rights reserved.
- //
- import UIKit
- import SwiftSoup
- import AVKit
- import AVFoundation
- class ContentItemView: UIView {
- @IBOutlet weak var ContentTitle: UILabel!
- @IBOutlet weak var ContentText: UITextView!
- var currentContent = 0
- var videoURL: String?
- var contentList: [Content]!
- @objc func buttonAction(sender: UIButton!) {
- let videoURL2 = URL(string: videoURL!)
- let player = AVPlayer(url: videoURL2!)
- let playerLayer = AVPlayerLayer(player: player)
- playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
- playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
- self.view.layer.addSublayer(playerLayer)
- player.play()
- }
- func reload() {
- // Set the title
- ContentTitle.text = contentList[currentContent].title
- // Set the content text
- let htmlString = contentList[currentContent].content
- do {
- // Parse HTML
- guard let doc: Document = try? SwiftSoup.parse(htmlString!) else { return }
- // Create an NSMutableAttributedString that we'll append everything to
- let fullString = NSMutableAttributedString(string: "")
- // Get all images and videos from HTML content, adding them to the "full string"
- for element in try doc.select(".content").array() {
- if (element.tagName() == "img") {
- // It's an image
- let image1Attachment = NSTextAttachment()
- image1Attachment.image = convertBase64ToImage(imageString: try element.attr("src"))
- let image1String = NSAttributedString(attachment: image1Attachment)
- fullString.append(image1String)
- } else if (element.tagName() == "abv") {
- // It's a video
- // Insert a play symbol
- let videoAttachment = NSTextAttachment()
- videoAttachment.image = UIImage(named: "first.png")
- var videoString = NSAttributedString(attachment: videoAttachment)
- fullString.append(videoString)
- videoURL = try element.attr("src")
- } else {
- // It's not an image or video
- let div_data = try element.html().data(using: String.Encoding.unicode)!
- let formattedString = try? NSAttributedString(
- data: div_data,
- options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
- documentAttributes: nil)
- fullString.append(formattedString!)
- }
- }
- ContentText.attributedText = fullString
- if !videoURL!.isEmpty {
- // Make button and add to contenttext
- let buttonHeight: CGFloat = 44
- let contentInset: CGFloat = 8
- //inset the textView
- ContentText.textContainerInset = UIEdgeInsets(top: contentInset, left: contentInset, bottom: (buttonHeight+contentInset*2), right: contentInset)
- let button = UIButton(frame: CGRect(x: contentInset, y: ContentText.contentSize.height - buttonHeight - contentInset, width: ContentText.contentSize.width-contentInset*2, height: buttonHeight))
- button.backgroundColor = .green
- button.setTitle("Test Button", for: .normal)
- button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
- ContentText.addSubview(button)
- }
- print(ContentTitle!, " ATTR STRING: ", ContentText.attributedText!)
- } catch {
- print("There was an error parsing the HTML")
- }
- }
- }
- func convertBase64ToImage(imageString: String) -> UIImage {
- let imageString2 = String( imageString.dropFirst(22))
- let imageData = Data(base64Encoded: imageString2, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
- return UIImage(data: imageData)!
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement