CWUtilsTest 1.1.1

CWUtilsTest 1.1.1

Ryan Song 维护。



  • 作者:
  • iamchiwon

CWUtils

Version License Platform

安装

CocoaPods

CWUtils 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中。

pod 'CWUtils'

Swift Package Manager

将目标与对应的产品相匹配

依赖关系

示例代码

工具

日志

DLog("log for debug build only")
ELog("error log for all build")

设备

runOnSimulatorOnly {
    print("running on simulator")
}
runOnDeviceOnly {
    print("running on device")
}

版本检查

let currentVersion: String = currentApplicationVersion()
let needUpdate: Bool = isUpdateAvailable()

验证

let isEmail = "[email protected]".isValid(withType: .email)
let isEmail2 = "[email protected]".isValid(withRegEx: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}")

扩展

字符串

let url = "www.apple.com".url()
let base64 = "hello world".base64()
let trimmed = "   hello world   \n \n    ".trim()
let localized = "Hello World".localized()

日期

let serverDateParser = Date.fromFormat("yyyy-MM-dd HH:mm:ss")
let date = serverDateParser("2018-02-17 17:00:00")

数组

struct Data : Equatable {}
var array: [Data]
//--------
let data = Data()
let exists: Bool = array.exists(data)
let index: Int = array.indexOf(data)
let array2 = array.removed(data)
array.remove(item: data) //mutating

可编码

struct Data : Codable {}
//--------
let data = Data()
let dictionary: [String:Any] = data.dictionary()
let jsonString: String = data.json()
let jsonString2: String = dictionary.json()
let data2: Data = dictionary.decode()

用户界面(UI)

UIColor

let red = UIColor(red:255, green:0, blue:0)
let blue = UIColor(0x00FF00)
let green = UIColor("#0000FF")

子视图(subview)

//view with tag
view.label(100)?.text = "Hello World"
//image with tag
view.imageView(100)?image = nil

弹出窗口(popup)

if needUpdate {
    popupOkCancel(on: self, title: "업데이트", message: "업데이트가 필요합니다.", onOk: {
        let urlString = "itms-apps://itunes.apple.com/app/id0000000000"
        UIApplication.shared.openURL(URL(string: urlString)!)
    })
} else {
    popupOK(on: self, title: "업데이트", message: "최신버전입니다.")
}

图片缩放(Image Resize)

let original = UIImage(named: "big_image")
let resized = original?.resized(maxSize: CGSize(width: 100, height: 100))
let resizedForScreen = original?.resizedToScreen()

视图创建器(含SnapKit)

let rect = createView(UIView(), parent: self.view, setting: { v in
            v.backgroundColor = .red
        }, constraint: { m in
            m.top.left.equalToSuperview()
            m.width.height.equalTo(100)
        })

RxSwift

手势识别器

view.addTapGestureRecognizer()
    .subscribe(onNext: { (tapGusture : UITapGestureRecognizer) in
    })

view.addSwipeGestureRecognizer()
    .subscribe(onNext: { (tapGusture : UISwipeGestureRecognizer) in
    })

按钮

//prevent double touched
view.button(100)?.whenTouchUpInside(thro)
    .subscribe(onNext: { button in
    })

键盘通知

whenKeyboardShowNotification()
    .subscribe(onNext: { (keyboardHeight: CGFloat, duration: TimeInterval) in
    })
    .disposed(by: disposeBag)

whenKeyboardHideNotification()
    .subscribe(onNext: { (duration: TimeInterval) in
    })
    .disposed(by: disposeBag)

滚动视图

collectionView.needsMore()
	.filter { !self.isLoading }
	.subscribe(onNext: { self.loadMore() })
	.disposed(by: disposeBag)

图片选择器

pickImageFromAlbum(on: self, withEdit: true)
    .subscribe(onNext: {  self.imageView.image = $0 })

作者

iamchiwon, [email protected]

许可证

CWUtils在MIT许可证下提供。请参阅LICENSE文件以获取更多信息。