CSSwiftExtension icon indicating copy to clipboard operation
CSSwiftExtension copied to clipboard

Some useful extension for Swift.

CSSwiftExtension

Build Status Cocoapods Carthage compatible Platform Swift Xcode MIT

Some useful extension for Swift to boost your productivity.

Requirements:

Xcode 10 (or later) with Swift 5. This library is made for iOS 10 or later.

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate CSSwiftExtension into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'CSSwiftExtension'
end

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate CSSwiftExtension into your Xcode project using Carthage, specify it in your Cartfile:

github "icetime17/CSSwiftExtension"

Run carthage update to build the framework and drag the built CSSwiftExtension.framework in folder /Carthage/Build/iOS into your Xcode project.

Manually

Add the Sources folder to your Xcode project to use all extensions, or a specific extension.

Usage

Foundation

String extension

let string = " hello 17, this is my city "
let a = string.cs_trimmed
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.setCornerRadius(radius: 20)
aView.setCornerRadius(corners: [.bottomLeft, .bottomRight], radius: 20)

UIImageView extension

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

UITableView extension

aTableView.cs.removeEmptyFooter()
aTableView.cs.scrollToTop(animated: true)

tableView.cs_registerNib(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")
    }
}

Contact

If you find an issue, just open a ticket. Pull requests are warmly welcome as well.

License

CSSwiftExtension is released under the MIT license. See LICENSE.md for details.