Advertisement
lcolli98

Untitled

Feb 1st, 2020
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.14 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.             // Parse HTML
  35.             guard let doc: Document = try? SwiftSoup.parse(htmlString!) else { return }
  36.            
  37.             // Get all images from HTML content
  38.             for element in try doc.select("img").array(){
  39.                 try print(element.attr("src"))
  40.             }
  41.            
  42.             // create an NSMutableAttributedString that we'll append everything to
  43.             let fullString = NSMutableAttributedString(string: "")
  44.  
  45.             // create our NSTextAttachment
  46.             let image1Attachment = NSTextAttachment()
  47.             image1Attachment.image = UIImage(named: "first.png")
  48.  
  49.             // wrap the attachment in its own attributed string so we can append it
  50.             let image1String = NSAttributedString(attachment: image1Attachment)
  51.  
  52.             // Add images and text to fullString
  53.             fullString.append(attrStr!)
  54.             fullString.append(image1String)
  55.             fullString.append(NSAttributedString(string: "End of text"))
  56.  
  57.             // draw the result in a label
  58.             ContentText.attributedText = fullString
  59.         } catch {
  60.             print("There was an error parsing the HTML")
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement