// // AppShare.swift // Inhealth // // Created by weclouds on 2019/12/6. // Copyright © 2019 weclouds. All rights reserved. // import UIKit import Toast_Swift import SwifterSwift import SnapKit import SwiftPopup import PKHUD import SwiftyJSON //58 + 12 + 10 80 extension UIButton { @objc func set(image anImage: UIImage?, title: String, titlePosition: UIView.ContentMode, additionalSpacing: CGFloat, state: UIControl.State){ self.imageView?.contentMode = .center self.setImage(anImage, for: state) positionLabelRespectToImage(title: title, position: titlePosition, spacing: additionalSpacing) self.titleLabel?.contentMode = .center self.setTitle(title, for: state) } private func positionLabelRespectToImage(title: String, position: UIView.ContentMode, spacing: CGFloat) { let imageSize = self.imageRect(forContentRect: self.frame) let titleFont = self.titleLabel?.font! let titleSize = title.size(withAttributes: [NSAttributedString.Key.font: titleFont!]) var titleInsets: UIEdgeInsets var imageInsets: UIEdgeInsets switch (position){ case .top: titleInsets = UIEdgeInsets(top: -(imageSize.height + titleSize.height + spacing), left: -(imageSize.width), bottom: 0, right: 0) imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width) case .bottom: titleInsets = UIEdgeInsets(top: (imageSize.height + titleSize.height + spacing), left: -(imageSize.width), bottom: 0, right: 0) imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width) case .left: titleInsets = UIEdgeInsets(top: 0, left: -(imageSize.width * 2), bottom: 0, right: 0) imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -(titleSize.width * 2 + spacing)) case .right: titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -spacing) imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) default: titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) } self.titleEdgeInsets = titleInsets self.imageEdgeInsets = imageInsets } } extension UIColor { ///UIColor转成纯色图片(UIImage) func asImage(_ size: CGSize) -> UIImage? { var resultImage: UIImage? = nil let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height) UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale) guard let context = UIGraphicsGetCurrentContext() else { return resultImage } context.setFillColor(self.cgColor) context.fill(rect) resultImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return resultImage } } // 计算文字高度或者宽度与weight参数无关 extension String { func ga_widthForComment(font: UIFont, height: CGFloat = 15) -> CGFloat { let rect = NSString(string: self).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil) return ceil(rect.width) } func ga_heightForComment(fontSize: CGFloat, width: CGFloat) -> CGFloat { let font = UIFont.systemFont(ofSize: fontSize) let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil) return ceil(rect.height) } func ga_heightForComment(fontSize: CGFloat, width: CGFloat, maxHeight: CGFloat) -> CGFloat { let font = UIFont.systemFont(ofSize: fontSize) let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil) return ceil(rect.height)>maxHeight ? maxHeight : ceil(rect.height) } } extension UIViewController { func g_toast(_ msg:String) { self.view.makeToast(msg, duration: 2.0, position: .center) } func g_httpFail() { g_toast("网络错误") } func g_delay_excute(_ cmd:(()->())?) { DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { cmd?() } } func g_real_delay_excute(_ cmd:(()->())?) { DispatchQueue.main.asyncAfter(deadline: .now() + 1) { cmd?() } } } extension UIDevice{ //判断是否是刘海屏 /// iPhoneX、iPhoneXR、iPhoneXs、iPhoneXs Max等 /// 判断刘海屏,返回true表示是刘海屏 /// public func isX() -> Bool { if UIDevice.current.userInterfaceIdiom == .pad { return false } let size = UIScreen.main.bounds.size let notchValue: Int = Int(size.width/size.height * 100) if 216 == notchValue || 46 == notchValue { return true } return false } } func g_real_delay_excute(_ cmd:(()->())?) { DispatchQueue.main.asyncAfter(deadline: .now() + 1) { cmd?() } } //延迟执行 func delay(_ delay: Double, closure:@escaping () -> Void) { DispatchQueue.main.asyncAfter( deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure) } func g_showHUD(_ message: String) { HUD.flash(.label(message), delay: 2.0) { _ in print("License Obtained.") } } ///设置请求超时操作 func g_showProgress(){ HUD.show(.progress) HUD.hide(afterDelay: 15) { (finished) in HUD.flash(.error, delay: 1.5) } } func g_showSuccess(){ HUD.flash(.success, delay: 0) } func g_showFail(_ message:String?){ if message == nil { HUD.flash(.error, delay: 1) }else{ HUD.flash(.labeledError(title: nil, subtitle: message), delay: 2) } } class AppShare: NSObject { static var username :String = "" static var client_key :String = "" static var version :String = "0" static var os :String = "" static var token : String = "" ///登录信息 static var mLoginData : LoginData? static var tempRole:Int? = 0 //临时变量 有api的时候再处理 static var noHotel :Bool = false static var register_username:String = "" static var register_password:String = "" static var register_truePassword:String = "" static var register_name:String = "" static var register_age:String = "" static var register_roomId:String = "" static var register_avatar :String = "" static var tabbarSelected:Bool = false }