base64解码
//MARK:base64编码
/**
* base64编码
*/
func base64Encoding(plainString:String)->String
{
let plainData = plainString.data(using: String.Encoding.utf8)
let base64String = plainData?.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))
return base64String!
}
/**
* base64解码
*/
func base64Decoding(encodedString:String)->String
{
let decodedData = NSData(base64Encoded: encodedString, options: NSData.Base64DecodingOptions.init(rawValue: 0))
let decodedString = NSString(data: decodedData! as Data, encoding: String.Encoding.utf8.rawValue)! as String
return decodedString
}
/// 判断字符串是否为空
///
/// - Parameter string: <#string description#>
/// - Returns: <#return value description#>
func EnterStringIsEmpty(string:String) -> Bool{
if string.count > 0 {
return false
}
return true
}
/// 判断字符串是否为空
///
/// - Parameter string: <#string description#>
/// - Returns: <#return value description#>
func EnterStringIsEmptyOp(string:String?)->Bool{
if string == nil || string == "" {
return true
}
return false
}
//MARK:各种格式判断
/**
* 判断邮箱格式
*/
func EnterIsEmail(email:String) -> Bool {
let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
let isValid = predicate.evaluate(with: email)
return (isValid ? true : false)
}
/**
* 判断中英文输入
*/
func EnterIsChineseAndEnglish(string:String) -> Bool {
let regex = "[\\u4e00-\\u9fa5a-zA-Z0-9]{4,20}"
let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
let isValid = predicate.evaluate(with: string)
return (isValid ? true : false)
}
/**
* 判断只能输入6位数字
*/
func EnterIsPayPasswordStyle(string:String) -> Bool {
let regex = "^[0-9]{6}$"
let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
let isValid = predicate.evaluate(with: string)
return (isValid ? true : false)
}
/**
* 判断只能输入4-20位数字或字母[0-9a-zA-Z]{4,20}
*/
func EnterIsNumbersOrLetters(string:String) -> Bool {
let regex = "[0-9a-zA-Z]{4,20}"
let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
let isValid = predicate.evaluate(with: string)
return (isValid ? true : false)
}
/**
* 输入8-20位数字,大写字母和小写字母(?![0-9A-Z]+$)(?![0-9a-z]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$
*/
func EnterPasswordStyle(string:String) -> Bool {
let regex = "(?![0-9A-Z]+$)(?![0-9a-z]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$"
let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
let isValid = predicate.evaluate(with: string)
return (isValid ? true : false)
}
//MARK: - 颜色
//RGB转换
func RGB(r:CGFloat,g:CGFloat,b:CGFloat) ->UIColor{
return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1.0)
}
//RGBA转换
func RGBA(r:CGFloat,g:CGFloat,b:CGFloat, a:CGFloat) ->UIColor{
return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}
/// 将十六进制颜色转伟UIColor
/// - Returns: UIColor
public func getUIColorWithHex(hexString:String) -> UIColor {
//处理数值
var cString = hexString.uppercased().trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
let length = (cString as NSString).length
//错误处理
if (length < 6 || length > 7 || (!cString.hasPrefix("#") && length == 7)){
return UIColor.white
}
if cString.hasPrefix("#"){
cString = (cString as NSString).substring(from: 1)
}
//字符chuan截取
var range = NSRange()
range.location = 0
range.length = 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 = 0,g:UInt32 = 0,b:UInt32 = 0
//进行转换
Scanner(string: rString).scanHexInt32(&r)
Scanner(string: gString).scanHexInt32(&g)
Scanner(string: bString).scanHexInt32(&b)
//根据颜色值创建UIColor
return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1.0)
}
// MARK: - 根据最大宽度计算宽高
func getLableSize(text: String, maxWidth: CGFloat,font:UIFont) -> CGRect {
let maxSize = CGSize(width: maxWidth, height: 0) //注意高度是0
let size = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin,
attributes: [NSAttributedStringKey.font:font], context: nil)
return size
}
// MARK: - 字典转字符串
func toJSONString(_ dic:[String : Any]) -> String?{
let data = try? JSONSerialization.data(withJSONObject: dic, options: [])
var str = String(data: data!, encoding: String.Encoding.utf8)
str = str?.replacingOccurrences(of: "/", with: "\\/")
return str
}
//MARK: - 时间
/*********时间***********/
///1. 返回当前时间 2018-01-01 23:23
func getNOWTime() -> String {
//获取当前时间
let now = Date()
//创建一个日期格式器
let dformatter = DateFormatter()
dformatter.dateFormat = "yyyy-MM-dd HH:mm"
return dformatter.string(from: now)
}
///1.1 返回当前时间 2018-01-01 23:23:23
func getNOWTimeHHMMSS() -> String {
//获取当前时间
let now = Date()
//创建一个日期格式器
let dformatter = DateFormatter()
dformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dformatter.string(from: now)
}
///2. 返回当前时间 2018-01-01
func getNOWTimeYYYY_MM_dd() -> String {
//获取当前时间
let now = Date()
//创建一个日期格式器
let dformatter = DateFormatter()
dformatter.dateFormat = "yyyy-MM-dd"
return dformatter.string(from: now)
}
///2.1 返回当前时间 2018-01
func getNOWTimeYYYY_MM() -> String {
//获取当前时间
let now = Date()
//创建一个日期格式器
let dformatter = DateFormatter()
dformatter.dateFormat = "yyyy-MM"
return dformatter.string(from: now)
}
///2.2 返回当前时间 20180101
func getNOWTimeYYYYMMDD() -> String {
//获取当前时间
let now = Date()
//创建一个日期格式器
let dformatter = DateFormatter()
dformatter.dateFormat = "yyyyMMDD"
return dformatter.string(from: now)
}
///2.3 返回当前时间 201801
func getNOWTimeYYYYMM() -> String {
//获取当前时间
let now = Date()
//创建一个日期格式器
let dformatter = DateFormatter()
dformatter.dateFormat = "yyyyMM"
return dformatter.string(from: now)
}
///3. 返回当前时间的时间戳
func getTimeIntervalSinceNow()->TimeInterval {
//获取当前时间
let now = Date()
let dformatter = DateFormatter()
dformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
//当前时间的时间戳
let dateStamp:TimeInterval = now.timeIntervalSince1970
return dateStamp
}
///3.1时间字符串转为时间戳
func stringToTimeStamp(stringTime:String?)->TimeInterval {
if let stringTime = stringTime
{
let dfmatter = DateFormatter()
dfmatter.dateFormat="yyyy-MM-dd HH:mm:ss"
let date = dfmatter.date(from: stringTime)
let dateStamp:TimeInterval = date!.timeIntervalSince1970
return dateStamp
}else{
return getTimeIntervalSinceNow()
}
}
///4.1 获取当前日期的前五天的日期
func getBeforeFiveDays() -> [String] {
var arr:[String] = []
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYYMMdd"
let calendar = NSCalendar.current
let com = calendar.dateComponents([.year,.month,.day], from: Date())
var dateCom = DateComponents()
for i in 1...5 {
dateCom.day = com.day! - i
dateCom.month = com.month
dateCom.year = com.year
let day = calendar.date(from: dateCom)
arr.append(dateFormatter.string(from: day!))
}
return arr
}
///4.1.2 获取当前日期的前五天的日期
func getBeforeFiveDays_YYYYMMDD() -> [String] {
var arr:[String] = []
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd"
let calendar = NSCalendar.current
let com = calendar.dateComponents([.year,.month,.day], from: Date())
var dateCom = DateComponents()
for i in 1...5 {
dateCom.day = com.day! - i
dateCom.month = com.month
dateCom.year = com.year
let day = calendar.date(from: dateCom)
arr.append(dateFormatter.string(from: day!))
}
return arr
}
///4.2 获取当前日期的前五个月
func getBeforeFiveMonths() -> [String] {
var arr:[String] = []
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMM"
let calendar = NSCalendar.current
let com = calendar.dateComponents([.year,.month,.day], from: Date())
var dateCom = DateComponents()
for i in 1...5 {
dateCom.month = com.month! - i
dateCom.year = com.year
let day = calendar.date(from: dateCom)
arr.append(dateFormatter.string(from: day!))
}
return arr
}
///4.2.2 获取当前日期的前五个月
func getBeforeFiveMonths_YYYYMM() -> [String] {
var arr:[String] = []
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM"
let calendar = NSCalendar.current
let com = calendar.dateComponents([.year,.month,.day], from: Date())
var dateCom = DateComponents()
for i in 1...5 {
dateCom.month = com.month! - i
dateCom.year = com.year
let day = calendar.date(from: dateCom)
arr.append(dateFormatter.string(from: day!))
}
return arr
}
///4.3 获取当前日期的前一天的日期 20180101
func getBeforeDay() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYYMMdd"
let calendar = NSCalendar.current
let com = calendar.dateComponents([.year,.month,.day], from: Date())
var dateCom = DateComponents()
dateCom.day = com.day! - 1
dateCom.month = com.month
dateCom.year = com.year
let day = calendar.date(from: dateCom)
return dateFormatter.string(from: day!)
}
///4.3.2 获取当前日期的前一个月的日期 201801
func getBeforeMonth() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYYMM"
let calendar = NSCalendar.current
let com = calendar.dateComponents([.year,.month,.day], from: Date())
var dateCom = DateComponents()
dateCom.day = com.day!
dateCom.month = com.month! - 1
dateCom.year = com.year
let day = calendar.date(from: dateCom)
return dateFormatter.string(from: day!)
}
///4.3.3 获取当前日期 201801
func getMonth() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYYMM"
// let calendar = NSCalendar.current
// _ = calendar.dateComponents([.year,.month,.day], from: Date())
// let dateCom = DateComponents()
// dateCom.day = com.day!
// dateCom.month = com.month!
// dateCom.year = com.year
// let day = calendar.date(from: dateCom)
// let day =
return dateFormatter.string(from: Date())
}
/*********时间***********/
//MARK: - 格式化字符串
/// 获取两位小数点的字符串
///
/// - Parameter string: <#string description#>
/// - Returns: <#return value description#>
func getFloat(string:String?) -> String {
if string == nil || string == ""{
return "0.00"
}
let sf:String = NSString.init(format: "%0.2f", Double(string!)!) as String
return sf
}
/// 获取整形字符串
///
/// - Parameter string: <#string description#>
/// - Returns: <#return value description#>
func getInt(string:String?) -> String {
if string == nil || string == ""{
return "0"
}
let sf:String = NSString.init(format: "%d", Int(string!)!) as String
return sf
}
/// 判断是不是全是空格
///
/// - Parameter str: <#str description#>
/// - Returns: <#return value description#>
func isAllApacing(str:String?) -> Bool {
if str == nil {
return true
}
let set = NSCharacterSet.whitespacesAndNewlines
let trimedString = str?.trimmingCharacters(in: set)
if trimedString?.count == 0{
return true
}else{
return false
}
}
//MARK: - 截屏
func cutImageWithView(view:UIView) -> UIImage
{
// 参数①:截屏区域 参数②:是否透明 参数③:清晰度
UIGraphicsBeginImageContextWithOptions(view.frame.size, true, UIScreen.main.scale)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
return image;
}
网友评论