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() {
- ContentTitle.text = contentList[currentContent].title
- //ContentText.text = contentList[currentContent].isVideo ? contentList[currentContent].description : contentList[currentContent].content
- var htmlString: String?
- htmlString = contentList[currentContent].content
- if (htmlString != nil) {
- let data = htmlString!.data(using: String.Encoding.unicode)!
- let attrStr = try? NSAttributedString(
- data: data,
- options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
- documentAttributes: nil)
- ContentText.attributedText = attrStr
- }
- }
- /*
- func reload() {
- ContentTitle.text = contentList[currentContent].title
- do {
- let html = contentList[currentContent].content!
- guard let doc: Document = try? SwiftSoup.parse(html) else { return }
- let data = try doc.html().data(using: String.Encoding.unicode)!
- let attrStr = try? NSAttributedString(
- data: data,
- options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
- documentAttributes: nil)
- ContentText.attributedText = attrStr
- //ContentText.text = try doc.html()
- print("DOC: ", doc)
- print("DOC.TEXT: ", try doc.text())
- } catch {
- print("There was an error parsing the HTML")
- }
- }
- */
- }
- extension NSAttributedString {
- func attributedStringWithResizedImages(with maxWidth: CGFloat) -> NSAttributedString {
- let text = NSMutableAttributedString(attributedString: self)
- text.enumerateAttribute(NSAttributedString.Key.attachment, in: NSMakeRange(0, text.length), options: .init(rawValue: 0), using: { (value, range, stop) in
- if let attachement = value as? NSTextAttachment {
- let image = attachement.image(forBounds: attachement.bounds, textContainer: NSTextContainer(), characterIndex: range.location)!
- if image.size.width > maxWidth {
- let newImage = image.resizeImage(scale: maxWidth/image.size.width)
- let newAttribut = NSTextAttachment()
- newAttribut.image = newImage
- text.addAttribute(NSAttributedStringKey.attachment, value: newAttribut, range: range)
- }
- }
- })
- return text
- }
- }
- extension UIImage {
- func resizeImage(scale: CGFloat) -> UIImage {
- let newSize = CGSize(width: self.size.width*scale, height: self.size.height*scale)
- let rect = CGRect(origin: CGPoint.zero, size: newSize)
- UIGraphicsBeginImageContext(newSize)
- self.draw(in: rect)
- let newImage = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- return newImage!
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement