Advertisement
lcolli98

Untitled

Jan 26th, 2020
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.39 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.    
  21.     func reload() {
  22.         ContentTitle.text = contentList[currentContent].title
  23.         //ContentText.text = contentList[currentContent].isVideo ? contentList[currentContent].description : contentList[currentContent].content
  24.        
  25.         var htmlString: String?
  26.         htmlString = contentList[currentContent].content
  27.  
  28.         if (htmlString != nil) {
  29.             let data = htmlString!.data(using: String.Encoding.unicode)!
  30.             let attrStr = try? NSAttributedString(
  31.                 data: data,
  32.                 options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
  33.                 documentAttributes: nil)
  34.             ContentText.attributedText = attrStr
  35.         }
  36.     }
  37.    
  38.     /*
  39.     func reload() {
  40.         ContentTitle.text = contentList[currentContent].title
  41.         do {
  42.             let html = contentList[currentContent].content!
  43.             guard let doc: Document = try? SwiftSoup.parse(html) else { return }
  44.             let data = try doc.html().data(using: String.Encoding.unicode)!
  45.             let attrStr = try? NSAttributedString(
  46.                 data: data,
  47.                 options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
  48.                 documentAttributes: nil)
  49.             ContentText.attributedText = attrStr
  50.             //ContentText.text = try doc.html()
  51.             print("DOC: ", doc)
  52.             print("DOC.TEXT: ", try doc.text())
  53.         } catch {
  54.             print("There was an error parsing the HTML")
  55.         }
  56.     }
  57.  */
  58. }
  59.  
  60. extension NSAttributedString {
  61.     func attributedStringWithResizedImages(with maxWidth: CGFloat) -> NSAttributedString {
  62.         let text = NSMutableAttributedString(attributedString: self)
  63.         text.enumerateAttribute(NSAttributedString.Key.attachment, in: NSMakeRange(0, text.length), options: .init(rawValue: 0), using: { (value, range, stop) in
  64.             if let attachement = value as? NSTextAttachment {
  65.                 let image = attachement.image(forBounds: attachement.bounds, textContainer: NSTextContainer(), characterIndex: range.location)!
  66.                 if image.size.width > maxWidth {
  67.                     let newImage = image.resizeImage(scale: maxWidth/image.size.width)
  68.                     let newAttribut = NSTextAttachment()
  69.                     newAttribut.image = newImage
  70.                     text.addAttribute(NSAttributedStringKey.attachment, value: newAttribut, range: range)
  71.                 }
  72.             }
  73.         })
  74.         return text
  75.     }
  76. }
  77.  
  78. extension UIImage {
  79.     func resizeImage(scale: CGFloat) -> UIImage {
  80.         let newSize = CGSize(width: self.size.width*scale, height: self.size.height*scale)
  81.         let rect = CGRect(origin: CGPoint.zero, size: newSize)
  82.  
  83.         UIGraphicsBeginImageContext(newSize)
  84.         self.draw(in: rect)
  85.         let newImage = UIGraphicsGetImageFromCurrentImageContext()
  86.         UIGraphicsEndImageContext()
  87.         return newImage!
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement