// // ViewController.swift // WisdomCampusApk // // Created by 方楚豪 on 2021/11/21. // import UIKit import Toast_Swift import SnapKit import MMKV import APNGKit import JFPopup import WebKit class AgreementVC: UIViewController { static let ENTER_KEY:String = "enterMain" static let AGREE_KEY:String = "wcAgreement" let agreeButton = UIButton() var width = 0.0 var height = 0.0 var mmkv:MMKV = MMKV.default()! override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.isHidden = true self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false if mmkv.contains(key: AgreementVC.ENTER_KEY) { let entryCampusAssistantIndex = mmkv.bool(forKey: AgreementVC.ENTER_KEY) if entryCampusAssistantIndex == true { DispatchQueue.main.asyncAfter(deadline: .now()) { AppUtils.shared.showMainController() } } } self.view.backgroundColor = .white self.width = self.view.frame.width self.height = self.view.frame.height print("屏幕尺寸为:宽度:\(width),高度:\(height)") let topBackgroundWidth = 375*width/375 let topBackgroundHeight = 440*width/375 let top = UIView(frame: CGRect(x: 0, y: 0, width: Int(topBackgroundWidth), height: Int(topBackgroundHeight))) let topBackgroundImageView = UIImageView(frame: top.frame) topBackgroundImageView.image = UIImage(named:"静态BG@3x.png") topBackgroundImageView.contentMode = .scaleAspectFill top.addSubview(topBackgroundImageView) do { let topImageWidth = 375*width/375 let topImageHeight = 286*width/375 let topImageYDistance = 124.5*width/375 let image = try APNGImage(named: "dct_00_iSpt") let topImageView = APNGImageView(image: image) topImageView.frame = CGRect(x: 0, y: Int(topImageYDistance), width: Int(topImageWidth), height: Int(topImageHeight)) top.addSubview(topImageView) }catch { } self.view.addSubview(top) let enterButtonWidth = 280*width/375 let enterButtonHeight = 44*width/375 let enterButtonXDistance = (width - enterButtonWidth)/2 let enterButtonYDistance = height - 100*width/375 let enterButtonView = UIView(frame: CGRect(x: enterButtonXDistance, y: enterButtonYDistance, width: enterButtonWidth, height: enterButtonHeight)) let enterButton = UIButton() enterButton.frame = CGRect(x: 0, y: 0, width: enterButtonWidth, height: enterButtonHeight) enterButton.setBackgroundImage(UIImage(named: "登录按钮@2x.png"), for: .normal) enterButton.imageView?.contentMode = .scaleAspectFill enterButton.titleLabel?.font = UIFont.init(name: "PingFang-SC-Medium", size: 18*width/375)//文字大小 enterButton.setTitle("进入智慧校园", for: .normal) //按钮文字 enterButton.setTitleColor(UIColor.white, for: .normal) //文字颜色 enterButtonView.addSubview(enterButton); enterButton.addTarget(self, action: #selector(enterButtonClicked), for: .touchUpInside) self.view.addSubview(enterButtonView) let agreeButtonSideLength = 15*width/375 let agreeButtonXDistance = 90*width/375 let agreeButtonYDistance = enterButtonYDistance + enterButtonHeight + 15*width/375 let agreeButtonView = UIView(frame: CGRect(x: agreeButtonXDistance, y: agreeButtonYDistance, width: agreeButtonSideLength, height: agreeButtonSideLength)) agreeButton.frame = CGRect(x: 0, y: 0, width: agreeButtonSideLength, height: agreeButtonSideLength) agreeButton.setBackgroundImage(UIImage(named: "未打钩@2x.png"), for: .normal) if mmkv.contains(key: AgreementVC.AGREE_KEY) { let agreeAllProtocol = mmkv.bool(forKey: AgreementVC.AGREE_KEY) if (agreeAllProtocol == true) { agreeButton.setBackgroundImage(UIImage(named: "打钩@2x.png"), for: .normal) } } agreeButtonView.addSubview(agreeButton) agreeButton.addTarget(self, action: #selector(agreeButtonClicked), for: .touchUpInside) self.view.addSubview(agreeButtonView) let agreeTextWidth = 190*width/375 let agreeTextHeight = 20*width/375 let agreeTextXDistance = agreeButtonXDistance + agreeButtonSideLength let agreeTextYDistance = agreeButtonYDistance let agreeTextView = UITextView(frame: CGRect(x: agreeTextXDistance, y: agreeTextYDistance, width: agreeTextWidth, height: agreeTextHeight)) agreeTextView.dataDetectorTypes = .all agreeTextView.isEditable = false agreeTextView.isScrollEnabled = false agreeTextView.isSelectable = false agreeTextView.textContainerInset.top = 0 agreeTextView.backgroundColor = .white self.view.addSubview(agreeTextView) //UIlabel的富⽂本设置 let attributeString = NSMutableAttributedString.init(string: "我已阅读并同意服务协议和隐私协议") let pstyle = NSMutableParagraphStyle() pstyle.alignment = NSTextAlignment.left attributeString.addAttribute(NSAttributedString.Key.paragraphStyle, value: pstyle, range: NSMakeRange(0, attributeString.length)) //设置字体颜色 attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: "3f4041".uicolor(), range: NSMakeRange(0, attributeString.length)) //文本所有字符字体PingFang-SC-Regular,字体大小11*width/375 attributeString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "PingFang-SC-Regular", size: 11*width/375)!, range: NSMakeRange(0, attributeString.length)) //文本7开始4个字符字体PingFang-SC-Regular,字体大小11*width/375 attributeString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "PingFang-SC-Regular", size: 11*width/375)!, range: NSMakeRange(7, 4)) //设置字体颜色 attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: "4c77ff".uicolor(), range: NSMakeRange(7, 4)) //文本12开始4个字符字体PingFang-SC-Regular,字体大小11*width/375 attributeString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "PingFang-SC-Regular", size: 11*width/375)!, range: NSMakeRange(12, 4)) //设置字体颜色 attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: "4c77ff".uicolor(), range: NSMakeRange(12, 4)) //文本12开始4个字符字体PingFang-SC-Regular,字体大小11*width/375 attributeString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "PingFang-SC-Regular", size: 11*width/375)!, range: NSMakeRange(11, 1)) //设置字体颜色 attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: "585858".uicolor(), range: NSMakeRange(11, 1)) agreeTextView.attributedText = attributeString agreeTextView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(agreeViewTextClick))) let webView = WKWebView(frame: .zero,configuration: WKWebViewConfiguration()) let myURL = URL(string: "http://www.baidu.com")! let myRequest = URLRequest(url: myURL) webView.load(myRequest) } @objc func enterButtonClicked() { if mmkv.contains(key: AgreementVC.AGREE_KEY) { let agree = mmkv.bool(forKey: AgreementVC.AGREE_KEY) if agree == true { mmkv.set(true, forKey: AgreementVC.ENTER_KEY) DispatchQueue.main.asyncAfter(deadline: .now()) { //show main vc AppUtils.shared.showMainController() } return } } self.view.makeToast("请先同意协议") } @objc func agreeButtonClicked() { if mmkv.contains(key: AgreementVC.AGREE_KEY) { let agree = mmkv.bool(forKey: AgreementVC.AGREE_KEY) if agree == true { agreeButton.setBackgroundImage(UIImage(named: "未打钩@2x.png"), for: .normal) mmkv.set(false, forKey: AgreementVC.AGREE_KEY) return } } agreeButton.setBackgroundImage(UIImage(named: "打钩@2x.png"), for: .normal) mmkv.set(true, forKey: AgreementVC.AGREE_KEY) } @objc func agreeViewTextClick(recognizer: UIGestureRecognizer) { let textView: UITextView = recognizer.view as! UITextView let layoutManager: NSLayoutManager = textView.layoutManager var location: CGPoint = recognizer.location(in: textView) location.x -= textView.textContainerInset.left location.y -= textView.textContainerInset.top var charIndex: Int charIndex = layoutManager.characterIndex(for: location, in: textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil) if charIndex < textView.textStorage.length { if charIndex >= 7 && charIndex < 11 { self.popup.dialog { return getDialog(filename: "服务协议") } }else if (charIndex >= 12 && charIndex < 16) { self.popup.dialog { return getDialog(filename: "隐私协议") } } } } func getDialog(filename: String) -> UIView { let myDialog = UIView(frame: CGRect(x: 0, y: 0, width: 300*width/375, height: 500*width/375)) myDialog.backgroundColor = .white let titleLabel = UILabel.init(frame: CGRect(x: 0, y: 10*width/375, width: 300*width/375, height: 20*width/375)) titleLabel.text = filename titleLabel.font = UIFont.init(name: "PingFang-SC-Medium", size: 20*width/375)//文字大小 titleLabel.textColor = "3f4041".uicolor() titleLabel.textAlignment = .center // 协议内容 let content = getTextFileStr(filename: filename) let contentScrollView = UIScrollView.init(frame: CGRect(x: 0, y: 40*width/375, width: 300*width/375, height: 450*width/375)) contentScrollView.contentSize = CGSize.init(width: 300*width/375, height: 3250*width/375) contentScrollView.showsVerticalScrollIndicator = false let contentLabel = UILabel.init(frame: CGRect(x: 12*width/375, y: 0, width: 280*width/375, height: 450*width/375)) contentLabel.numberOfLines = 0 contentLabel.text = content contentLabel.font = UIFont.init(name: "PingFang-SC-Medium", size: 16*width/375)//文字大小 contentLabel.textColor = "3f4041".uicolor() contentLabel.sizeToFit() contentScrollView.addSubview(contentLabel) myDialog.addSubview(contentScrollView) myDialog.addSubview(titleLabel) myDialog.layer.cornerRadius = 10 return myDialog } func getTextFileStr(filename:String!) -> String! { if let path = Bundle.main.path(forResource: filename, ofType: "txt") { do { let data = try String(contentsOfFile: path, encoding: .utf8) return data } catch { print(error) } } return "" } } extension String { /// 十六进制字符串颜色转为UIColor /// - Parameter alpha: 透明度 func uicolor(alpha: CGFloat = 1.0) -> UIColor { // 存储转换后的数值 var red: UInt64 = 0, green: UInt64 = 0, blue: UInt64 = 0 var hex = self // 如果传入的十六进制颜色有前缀,去掉前缀 if hex.hasPrefix("0x") || hex.hasPrefix("0X") { hex = String(hex[hex.index(hex.startIndex, offsetBy: 2)...]) } else if hex.hasPrefix("#") { hex = String(hex[hex.index(hex.startIndex, offsetBy: 1)...]) } // 如果传入的字符数量不足6位按照后边都为0处理,当然你也可以进行其它操作 if hex.count < 6 { for _ in 0..<6-hex.count { hex += "0" } } // 分别进行转换 // 红 Scanner(string: String(hex[..