AppShare.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // AppShare.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/6.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import Toast_Swift
  10. import SwifterSwift
  11. import SnapKit
  12. import SwiftPopup
  13. import PKHUD
  14. import SwiftyJSON
  15. //58 + 12 + 10 80
  16. extension UIButton {
  17. @objc func set(image anImage: UIImage?, title: String,
  18. titlePosition: UIView.ContentMode, additionalSpacing: CGFloat, state: UIControl.State){
  19. self.imageView?.contentMode = .center
  20. self.setImage(anImage, for: state)
  21. positionLabelRespectToImage(title: title, position: titlePosition, spacing: additionalSpacing)
  22. self.titleLabel?.contentMode = .center
  23. self.setTitle(title, for: state)
  24. }
  25. private func positionLabelRespectToImage(title: String, position: UIView.ContentMode,
  26. spacing: CGFloat) {
  27. let imageSize = self.imageRect(forContentRect: self.frame)
  28. let titleFont = self.titleLabel?.font!
  29. let titleSize = title.size(withAttributes: [NSAttributedString.Key.font: titleFont!])
  30. var titleInsets: UIEdgeInsets
  31. var imageInsets: UIEdgeInsets
  32. switch (position){
  33. case .top:
  34. titleInsets = UIEdgeInsets(top: -(imageSize.height + titleSize.height + spacing),
  35. left: -(imageSize.width), bottom: 0, right: 0)
  36. imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
  37. case .bottom:
  38. titleInsets = UIEdgeInsets(top: (imageSize.height + titleSize.height + spacing),
  39. left: -(imageSize.width), bottom: 0, right: 0)
  40. imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
  41. case .left:
  42. titleInsets = UIEdgeInsets(top: 0, left: -(imageSize.width * 2), bottom: 0, right: 0)
  43. imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0,
  44. right: -(titleSize.width * 2 + spacing))
  45. case .right:
  46. titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -spacing)
  47. imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  48. default:
  49. titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  50. imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  51. }
  52. self.titleEdgeInsets = titleInsets
  53. self.imageEdgeInsets = imageInsets
  54. }
  55. }
  56. extension UIColor {
  57. ///UIColor转成纯色图片(UIImage)
  58. func asImage(_ size: CGSize) -> UIImage? {
  59. var resultImage: UIImage? = nil
  60. let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  61. UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
  62. guard let context = UIGraphicsGetCurrentContext() else {
  63. return resultImage
  64. }
  65. context.setFillColor(self.cgColor)
  66. context.fill(rect)
  67. resultImage = UIGraphicsGetImageFromCurrentImageContext()
  68. UIGraphicsEndImageContext()
  69. return resultImage
  70. }
  71. }
  72. // 计算文字高度或者宽度与weight参数无关
  73. extension String {
  74. func ga_widthForComment(font: UIFont, height: CGFloat = 15) -> CGFloat {
  75. let rect = NSString(string: self).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
  76. return ceil(rect.width)
  77. }
  78. func ga_heightForComment(fontSize: CGFloat, width: CGFloat) -> CGFloat {
  79. let font = UIFont.systemFont(ofSize: fontSize)
  80. let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
  81. return ceil(rect.height)
  82. }
  83. func ga_heightForComment(fontSize: CGFloat, width: CGFloat, maxHeight: CGFloat) -> CGFloat {
  84. let font = UIFont.systemFont(ofSize: fontSize)
  85. let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
  86. return ceil(rect.height)>maxHeight ? maxHeight : ceil(rect.height)
  87. }
  88. }
  89. extension UIViewController {
  90. func g_toast(_ msg:String) {
  91. self.view.makeToast(msg, duration: 2.0, position: .center)
  92. }
  93. func g_httpFail() {
  94. g_toast("网络错误")
  95. }
  96. func g_delay_excute(_ cmd:(()->())?) {
  97. DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
  98. cmd?()
  99. }
  100. }
  101. func g_real_delay_excute(_ cmd:(()->())?) {
  102. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  103. cmd?()
  104. }
  105. }
  106. }
  107. extension UIDevice{
  108. //判断是否是刘海屏
  109. /// iPhoneX、iPhoneXR、iPhoneXs、iPhoneXs Max等
  110. /// 判断刘海屏,返回true表示是刘海屏
  111. ///
  112. public func isX() -> Bool {
  113. if UIDevice.current.userInterfaceIdiom == .pad {
  114. return false
  115. }
  116. let size = UIScreen.main.bounds.size
  117. let notchValue: Int = Int(size.width/size.height * 100)
  118. if 216 == notchValue || 46 == notchValue {
  119. return true
  120. }
  121. return false
  122. }
  123. }
  124. func g_real_delay_excute(_ cmd:(()->())?) {
  125. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  126. cmd?()
  127. }
  128. }
  129. //延迟执行
  130. func delay(_ delay: Double, closure:@escaping () -> Void) {
  131. DispatchQueue.main.asyncAfter(
  132. deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
  133. }
  134. func g_showHUD(_ message: String) {
  135. HUD.flash(.label(message), delay: 2.0) { _ in
  136. print("License Obtained.")
  137. }
  138. }
  139. ///设置请求超时操作
  140. func g_showProgress(){
  141. HUD.show(.progress)
  142. HUD.hide(afterDelay: 15) { (finished) in
  143. HUD.flash(.error, delay: 1.5)
  144. }
  145. }
  146. func g_showSuccess(){
  147. HUD.flash(.success, delay: 0)
  148. }
  149. func g_showFail(_ message:String?){
  150. if message == nil {
  151. HUD.flash(.error, delay: 1)
  152. }else{
  153. HUD.flash(.labeledError(title: nil, subtitle: message), delay: 2)
  154. }
  155. }
  156. class AppShare: NSObject {
  157. static var username :String = ""
  158. static var client_key :String = ""
  159. static var version :String = "0"
  160. static var os :String = ""
  161. static var token : String = ""
  162. ///登录信息
  163. static var mLoginData : LoginData?
  164. static var tempRole:Int? = 0 //临时变量 有api的时候再处理
  165. static var noHotel :Bool = false
  166. static var register_username:String = ""
  167. static var register_password:String = ""
  168. static var register_truePassword:String = ""
  169. static var register_name:String = ""
  170. static var register_age:String = ""
  171. static var register_roomId:String = ""
  172. static var register_avatar :String = ""
  173. static var tabbarSelected:Bool = false
  174. }