1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
import UIKit import CommonCrypto
struct Screen{ static var width:CGFloat{ return UIScreen.main.bounds.width } static var height:CGFloat{ return UIScreen.main.bounds.height } static var scale:CGFloat{ return Screen.width/375.0 } }
extension UIView{ var fy_frame:CGRect{ set{ self.frame = fy_frame } get{ return self.frame } } var fy_origin:CGPoint{ set{ self.frame = CGRect(x: fy_origin.x, y: fy_origin.y, width: self.fy_width, height: self.fy_height) } get{ return self.frame.origin } } var fy_size:CGSize{ set{ self.frame = CGRect(x: self.fy_origin.x, y: self.fy_origin.y, width: fy_size.width, height: fy_size.height) } get{ return self.frame.size } } var fy_width:CGFloat{ set{ self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: fy_width, height: self.frame.size.height) } get{ return self.bounds.size.width } } var fy_height:CGFloat{ set{ self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: fy_height) } get{ return self.bounds.size.height } } var fy_x:CGFloat{ set{ self.frame = CGRect(x: fy_x, y: self.frame.origin.y, width: self.frame.size.width, height: self.frame.size.height) } get{ return self.frame.origin.x } } var fy_y:CGFloat{ set{ self.frame = CGRect(x: self.frame.origin.x, y: fy_y, width: self.frame.size.width, height:self.frame.size.height) } get{ return self.frame.origin.y } }
var fy_maxX:CGFloat{ get{ return self.fy_x+self.fy_width } } var fy_maxY:CGFloat{ get{ return self.fy_y+self.fy_height } } }
extension UIView{ func corner(byRoundingCorners corners: UIRectCorner, radii: CGFloat) { let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii)) let maskLayer = CAShapeLayer() maskLayer.frame = self.bounds maskLayer.path = maskPath.cgPath self.layer.mask = maskLayer } }
enum FYError: Swift.Error { case NOFINT }
extension UINib{ public class func readNibView(str: String) throws -> UIView { let nib = UINib.init(nibName: str, bundle: nil) if let view = nib.instantiate(withOwner: nil, options: nil).first as? UIView { return view } throw FYError.NOFINT } }
extension UIColor{ func fy_RGB(red:CGFloat,green:CGFloat,blue:CGFloat) -> UIColor { return fy_RGBA(red: red, green: green, blue: blue, alpha: 1.0) } func fy_RGBA(red:CGFloat,green:CGFloat,blue:CGFloat,alpha:CGFloat) -> UIColor { return UIColor.init(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha) } func fy_hexColor(hexColor:String,defaultStringColor:String = "000000",alpha:CGFloat = 1.0) -> UIColor { var cString: String = hexColor.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) if cString.count < 6 { cString = defaultStringColor } if cString.hasPrefix("0X") || cString.hasPrefix("0x") { cString = String(cString.suffix(from: cString.index(cString.startIndex, offsetBy: 2))) } if cString.hasPrefix("#") { cString = String(cString.suffix(from: cString.index(cString.startIndex, offsetBy: 1))) } if cString.count != 6 { cString = defaultStringColor } var range: NSRange = NSMakeRange(0, 2) let rString = (cString as NSString).substring(with: range) range.location = 2 let gString = (cString as NSString).substring(with: range) range.location = 4 let bString = (cString as NSString).substring(with: range) var r: UInt32 = 0x0 var g: UInt32 = 0x0 var b: UInt32 = 0x0 Scanner(string: rString).scanHexInt32(&r) Scanner(string: gString).scanHexInt32(&g) Scanner(string: bString).scanHexInt32(&b) return fy_RGBA(red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), alpha: alpha) } }
extension String{ static let EMPTY = "" subscript (r: Range<Int>) -> String { get { let startIndex = self.index(self.startIndex, offsetBy: r.lowerBound) let endIndex = self.index(self.startIndex, offsetBy: r.upperBound) return String(self[startIndex..<endIndex]) } } func fy_start(length n: Int) -> String { if self.count < n { return self } if n < 0 { return self } let index = self.index(self.startIndex, offsetBy: n) return String(self[..<index]) } func fy_last(length n: Int) -> String { if self.count < n { return self } let index = self.index((self.endIndex), offsetBy: -n) return String(self[index...]) } func fy_trimAll() -> String { return self.replacingOccurrences(of: " ", with: String.EMPTY) } func fy_isPassWord() -> Bool { let numberRegex:NSPredicate = NSPredicate(format: "SELF MATCHES %@", "^.*[0-9]+.*$") let letterRegex:NSPredicate = NSPredicate(format: "SELF MATCHES %@", "^.*[A-Za-z]+.*$") if numberRegex.evaluate(with: self) && letterRegex.evaluate(with: self){ return true } return false } var fy_md5: String { let cStr = self.cString(using: .utf8) let digestLen = Int(CC_MD5_DIGEST_LENGTH) let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: digestLen) CC_MD5(cStr!,(CC_LONG)(strlen(cStr!)), buffer) let md5String = NSMutableString() for i in 0 ..< digestLen { md5String.appendFormat("%02X", buffer[i]) } free(buffer) return md5String as String } var fy_base64Encoding: String { let data = self.data(using: String.Encoding.utf8) var base64String = data?.base64EncodedString(options: .lineLength76Characters) base64String = base64String?.replacingOccurrences(of: "\r", with: "") return base64String ?? "" } var fy_base64Decoded: String { let decodedData = Data(base64Encoded: self, options: Data.Base64DecodingOptions(rawValue: 0)) return String(data: decodedData!, encoding: String.Encoding.utf8) ?? "" } func fy_getSecretMobileNo() -> String { if self.isEmpty { return self } if self.count > 7 { return "\(self.fy_start(length: 3))****\(self.fy_last(length:4))" }else{ return self; } } func fy_startIndexToLenght(_ start: Int, lenght: Int) -> String { if start > self.count { return self } let startIndex = self.index(self.startIndex, offsetBy: start) if self.count < (start + lenght) { return String(self.suffix(from: startIndex)) } let endIndex = self.index(self.startIndex, offsetBy: start + lenght) return String(self[startIndex..<endIndex]) } func fy_isIncludeChineseIn() -> Bool { for(_, value) in self.enumerated() { if value >= "\u{4E00}" && value <= "\u{9FA5}" { return true } } return false }
func fy_isPeriodEmail() -> Bool { let email = "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" let regextestEmails = NSPredicate(format: "SELF MATCHES %@", email) return regextestEmails.evaluate(with: self) } func fy_isPeriodIdCard() -> Bool { if self.count != 18 { return false } let regex2 = "^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$" let identityCardPredicate = NSPredicate(format: "SELF MATCHES %@", regex2) let flag = identityCardPredicate.evaluate(with: self) if !flag { return flag } else { let idCardWiArray = ["7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"] let idCardYArray = ["1", "0", "10", "9", "8", "7", "6", "5", "4", "3", "2"] var idCardArr = [Int]() var idCardWiSum = 0 for index in 0..<17 { let subStrIndex = fy_startIndexToLenght(index, lenght: 1) let idCardWiIndex = idCardWiArray[index] idCardArr.append(Int(subStrIndex)! * Int(idCardWiIndex)!) } for index in 0..<idCardArr.count { idCardWiSum += idCardArr[index] } let idCardMod = idCardWiSum % 11 let idCardLast = fy_startIndexToLenght(17, lenght: 1) if idCardMod == 2 { if idCardLast == "X" || idCardLast == "x" { return true } else { return false } } else { if idCardLast == idCardYArray[idCardMod] { return true } else { return false } } } } func fy_checkPassword() -> Bool { let password = "^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,12}" let regextestPassword = NSPredicate(format: "SELF MATCHES %@", password) return regextestPassword.evaluate(with:self) } func fy_contains(find: String) -> Bool{ return self.range(of: find) != nil } func fy_toInt() -> Int? { return Int(self) } func fy_verifyPhoneNumber() -> Bool { let mobile = "^1((3[0-9]|4[5-68-9]|5[0-35-9]|6[6]|7[0-9]|8[0-9]|9[89])\\d{8}$)" let regextestmobile = NSPredicate(format: "SELF MATCHES %@",mobile) return regextestmobile.evaluate(with: self) } func fy_landlineNumber() -> Bool { let strNum = "^(0\\d{2,3}-?\\d{7,8}$)" let checktest = NSPredicate.init(format: "SELF MATCHES %@", strNum) let isPhone = checktest.evaluate(with: self) let strNum1 = "^(\\d{7,8}$)" let checktest1 = NSPredicate.init(format: "SELF MATCHES %@", strNum1) let isPhone1 = checktest1.evaluate(with: self) if isPhone || isPhone1 { return true } else { return false } }
}
extension Array{ func arrayToString() -> String? { if let data = try? JSONSerialization.data(withJSONObject: self, options: JSONSerialization.WritingOptions.init(rawValue: 0)) { if let jsonStr = String.init(data: data, encoding: String.Encoding.utf8) { return jsonStr } } return nil } }
|