| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- //
- // NewsDetailViewController.swift
- // MTP2_iOS
- //
- // Created by Muchinfo on 2021/6/22.
- // Copyright © 2021 Muchinfo. All rights reserved.
- //
- import UIKit
- import WebKit
- import WHToast
- import ActionSheetPicker_3_0
- // 新闻详情视图容器控制类
- class NewsDetailViewController: BaseViewController {
-
- // MARK: - 属性列表
- /// 滚动视图
- @IBOutlet weak var scrollView: UIScrollView!
- /// 发布日期
- @IBOutlet weak var date: UILabel!
- /// 发布标题
- @IBOutlet weak var text: UILabel!
- /// 内容状态视图
- @IBOutlet weak var webView: WKWebView! {
- didSet {
- webView.uiDelegate = self
- webView.navigationDelegate = self
- webView.scrollView.showsVerticalScrollIndicator = false
- webView.scrollView.showsHorizontalScrollIndicator = false
- webView.isOpaque = false
- }
- }
- /// 高度约束
- @IBOutlet weak var heightLayoutConstraint: NSLayoutConstraint!
- /// 字体大小
- @IBOutlet weak var font: UIBarButtonItem!
-
- /// titles
- let titles = ["极小", "小", "中", "大", "极大"]
- /// values
- let values: [CGFloat] = [0.8, 0.9, 1.0, 1.5, 2.0]
-
- // MARK: - 生命周期相关
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- /// loding.....
- addLoadingView()
- /// 异常
- guard let obj = takeInfo as? MoSiteColumnDetail else { return }
- /// 发布日期
- date.text = obj.creaedate.isBlank()
- /// 发布标题
- text.text = obj.title
- /// 加载视图
- webView.loadHTMLString(obj.context.getUrlString(), baseURL: nil)
- }
-
- @IBAction fileprivate func onBarButtonItemPressed(_ sender: UIBarButtonItem) {
- switch sender {
- case font:
- let select = values.firstIndex(of: UserDefaultsUtils.fontSize()) ?? 3
- ActionSheetStringPicker.show(withTitle: "设置字体大小", rows: titles, initialSelection: select, doneBlock: { picker, value, index in
- UserDefaultsUtils.setFontSize(CGFloat(self.values[value]))
- /// 异常
- guard let obj = self.takeInfo as? MoSiteColumnDetail else { return }
- /// 加载视图
- self.webView.loadHTMLString(obj.context.getUrlString(), baseURL: nil)
- }, cancel: { picker in
- return
- }, origin: self.view)
- default:
- break
- }
- }
- }
- // MARK: - WKUIDelegate, WKNavigationDelegate
- extension NewsDetailViewController: WKUIDelegate, WKNavigationDelegate {
- /// didFinish
- /// - Parameters:
- /// - webView: webView
- /// - navigation: navigation
- func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
- /// stopAnimating
- _anim?.stopAnimating()
- /// 图片缩放比例不正确问题
- webView.evaluateJavaScript("""
- var imgs = document.getElementsByTagName("img")
- for (var i = 0; i < imgs.length; i++) {
- imgs[i].setAttribute('width', '100%')
- }
- """,completionHandler: nil)
-
- webView.evaluateJavaScript("""
- var oMeta = document.createElement('meta');
- oMeta.content = 'width=device-width, initial-scale=1, user-scalable=0';
- oMeta.name = 'viewport';
- document.getElementsByTagName('head')[0].appendChild(oMeta);
- """,completionHandler: nil)
-
- /// 动态计算webView内容高度
- webView.evaluateJavaScript("document.body.scrollHeight") { [unowned self] (result, error) in
- if let height: Double = result as? Double {
- self.heightLayoutConstraint.constant = CGFloat(height)
- }
- }
- let select = UserDefaultsUtils.fontSize()
- /// 设置字体大小
- webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(select*100)%'", completionHandler: nil)
- /// 设置颜色
- webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#333333'", completionHandler:nil)
- /// 设置webView 内容背景色
- webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.background='#FFFFFF'") { (result, error) in }
- }
-
- /// didFail
- /// - Parameters:
- /// - webView: webView
- /// - navigation: navigation
- /// - error: error
- func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
- WHToast.showMessage(error.localizedDescription, duration: ToastTimer, finishHandler: {})
- }
-
- /// didStartProvisionalNavigation
- /// - Parameters:
- /// - webView: webView
- /// - navigation: navigation
- func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
- /// startAnimating
- _anim?.startAnimating()
- /// 设置webView 内容背景色
- webView.evaluateJavaScript("document.getElementsByTagName('body')[0].style.background='#FFFFFF'") { (result, error) in }
- }
- }
|