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
- class ContentItemView: UIView {
- @IBOutlet weak var ContentTitle: UILabel!
- @IBOutlet weak var ContentText: UILabel!
- var currentContent = 0
- var contentList: [Content]!
- func reload() {
- // Set the title
- ContentTitle.text = contentList[currentContent].title
- // Set the content text
- let htmlString = contentList[currentContent].content
- let data = htmlString!.data(using: String.Encoding.unicode)!
- let attrStr = try? NSAttributedString(
- data: data,
- options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
- documentAttributes: nil)
- // Pull all HTML content
- do {
- // Parse HTML
- guard let doc: Document = try? SwiftSoup.parse(htmlString!) else { return }
- // Get all images from HTML content
- for element in try doc.select("img").array(){
- try print(element.attr("src"))
- }
- // create an NSMutableAttributedString that we'll append everything to
- let fullString = NSMutableAttributedString(string: "")
- // create our NSTextAttachment
- let image1Attachment = NSTextAttachment()
- image1Attachment.image = UIImage(named: "first.png")
- // wrap the attachment in its own attributed string so we can append it
- let image1String = NSAttributedString(attachment: image1Attachment)
- // Add images and text to fullString
- fullString.append(attrStr!)
- fullString.append(image1String)
- fullString.append(NSAttributedString(string: "End of text"))
- // draw the result in a label
- ContentText.attributedText = fullString
- } catch {
- print("There was an error parsing the HTML")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement