1.扩展图片
// 通过颜色生成图片
extension UIImage{
class func createImage(with color: UIColor, size:CGSize) -> UIImage{
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context!.setFillColor(color.cgColor)
context!.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
2.扩展颜色
// 通过16进制生成颜色
extension UIColor {
convenience init(hexColor: String,alpha:CGFloat) {
// 存储转换后的数值
var red:UInt32 = 0, green:UInt32 = 0, blue:UInt32 = 0
// 分别转换进行转换 // 这个地方需要使用到扩展字符串的下标截取
Scanner(string: hexColor[0..<2]).scanHexInt32(&red)
Scanner(string: hexColor[2..<4]).scanHexInt32(&green)
Scanner(string: hexColor[4..<6]).scanHexInt32(&blue)
self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: alpha)
}
}
3.扩展字符串
extension String {
/// String使用下标截取字符串
/// 例: "示例字符串"[0..<2] 结果是 "示例"
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 self[startIndex..<endIndex]
}
}
// md5校验
func md5() -> String {
let str = self.cString(using: String.Encoding.utf8)
let strLen = CUnsignedInt(self.lengthOfBytes(using: String.Encoding.utf8))
let digestLen = Int(CC_MD5_DIGEST_LENGTH)
let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
CC_MD5(str!, strLen, result)
let hash = NSMutableString()
for i in 0 ..< digestLen {
hash.appendFormat("%02x", result[i])
}
result.deinitialize()
return String(format: hash as String)
}
// 对象方法 (获取文件大小)
func getFileSize() -> UInt64 {
var size: UInt64 = 0
let fileManager = FileManager.default
var isDir: ObjCBool = false
let isExists = fileManager.fileExists(atPath: self, isDirectory: &isDir)
// 判断文件存在
if isExists {
// 是否为文件夹
if isDir.boolValue {
// 迭代器 存放文件夹下的所有文件名
let enumerator = fileManager.enumerator(atPath: self)
for subPath in enumerator! {
// 获得全路径
let fullPath = self.appending("/\(subPath)")
do {
let attr = try fileManager.attributesOfItem(atPath: fullPath)
size += attr[FileAttributeKey.size] as! UInt64
} catch {
print("error :\(error)")
}
}
} else { // 单文件
do {
let attr = try fileManager.attributesOfItem(atPath: self)
size += attr[FileAttributeKey.size] as! UInt64
} catch {
print("error :\(error)")
}
}
}
return size
}
}
4.计算字符串高度
// 计算Label高度
static func textHeight(_ text : String , fontSize: CGFloat , maxWidth : CGFloat) -> CGFloat {
return text.boundingRect(with: CGSize.init(width: maxWidth, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: fontSize)], context: nil).size.height
}
5.时间转换星期
/// 时间转换星期
static func getWeekDayFormDate(_ time: Int) -> String {
let weekday: NSArray = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"] as NSArray
let date = Date.init(timeIntervalSince1970: TimeInterval(time))
let calendar = Calendar(identifier: .republicOfChina)
let components = calendar.component(.weekday, from: date)
let weekStr = weekday.object(at: components-1)
return weekStr as! String
}
5.金额单位转换
static func calculatePriceUnit(_ price: Double) -> NSString{
let array = ["元","万元","亿元","万亿元"]
var tempPrice = 0.0
for i in 0..<array.count{
if 0 < price && price < pow(10000, Double(i + 1)) {
tempPrice = i > 0 ? price / pow(10000, Double(i)) : price
var str = NSString(format: "%.0f", tempPrice)
str = "\(str) \(array[i])" as NSString
return str
}
}
return "错误"
}
6.字节单位转换
// 将字节(byte单位转换)
static func transformSize(_ size: Double) -> String {
var newSize: Double = 0
// 小于1024byte
if 0 < size && size < 1024 {
return "\(size)B"
}
// 1B - 1M
else if size < 1024 * 1024 {
newSize = size / 1024.0
let str = String(format: "%.1f", newSize)
return str+"KB"
}
// 1M - 1G
else if size < 1024 * 1024 * 1024 {
newSize = size / (1024.0 * 1024.0)
let str = String(format: "%.1f", newSize)
return str+"M"
}
return "模型错误"
}
网友评论