CSSwiftExtension - 贡献一个非常好用的Swift extension集合

CSSwiftExtension是我个人开源的一个非常有用的Swift extension集合。支持CocoadPods和Carthage。
目前主要包含Foundation和UIKit的两类extension,基本使用如下:

Foundation

String extension

let string = " hello 17,this is my city "
let a = string.cs_trimmed
let b = string.cs_length
aNonUTF8String.cs_utf8String

let regExp_email = "^[a-zA-Z0-9]{1,}@[a-zA-Z0-9]{1,}\\.[a-zA-Z]{2,}$"
cs_validateWithRegExp(regExp: regExp_email)

Array extension

[1,5,10].cs_sum
["a","b","c","a","c"].cs_removeDuplicates()

UIKit

UIApplication extension

UIApplication.shared.cs_appVersion
UIApplication.shared.cs_currentViewController

UIColor extension

imageView.backgroundColor = UIColor(hexString: 0x123456,alpha: 0.5)
imageView.backgroundColor = UIColor.cs_random

UIImage extension

guard let image = UIImage(named: "Model.jpg") else { return }
let a = image.cs_imageMirrored
let b = image.cs_imageCropped(bounds: CGRect(x: 0,y: 0,width: 200,height: 200))
let c = image.cs_imageWithnormalOrientation
let d = image.cs_imageRotatedBydegrees(degrees: 90)
let e = image.cs_imageWithCornerRadius(cornerRadius: 100)
let f = image.cs_imageScaledToSize(targetSize: CGSize(width: 300,height: 300),withOriginalRatio: true)
let g = image.cs_wechatShareThumbnail
let h = image.cs_grayScale

// Thanks to https://github.com/bahlo/SwiftGif for gif support
aimageView.loadGif(name: "Railway")
aimageView.image = UIImage.gif(name: "Railway")

UIView extension

imageView.cs_snapShot()
let aView = AView.cs_loadFromNib("AView") as? AView
aView.cs_cornerRadius(corners: [.bottomLeft,.bottomright],radius: 20)

UIImageView extension

let imageView = UIImageView(frame: CGRect(x: 0,width: 300,height: 500),blurEffectStyle: .light)

UITableView extension

aTableView.cs_removeEmptyFooter()
aTableView.cs_scrollToTop(animated: true)

tableView.cs_register(MyTableViewCell.self)
let cell = tableView.cs_dequeueReusableCell(forIndexPath: indexPath) as MyTableViewCell

UIButton extension

btnTest.cs_acceptEventInterval = 2 // to avoid UIButton's multiple click operation
btnTest.cs_setBackgroundColor(UIColor.blue,for: .normal) // set backgroundColor
btnTest.cs_setBackgroundColor(UIColor.red,for: .highlighted)

CGPoint extension

aPoint.cs_distance(toPoint: bPoint)
CGPoint.cs_distance(fromPoint: aPoint,toPoint: bPoint)

dispatchQueue extension

dispatchQueue.cs_delay(2) {
    print("delay action")
}
dispatchQueue.cs_global {
    print("global action")
    dispatchQueue.cs_main {
        print("main action")
    }
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...