SHARE
TWEET

Untitled

a guest Sep 19th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import UIKit
  2.  
  3. protocol ChangedTitleDelegate {
  4.     func titleChanged(title: String)
  5. }
  6.  
  7. class DetailViewController: UIViewController {
  8.    
  9.     var delegate : ChangedTitleDelegate?
  10.    
  11.     var itemTitle = ""
  12.     var itemDescription = ""
  13.     let attributes : [NSAttributedString.Key: Any] = [
  14.         .font: UIFont.systemFont(ofSize: 23),
  15.         .foregroundColor: UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.5),
  16.     ]    
  17.     var attributedTitle: NSAttributedString {
  18.         return NSAttributedString(string: itemDescription.convertHtml().string, attributes: attributes)
  19.     }
  20.    
  21.     @IBOutlet weak var tableView: UITableView!
  22.    
  23.     override func viewDidLoad() {
  24.         super.viewDidLoad()
  25.        
  26.         delegate?.titleChanged(title: itemTitle)
  27.         setupNavBar()
  28.         tableView.rowHeight = UITableView.automaticDimension
  29.     }
  30.    
  31.     func setupNavBar() {
  32.         title = itemTitle
  33.         navigationItem.largeTitleDisplayMode = .always
  34.     }
  35.    
  36. }
  37.  
  38. // MARK: - Extensions
  39. extension DetailViewController: UITableViewDelegate, UITableViewDataSource {
  40.     func numberOfSections(in tableView: UITableView) -> Int {
  41.         return 1
  42.     }
  43.    
  44.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  45.         return 1
  46.     }
  47.    
  48.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  49.         let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  50.        
  51.         cell.textLabel?.attributedText = attributedTitle
  52.         cell.textLabel?.numberOfLines = 0
  53.        
  54.         return cell
  55.     }
  56. }
  57.  
  58. extension String {
  59.     func convertHtml() -> NSAttributedString {
  60.         guard let data = data(using: .utf8) else { return NSAttributedString() }
  61.         do {
  62.             return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
  63.         } catch { return NSAttributedString() }
  64.     }
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top