You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
5.5 KiB
166 lines
5.5 KiB
1 year ago
|
//
|
||
|
// UIColor+HexColor.swift
|
||
|
// RxXMLY
|
||
|
//
|
||
|
// Created by sessionCh on 2017/12/14.
|
||
|
// Copyright © 2017年 sessionCh. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import UIKit
|
||
|
|
||
|
extension String {
|
||
|
|
||
|
// MARK:- 获取字符串大小
|
||
|
func getSize(fontSize: CGFloat) -> CGSize {
|
||
|
let str = self as NSString
|
||
|
|
||
|
let size = CGSize(width: UIScreen.main.bounds.width, height: CGFloat(MAXFLOAT))
|
||
|
return str.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize)], context: nil).size
|
||
|
}
|
||
|
|
||
|
// MARK:- 获取字符串大小
|
||
|
func getSize(font: UIFont) -> CGSize {
|
||
|
let str = self as NSString
|
||
|
|
||
|
let size = CGSize(width: UIScreen.main.bounds.width, height: CGFloat(MAXFLOAT))
|
||
|
return str.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil).size
|
||
|
}
|
||
|
|
||
|
// MARK:- 十六进制的字符串转数字
|
||
|
func hexStringToInt() -> Int {
|
||
|
let str = self.uppercased()
|
||
|
var sum = 0
|
||
|
for i in str.utf8 {
|
||
|
sum = sum * 16 + Int(i) - 48
|
||
|
if i >= 65 {
|
||
|
sum -= 7
|
||
|
}
|
||
|
}
|
||
|
return sum
|
||
|
}
|
||
|
|
||
|
func subStringStart(start: Int) -> String? {
|
||
|
let startIndex = self.index(self.startIndex, offsetBy: start)
|
||
|
return String(self.suffix(from: startIndex))
|
||
|
}
|
||
|
|
||
|
func weekRegion() -> String {
|
||
|
let dateFormatter = DateFormatter()
|
||
|
dateFormatter.dateFormat = "yyyy-MM-dd"
|
||
|
let date = dateFormatter.date(from: self)
|
||
|
let currentStartWeek = date?.startOfWeek
|
||
|
let startWeekStr = dateFormatter.string(from: currentStartWeek!)
|
||
|
let currentEndWeek = date?.endOfWeek
|
||
|
let endWeekStr = dateFormatter.string(from: currentEndWeek!)
|
||
|
let startWeek = startWeekStr.pregReplace(pattern: "-", with: "/")
|
||
|
let endWeek = endWeekStr.pregReplace(pattern: "-", with: "/")
|
||
|
let region = [startWeek, endWeek]
|
||
|
return region.joined(separator: "-")
|
||
|
}
|
||
|
|
||
|
//指定年月的开始日期(2018-08-14)
|
||
|
func startOfMonth() -> String {
|
||
|
var startDateStr: String = ""
|
||
|
let comps = self.components(separatedBy: "-")
|
||
|
if comps.count == 3 {
|
||
|
let calendar = NSCalendar.current
|
||
|
var startComps = DateComponents()
|
||
|
startComps.day = 1
|
||
|
startComps.month = (comps[1] as NSString).integerValue
|
||
|
startComps.year = (comps[0] as NSString).integerValue
|
||
|
let startDate = calendar.date(from: startComps)!
|
||
|
|
||
|
let dateFormatter = DateFormatter()
|
||
|
dateFormatter.dateFormat = "yyyy-MM-dd"
|
||
|
startDateStr = dateFormatter.string(from: startDate)
|
||
|
}
|
||
|
return startDateStr
|
||
|
}
|
||
|
|
||
|
//指定年月的结束日期(2018-08-14)
|
||
|
func endOfMonth() -> String {
|
||
|
var endDateStr: String = ""
|
||
|
let comps = self.components(separatedBy: "-")
|
||
|
if comps.count == 3 {
|
||
|
let calendar = NSCalendar.current
|
||
|
var components = DateComponents()
|
||
|
components.month = 1
|
||
|
components.day = -1
|
||
|
let dateFormatter = DateFormatter()
|
||
|
dateFormatter.dateFormat = "yyyy-MM-dd"
|
||
|
let startDate = dateFormatter.date(from: self.startOfMonth())
|
||
|
let endDate = calendar.date(byAdding: components,
|
||
|
to: startDate!)
|
||
|
endDateStr = dateFormatter.string(from: endDate!)
|
||
|
}
|
||
|
|
||
|
return endDateStr
|
||
|
}
|
||
|
|
||
|
func monthRegion() -> String {
|
||
|
let startMonthStr = self.startOfMonth()
|
||
|
let endMonthStr = self.endOfMonth()
|
||
|
|
||
|
let startMonth = startMonthStr.pregReplace(pattern: "-", with: "/")
|
||
|
let endMonth = endMonthStr.pregReplace(pattern: "-", with: "/")
|
||
|
let region = [startMonth, endMonth]
|
||
|
return region.joined(separator: "-")
|
||
|
}
|
||
|
|
||
|
|
||
|
/// 时间字符串格式转化
|
||
|
///
|
||
|
/// - Parameters:
|
||
|
/// - from: 原来的格式
|
||
|
/// - to: 目标格式
|
||
|
/// - Returns: 时间字符串
|
||
|
func transformDateStrFormatter(from: String, to: String) -> String {
|
||
|
let fromFormatter = DateFormatter()
|
||
|
fromFormatter.dateFormat = from
|
||
|
let fromDate = fromFormatter.date(from: self)
|
||
|
|
||
|
let toFormatter = DateFormatter()
|
||
|
toFormatter.dateFormat = to
|
||
|
let toDateStr = toFormatter.string(from: fromDate!)
|
||
|
return toDateStr
|
||
|
}
|
||
|
|
||
|
|
||
|
/// 判断是否为网页地址
|
||
|
///
|
||
|
/// - Returns: true:是,false:否
|
||
|
func isLinkUrl() -> Bool {
|
||
|
let regex = "[a-zA-Z]+://[^\\s]*"
|
||
|
let regexLink = NSPredicate(format: "SELF MATCHES %@",regex)
|
||
|
if self.isEmpty || regexLink.evaluate(with: self) == false {
|
||
|
return false
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
/// 垂直字符串
|
||
|
var verticalStr: String {
|
||
|
var newStr = ""
|
||
|
for char in self {
|
||
|
newStr.append(char)
|
||
|
newStr.append("\n")
|
||
|
}
|
||
|
return newStr
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
extension String {
|
||
|
// 字符串 => Int
|
||
|
var integerValue: Int {
|
||
|
return (self as NSString).integerValue
|
||
|
}
|
||
|
var floatValue: Float {
|
||
|
return (self as NSString).floatValue
|
||
|
}
|
||
|
func pregReplace(pattern: String, with: String, options: NSRegularExpression.Options = []) -> String {
|
||
|
let regex = try! NSRegularExpression(pattern: pattern, options: options)
|
||
|
return regex.stringByReplacingMatches(in: self, options: [], range: NSMakeRange(0, self.count), withTemplate: with)
|
||
|
}
|
||
|
}
|