Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- protocol ChangedTitleDelegate {
- func titleChanged(title: String)
- }
- class DetailViewController: UIViewController {
- var delegate : ChangedTitleDelegate?
- var itemTitle = ""
- var itemDescription = ""
- let attributes : [NSAttributedString.Key: Any] = [
- .font: UIFont.systemFont(ofSize: 23),
- .foregroundColor: UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.5),
- ]
- var attributedTitle: NSAttributedString {
- return NSAttributedString(string: itemDescription.convertHtml().string, attributes: attributes)
- }
- @IBOutlet weak var tableView: UITableView!
- override func viewDidLoad() {
- super.viewDidLoad()
- delegate?.titleChanged(title: itemTitle)
- setupNavBar()
- tableView.rowHeight = UITableView.automaticDimension
- }
- func setupNavBar() {
- title = itemTitle
- navigationItem.largeTitleDisplayMode = .always
- }
- }
- // MARK: - Extensions
- extension DetailViewController: UITableViewDelegate, UITableViewDataSource {
- func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return 1
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
- cell.textLabel?.attributedText = attributedTitle
- cell.textLabel?.numberOfLines = 0
- return cell
- }
- }
- extension String {
- func convertHtml() -> NSAttributedString {
- guard let data = data(using: .utf8) else { return NSAttributedString() }
- do {
- return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
- } catch { return NSAttributedString() }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement