NewsDetailViewController.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // NewsDetailViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2021/6/22.
  6. // Copyright © 2021 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import WebKit
  10. import WHToast
  11. import ActionSheetPicker_3_0
  12. // 新闻详情视图容器控制类
  13. class NewsDetailViewController: BaseViewController {
  14. // MARK: - 属性列表
  15. /// 滚动视图
  16. @IBOutlet weak var scrollView: UIScrollView!
  17. /// 发布日期
  18. @IBOutlet weak var date: UILabel!
  19. /// 发布标题
  20. @IBOutlet weak var text: UILabel!
  21. /// 内容状态视图
  22. @IBOutlet weak var webView: WKWebView! {
  23. didSet {
  24. webView.uiDelegate = self
  25. webView.navigationDelegate = self
  26. webView.scrollView.showsVerticalScrollIndicator = false
  27. webView.scrollView.showsHorizontalScrollIndicator = false
  28. webView.isOpaque = false
  29. }
  30. }
  31. /// 高度约束
  32. @IBOutlet weak var heightLayoutConstraint: NSLayoutConstraint!
  33. /// 字体大小
  34. @IBOutlet weak var font: UIBarButtonItem!
  35. /// titles
  36. let titles = ["极小", "小", "中", "大", "极大"]
  37. /// values
  38. let values: [CGFloat] = [0.8, 0.9, 1.0, 1.5, 2.0]
  39. // MARK: - 生命周期相关
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. // Do any additional setup after loading the view.
  43. /// loding.....
  44. addLoadingView()
  45. /// 异常
  46. guard let obj = takeInfo as? MoSiteColumnDetail else { return }
  47. /// 发布日期
  48. date.text = obj.creaedate.isBlank()
  49. /// 发布标题
  50. text.text = obj.title
  51. /// 加载视图
  52. webView.loadHTMLString(obj.context.getUrlString(), baseURL: nil)
  53. }
  54. @IBAction fileprivate func onBarButtonItemPressed(_ sender: UIBarButtonItem) {
  55. switch sender {
  56. case font:
  57. let select = values.firstIndex(of: UserDefaultsUtils.fontSize()) ?? 3
  58. ActionSheetStringPicker.show(withTitle: "设置字体大小", rows: titles, initialSelection: select, doneBlock: { picker, value, index in
  59. UserDefaultsUtils.setFontSize(CGFloat(self.values[value]))
  60. /// 异常
  61. guard let obj = self.takeInfo as? MoSiteColumnDetail else { return }
  62. /// 加载视图
  63. self.webView.loadHTMLString(obj.context.getUrlString(), baseURL: nil)
  64. }, cancel: { picker in
  65. return
  66. }, origin: self.view)
  67. default:
  68. break
  69. }
  70. }
  71. }
  72. // MARK: - WKUIDelegate, WKNavigationDelegate
  73. extension NewsDetailViewController: WKUIDelegate, WKNavigationDelegate {
  74. /// didFinish
  75. /// - Parameters:
  76. /// - webView: webView
  77. /// - navigation: navigation
  78. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  79. /// stopAnimating
  80. _anim?.stopAnimating()
  81. /// 图片缩放比例不正确问题
  82. webView.evaluateJavaScript("""
  83. var imgs = document.getElementsByTagName("img")
  84. for (var i = 0; i < imgs.length; i++) {
  85. imgs[i].setAttribute('width', '100%')
  86. }
  87. """,completionHandler: nil)
  88. webView.evaluateJavaScript("""
  89. var oMeta = document.createElement('meta');
  90. oMeta.content = 'width=device-width, initial-scale=1, user-scalable=0';
  91. oMeta.name = 'viewport';
  92. document.getElementsByTagName('head')[0].appendChild(oMeta);
  93. """,completionHandler: nil)
  94. /// 动态计算webView内容高度
  95. webView.evaluateJavaScript("document.body.scrollHeight") { [unowned self] (result, error) in
  96. if let height: Double = result as? Double {
  97. self.heightLayoutConstraint.constant = CGFloat(height)
  98. }
  99. }
  100. let select = UserDefaultsUtils.fontSize()
  101. /// 设置字体大小
  102. webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(select*100)%'", completionHandler: nil)
  103. /// 设置颜色
  104. webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#333333'", completionHandler:nil)
  105. /// 设置webView 内容背景色
  106. webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.background='#FFFFFF'") { (result, error) in }
  107. }
  108. /// didFail
  109. /// - Parameters:
  110. /// - webView: webView
  111. /// - navigation: navigation
  112. /// - error: error
  113. func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  114. WHToast.showMessage(error.localizedDescription, duration: ToastTimer, finishHandler: {})
  115. }
  116. /// didStartProvisionalNavigation
  117. /// - Parameters:
  118. /// - webView: webView
  119. /// - navigation: navigation
  120. func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  121. /// startAnimating
  122. _anim?.startAnimating()
  123. /// 设置webView 内容背景色
  124. webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.background='#FFFFFF'") { (result, error) in }
  125. }
  126. }