compatible with Swift 4

This commit is contained in:
caiyuanpeng 2017-10-14 21:14:25 +08:00
parent 7ffa1ca045
commit 2fd2659bc8
11 changed files with 186 additions and 120 deletions

View File

@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "Cupcake"
s.version = "1.1.1"
s.version = "1.2.0"
s.summary = "An easy way to create and layout UI components for iOS."
s.description = <<-DESC

View File

@ -56,7 +56,7 @@ public extension NSMutableAttributedString {
...
*/
@discardableResult public func font(_ style: Any) -> Self {
cpk_addAttribute(name: NSFontAttributeName, value: Font(style))
cpk_addAttribute(name: "NSFont", value: Font(style))
return self
}
@ -72,7 +72,7 @@ public extension NSMutableAttributedString {
...
*/
@discardableResult public func color(_ any: Any) -> Self {
cpk_addAttribute(name: NSForegroundColorAttributeName, value: Color(any)!)
cpk_addAttribute(name: "NSColor", value: Color(any)!)
return self
}
@ -89,7 +89,7 @@ public extension NSMutableAttributedString {
...
*/
@discardableResult public func bg(_ any: Any) -> Self {
cpk_addAttribute(name: NSBackgroundColorAttributeName, value: Color(any) ?? Color(Img(any))!)
cpk_addAttribute(name: "NSBackgroundColor", value: Color(any) ?? Color(Img(any))!)
return self
}
@ -110,9 +110,9 @@ public extension NSMutableAttributedString {
styles = NSNumber(value: style.rawValue | NSUnderlineStyle.styleSingle.rawValue)
}
cpk_addAttribute(name: NSUnderlineStyleAttributeName, value: styles)
cpk_addAttribute(name: "NSUnderline", value: styles)
if let underlineColor = Color(color) {
cpk_addAttribute(name: NSUnderlineColorAttributeName, value: underlineColor)
cpk_addAttribute(name: "NSUnderlineColor", value: underlineColor)
}
return self
}
@ -138,9 +138,9 @@ public extension NSMutableAttributedString {
styles = NSNumber(value: style.rawValue | NSUnderlineStyle.styleSingle.rawValue)
}
cpk_addAttribute(name: NSStrikethroughStyleAttributeName, value: styles)
cpk_addAttribute(name: "NSStrikethrough", value: styles)
if let strikethroughColor = Color(color) {
cpk_addAttribute(name: NSStrikethroughColorAttributeName, value: strikethroughColor)
cpk_addAttribute(name: "NSStrikethroughColor", value: strikethroughColor)
}
return self
}
@ -158,9 +158,9 @@ public extension NSMutableAttributedString {
.stroke(-4, "red")
*/
@discardableResult public func stroke(_ width: CGFloat, _ color: Any? = nil) -> Self {
cpk_addAttribute(name: NSStrokeWidthAttributeName, value: width)
cpk_addAttribute(name: "NSStrokeWidth", value: width)
if let strokeColor = Color(color) {
cpk_addAttribute(name: NSStrokeColorAttributeName, value: strokeColor)
cpk_addAttribute(name: "NSStrokeColor", value: strokeColor)
}
return self
}
@ -172,7 +172,7 @@ public extension NSMutableAttributedString {
.oblique(-0.3)
*/
@discardableResult public func oblique(_ value: CGFloat) -> Self {
cpk_addAttribute(name: NSObliquenessAttributeName, value: value)
cpk_addAttribute(name: "NSObliqueness", value: value)
return self
}
@ -183,7 +183,7 @@ public extension NSMutableAttributedString {
.offset(-20)
*/
@discardableResult public func offset(_ offset: CGFloat) -> Self {
cpk_addAttribute(name: NSBaselineOffsetAttributeName, value: offset)
cpk_addAttribute(name: "NSBaselineOffset", value: offset)
return self
}
@ -196,7 +196,7 @@ public extension NSMutableAttributedString {
*/
@discardableResult public func link(_ url: String? = nil) -> Self {
if let urlString = url {
cpk_addAttribute(name: NSLinkAttributeName, value: urlString)
cpk_addAttribute(name: "NSLink", value: urlString)
} else {
cpk_addAttribute(name: CPKLabelLinkAttributeName, value: CPKLabelLinkAttributeValue)
}

View File

@ -26,7 +26,7 @@ public extension UIButton {
.str( AttStr("hello world").strikethrough() )
...
*/
@discardableResult public func str(_ any: Any) -> Self {
@objc @discardableResult public func str(_ any: Any) -> Self {
if let attStr = any as? NSAttributedString {
setAttributedTitle(attStr, for: .normal)
} else {
@ -47,7 +47,7 @@ public extension UIButton {
.font(someLabel.font)
...
**/
@discardableResult public func font(_ any: Any) -> Self {
@objc @discardableResult public func font(_ any: Any) -> Self {
self.titleLabel?.font = Font(any)
return self
}
@ -63,7 +63,7 @@ public extension UIButton {
.color(someLabel.textColor)
...
*/
@discardableResult public func color(_ any: Any) -> Self {
@objc @discardableResult public func color(_ any: Any) -> Self {
setTitleColor(Color(any), for: .normal)
return self
}
@ -79,7 +79,7 @@ public extension UIButton {
.highColor(someLabel.textColor)
...
*/
@discardableResult public func highColor(_ any: Any) -> Self {
@objc @discardableResult public func highColor(_ any: Any) -> Self {
setTitleColor(Color(any), for: .highlighted)
return self
}
@ -95,7 +95,7 @@ public extension UIButton {
.img(someImage)
...
*/
@discardableResult public func img(_ any: Any) -> Self {
@objc @discardableResult public func img(_ any: Any) -> Self {
let image = Img(any)
setImage(image, for: .normal)
if self.frame.isEmpty {
@ -115,7 +115,7 @@ public extension UIButton {
.highImg(someImage)
...
*/
@discardableResult public func highImg(_ any: Any) -> Self {
@objc @discardableResult public func highImg(_ any: Any) -> Self {
setImage(Img(any), for: .highlighted)
return self
}
@ -133,7 +133,7 @@ public extension UIButton {
.bg(someImage) //using image
...
*/
@discardableResult override public func bg(_ any: Any) -> Self {
@objc @discardableResult override public func bg(_ any: Any) -> Self {
let image = Img(any)
setBackgroundImage(image, for: .normal)
cpk_masksToBoundsIfNeed()
@ -157,7 +157,7 @@ public extension UIButton {
.highBg(someImage) //using image
...
*/
@discardableResult public func highBg(_ any: Any) -> Self {
@objc @discardableResult public func highBg(_ any: Any) -> Self {
setBackgroundImage(Img(any), for: .highlighted)
cpk_masksToBoundsIfNeed()
return self
@ -181,7 +181,7 @@ public extension UIButton {
* Usages:
.gap(10)
*/
@discardableResult public func gap(_ spacing: CGFloat) -> Self {
@objc @discardableResult public func gap(_ spacing: CGFloat) -> Self {
self.cpkGap = spacing
let halfGap = spacing / 2
@ -202,7 +202,7 @@ public extension UIButton {
.reversed()
.reversed(false)
*/
@discardableResult public func reversed(_ reversed: Bool = true) -> Self {
@objc @discardableResult public func reversed(_ reversed: Bool = true) -> Self {
let t = reversed ? CATransform3DMakeScale(-1, 1, 1) : CATransform3DIdentity
self.layer.sublayerTransform = t
self.imageView?.layer.transform = t
@ -217,7 +217,7 @@ public extension UIButton {
.lines(0) //multilines
.lines() //same as .lines(0)
*/
@discardableResult public func lines(_ numberOfLines: CGFloat = 0) -> Self {
@objc @discardableResult public func lines(_ numberOfLines: CGFloat = 0) -> Self {
self.titleLabel?.numberOfLines = Int(numberOfLines)
self.titleLabel?.lineBreakMode = .byWordWrapping
self.titleLabel?.textAlignment = .center

View File

@ -28,7 +28,7 @@ public extension UIImageView {
.img(someImage)
...
*/
@discardableResult public func img(_ any: Any) -> Self {
@objc @discardableResult public func img(_ any: Any) -> Self {
self.image = Img(any)
if self.image != nil {
@ -48,7 +48,7 @@ public extension UIImageView {
.mode(.center)
...
*/
@discardableResult public func mode(_ contentMode: UIViewContentMode) -> Self {
@objc @discardableResult public func mode(_ contentMode: UIViewContentMode) -> Self {
self.contentMode = contentMode
return self
}

View File

@ -25,7 +25,7 @@ public extension UILabel {
.str( AttStr("hello world").strikethrough() )
...
*/
@discardableResult public func str(_ any: Any) -> Self {
@objc @discardableResult public func str(_ any: Any) -> Self {
if let attStr = any as? NSAttributedString {
self.attributedText = attStr
} else {
@ -46,7 +46,7 @@ public extension UILabel {
.font(someLabel.font)
...
**/
@discardableResult public func font(_ any: Any) -> Self {
@objc @discardableResult public func font(_ any: Any) -> Self {
self.font = Font(any)
return self
}
@ -62,7 +62,7 @@ public extension UILabel {
.color(someLabel.textColor)
...
*/
@discardableResult public func color(_ any: Any) -> Self {
@objc @discardableResult public func color(_ any: Any) -> Self {
self.textColor = Color(any)
return self
}
@ -74,7 +74,7 @@ public extension UILabel {
.lines(0) //multilines
.lines() //same as .lines(0)
*/
@discardableResult public func lines(_ numberOfLines: CGFloat = 0) -> Self {
@objc @discardableResult public func lines(_ numberOfLines: CGFloat = 0) -> Self {
self.numberOfLines = Int(numberOfLines)
return self
}
@ -84,7 +84,7 @@ public extension UILabel {
* Usages:
.lineGap(8)
*/
@discardableResult public func lineGap(_ lineSpacing: CGFloat) -> Self {
@objc @discardableResult public func lineGap(_ lineSpacing: CGFloat) -> Self {
self.cpkLineGap = lineSpacing
return self
}
@ -96,7 +96,7 @@ public extension UILabel {
.align(.justified)
...
*/
@discardableResult public func align(_ textAlignment: NSTextAlignment) -> Self {
@objc @discardableResult public func align(_ textAlignment: NSTextAlignment) -> Self {
self.textAlignment = textAlignment
return self
}

View File

@ -35,11 +35,12 @@ public extension String {
@discardableResult public func subFrom(_ indexOrSubstring: Any) -> String {
if var index = indexOrSubstring as? Int {
if index < 0 { index += self.characters.count }
return self.substring(from: self.index(self.startIndex, offsetBy: index))
let from = self.index(self.startIndex, offsetBy: index)
return self.cpk_substring(from: from)
} else if let substr = indexOrSubstring as? String {
if let range = self.range(of: substr) {
return self.substring(from: range.lowerBound)
return self.cpk_substring(from: range.lowerBound)
}
}
@ -57,11 +58,12 @@ public extension String {
@discardableResult public func subTo(_ indexOrSubstring: Any) -> String {
if var index = indexOrSubstring as? Int {
if index < 0 { index += self.characters.count }
return self.substring(to: self.index(self.startIndex, offsetBy: index))
let to = self.index(self.startIndex, offsetBy: index)
return self.cpk_substring(to: to)
} else if let substr = indexOrSubstring as? String {
if let range = self.range(of: substr) {
return self.substring(to: range.lowerBound)
return self.cpk_substring(to: range.lowerBound)
}
}
@ -80,32 +82,32 @@ public extension String {
return String(self[self.index(self.startIndex, offsetBy: index)])
} else if let range = indexOrRange as? Range<String.Index> {
return self.substring(with: range)
return self.cpk_substring(with: range)
} else if let range = indexOrRange as? Range<Int> {
let lower = self.index(self.startIndex, offsetBy: range.lowerBound)
let upper = self.index(self.startIndex, offsetBy: range.upperBound)
return self.substring(with: lower..<upper)
return self.cpk_substring(with: lower..<upper)
} else if let range = indexOrRange as? CountableRange<Int> {
let lower = self.index(self.startIndex, offsetBy: range.lowerBound)
let upper = self.index(self.startIndex, offsetBy: range.upperBound)
return self.substring(with: lower..<upper)
return self.cpk_substring(with: lower..<upper)
} else if let range = indexOrRange as? ClosedRange<Int> {
let lower = self.index(self.startIndex, offsetBy: range.lowerBound)
let upper = self.index(self.startIndex, offsetBy: range.upperBound + 1)
return self.substring(with: lower..<upper)
return self.cpk_substring(with: lower..<upper)
} else if let range = indexOrRange as? CountableClosedRange<Int> {
let lower = self.index(self.startIndex, offsetBy: range.lowerBound)
let upper = self.index(self.startIndex, offsetBy: range.upperBound + 1)
return self.substring(with: lower..<upper)
return self.cpk_substring(with: lower..<upper)
} else if let range = indexOrRange as? NSRange {
let lower = self.index(self.startIndex, offsetBy: range.location)
let upper = self.index(self.startIndex, offsetBy: range.location + range.length)
return self.substring(with: lower..<upper)
return self.cpk_substring(with: lower..<upper)
}
return ""

View File

@ -27,7 +27,7 @@ public extension UITextField {
.str( AttStr("hello world").strikethrough() )
...
*/
@discardableResult public func str(_ any: Any) -> Self {
@objc @discardableResult public func str(_ any: Any) -> Self {
if let attStr = any as? NSAttributedString {
self.attributedText = attStr
} else {
@ -44,7 +44,7 @@ public extension UITextField {
.hint("Enter your name")
.hint( AttStr("Enter your name").font(13) )
*/
@discardableResult public func hint(_ any: Any) -> Self {
@objc @discardableResult public func hint(_ any: Any) -> Self {
if let attStr = any as? NSAttributedString {
self.attributedPlaceholder = attStr
} else {
@ -64,7 +64,7 @@ public extension UITextField {
.font(someLabel.font)
...
**/
@discardableResult public func font(_ any: Any) -> Self {
@objc @discardableResult public func font(_ any: Any) -> Self {
self.font = Font(any)
return self
}
@ -80,7 +80,7 @@ public extension UITextField {
.color(someLabel.textColor)
...
*/
@discardableResult public func color(_ any: Any) -> Self {
@objc @discardableResult public func color(_ any: Any) -> Self {
self.textColor = Color(any)
return self
}
@ -90,7 +90,7 @@ public extension UITextField {
* Usages:
.maxLength(10)
*/
@discardableResult public func maxLength(_ length: CGFloat) -> Self {
@objc @discardableResult public func maxLength(_ length: CGFloat) -> Self {
self.cpkMaxLength = Int(length)
return self
}
@ -114,7 +114,7 @@ public extension UITextField {
.secure() //secureTextEntry = true
.secure(false) //secureTextEntry = false
*/
@discardableResult public func secure(_ secureTextEntry: Bool = true) -> Self {
@objc @discardableResult public func secure(_ secureTextEntry: Bool = true) -> Self {
self.isSecureTextEntry = secureTextEntry
return self
}
@ -126,7 +126,7 @@ public extension UITextField {
.align(.justified)
...
*/
@discardableResult public func align(_ textAlignment: NSTextAlignment) -> Self {
@objc @discardableResult public func align(_ textAlignment: NSTextAlignment) -> Self {
self.textAlignment = textAlignment
return self
}
@ -138,7 +138,7 @@ public extension UITextField {
.keyboard(.emailAddress)
...
*/
@discardableResult public func keyboard(_ keyboardType: UIKeyboardType) -> Self {
@objc @discardableResult public func keyboard(_ keyboardType: UIKeyboardType) -> Self {
self.keyboardType = keyboardType
return self
}
@ -150,7 +150,7 @@ public extension UITextField {
.returnKey(.google)
...
*/
@discardableResult public func returnKey(_ returnKeyType: UIReturnKeyType) -> Self {
@objc @discardableResult public func returnKey(_ returnKeyType: UIReturnKeyType) -> Self {
self.returnKeyType = returnKeyType
return self
}
@ -162,7 +162,7 @@ public extension UITextField {
.clearMode(.always)
...
*/
@discardableResult public func clearMode(_ clearButtonMode: UITextFieldViewMode) -> Self {
@objc @discardableResult public func clearMode(_ clearButtonMode: UITextFieldViewMode) -> Self {
self.clearButtonMode = clearButtonMode
return self
}

View File

@ -24,7 +24,7 @@ public extension UITextView {
.str( AttStr("hello world").strikethrough() )
...
*/
@discardableResult public func str(_ any: Any) -> Self {
@objc @discardableResult public func str(_ any: Any) -> Self {
if let attStr = any as? NSAttributedString {
self.attributedText = attStr
} else {
@ -41,7 +41,7 @@ public extension UITextView {
.hint("Enter here")
.hint( AttStr("Enter here").font(13) )
*/
@discardableResult public func hint(_ any: Any) -> Self {
@objc @discardableResult public func hint(_ any: Any) -> Self {
cpk_setPlaceholder(any)
return self
}
@ -57,7 +57,7 @@ public extension UITextView {
.font(someLabel.font)
...
**/
@discardableResult public func font(_ any: Any) -> Self {
@objc @discardableResult public func font(_ any: Any) -> Self {
self.font = Font(any)
return self
}
@ -73,7 +73,7 @@ public extension UITextView {
.color(someLabel.textColor)
...
*/
@discardableResult public func color(_ any: Any) -> Self {
@objc @discardableResult public func color(_ any: Any) -> Self {
self.textColor = Color(any)
return self
}
@ -83,7 +83,7 @@ public extension UITextView {
* Usages:
.maxLength(10)
*/
@discardableResult public func maxLength(_ length: CGFloat) -> Self {
@objc @discardableResult public func maxLength(_ length: CGFloat) -> Self {
self.cpkMaxLength = Int(length)
return self
}
@ -108,7 +108,7 @@ public extension UITextView {
.align(.justified)
...
*/
@discardableResult public func align(_ textAlignment: NSTextAlignment) -> Self {
@objc @discardableResult public func align(_ textAlignment: NSTextAlignment) -> Self {
self.textAlignment = textAlignment
return self
}

View File

@ -28,7 +28,7 @@ public extension UIView {
.bg("cat") //using image
...
*/
@discardableResult public func bg(_ any: Any) -> Self {
@objc @discardableResult public func bg(_ any: Any) -> Self {
self.backgroundColor = Color(any) ?? Color(Img(any))
return self
}
@ -41,7 +41,7 @@ public extension UIView {
.tint("red")
.tint("#00F")
*/
@discardableResult public func tint(_ any: Any) -> Self {
@objc @discardableResult public func tint(_ any: Any) -> Self {
self.tintColor = Color(any)
return self
}
@ -54,7 +54,7 @@ public extension UIView {
.radius(10)
.radius(-1) //passing negative number means using auto rounding
*/
@discardableResult public func radius(_ cornerRadius: CGFloat) -> Self {
@objc @discardableResult public func radius(_ cornerRadius: CGFloat) -> Self {
if cornerRadius < 0 {
self.layer.cornerRadius = self.bounds.height / 2
self.cpkAutoRoundingRadius = true
@ -75,7 +75,7 @@ public extension UIView {
.border(1)
.border(1, "red")
*/
@discardableResult public func border(_ borderWidth: CGFloat, _ borderColor: Any? = nil) -> Self {
@objc @discardableResult public func border(_ borderWidth: CGFloat, _ borderColor: Any? = nil) -> Self {
self.layer.borderWidth = borderWidth
self.layer.borderColor = Color(borderColor)?.cgColor
return self
@ -88,10 +88,10 @@ public extension UIView {
.shadow(0.7, 2)
.shadow(0.7, 3, 0, 0)
*/
@discardableResult public func shadow(_ shadowOpacity: CGFloat,
_ shadowRadius: CGFloat = 3,
_ shadowOffsetX: CGFloat = 0,
_ shadowOffsetY: CGFloat = 3) -> Self {
@objc @discardableResult public func shadow(_ shadowOpacity: CGFloat,
_ shadowRadius: CGFloat = 3,
_ shadowOffsetX: CGFloat = 0,
_ shadowOffsetY: CGFloat = 3) -> Self {
self.layer.shadowOpacity = Float(shadowOpacity)
self.layer.shadowRadius = shadowRadius
@ -108,7 +108,7 @@ public extension UIView {
.styles(myStyle1, myStyle2, "globalStyle1")
.styles(someView) //retrieve styles direct from view
*/
@discardableResult public func styles(_ s1: Any, _ s2: Any? = nil, _ s3: Any? = nil, _ s4: Any? = nil) -> Self {
@objc @discardableResult public func styles(_ s1: Any, _ s2: Any? = nil, _ s3: Any? = nil, _ s4: Any? = nil) -> Self {
var array = Array<Any>()
array.append(s1)
@ -142,7 +142,7 @@ public extension UIView {
.touchInsets(10, 20, 30) //top: 10, left: 20, bottom: 0 , right: 30
.touchInsets(10, 20, 30, 40) //top: 10, left: 20, bottom: 30, right: 40
*/
@discardableResult public func touchInsets(_ p1: Any, _ p2: Any? = nil, _ p3: Any? = nil, _ p4: Any? = nil) -> Self {
@objc @discardableResult public func touchInsets(_ p1: Any, _ p2: Any? = nil, _ p3: Any? = nil, _ p4: Any? = nil) -> Self {
self.cpkTouchInsets = cpk_edgeInsetsFromParameters(p1, p2, p3, p4)
return self
}
@ -156,7 +156,7 @@ public extension UIView {
.onClick({ _ in /* ... */ }) //if you don't care at all
.onClick({ [weak self] _ in /* ... */ }) //capture self as weak reference when needed
*/
@discardableResult public func onClick(_ closure: @escaping (UIView)->()) -> Self {
@objc @discardableResult public func onClick(_ closure: @escaping (UIView)->()) -> Self {
cpk_onClick(closure, nil)
return self
}
@ -166,7 +166,7 @@ public extension UIView {
* Usages:
.addTo(superView)
*/
@discardableResult public func addTo(_ superview: UIView) -> Self {
@objc @discardableResult public func addTo(_ superview: UIView) -> Self {
superview.addSubview(self)
return self
}
@ -206,7 +206,7 @@ public extension UIView {
.margin(10, 20, 30) //top: 10, left: 20, bottom: 0 , right: 30
.margin(10, 20, 30, 40) //top: 10, left: 20, bottom: 30, right: 40
*/
@discardableResult public func margin(_ p1: Any, _ p2: Any? = nil, _ p3: Any? = nil, _ p4: Any? = nil) -> Self {
@objc @discardableResult public func margin(_ p1: Any, _ p2: Any? = nil, _ p3: Any? = nil, _ p4: Any? = nil) -> Self {
self.layoutMargins = cpk_edgeInsetsFromParameters(p1, p2, p3, p4)
return self
}
@ -271,7 +271,7 @@ public extension UIView {
_.size.equal(100, 100)
})
*/
@discardableResult public func makeCons(_ closure: (ConsMaker)->()) -> Self {
@objc @discardableResult public func makeCons(_ closure: (ConsMaker)->()) -> Self {
let maker = ConsMaker(firstItem: self)
closure(maker)
maker.updateConstraints()
@ -287,7 +287,7 @@ public extension UIView {
_.size.equal(anotherView)
})
*/
@discardableResult public func remakeCons(_ closure: (ConsMaker)->()) -> Self {
@objc @discardableResult public func remakeCons(_ closure: (ConsMaker)->()) -> Self {
let maker = ConsMaker(firstItem: self)
closure(maker)
maker.remakeConstraints()
@ -310,9 +310,9 @@ public extension UIView {
* Usages:
.embedIn(superview, "10", 20, "30", 40) //topMargin: 10, left: 20, bottomMargin: 30, right: 40
*/
@discardableResult public func embedIn(_ superview: UIView,
_ p1: Any? = "", _ p2: Any? = "",
_ p3: Any? = "", _ p4: Any? = "") -> Self {
@objc @discardableResult public func embedIn(_ superview: UIView,
_ p1: Any? = "", _ p2: Any? = "",
_ p3: Any? = "", _ p4: Any? = "") -> Self {
superview.addSubview(self)
let edge = cpk_edgeInsetsTupleFromParameters(p1, p2, p3, p4)

View File

@ -502,9 +502,6 @@ let CPKLabelLinkAttributeValue = "CPKLabelLinkValue"
fileprivate let CPKLinkColor = UIColor(red: 0, green: 0.478431, blue: 1, alpha: 1)
fileprivate let CPKFixLineSpacingIssueAttributeName = "CPKFixLineSpacingIssue"
fileprivate let CPKFixLineSpacingIssueAttributeValue = "Fix single line Label with lineSpacing issue"
public extension NSObject {
@ -525,27 +522,27 @@ public extension NSObject {
return false
}
var m1 = class_getInstanceMethod(self, s1)
var m2 = class_getInstanceMethod(self, s2)
var m1 = class_getInstanceMethod(self, s1!)
var m2 = class_getInstanceMethod(self, s2!)
if m1 == nil || m2 == nil {
return false
}
class_addMethod(self, s1, method_getImplementation(m1), method_getTypeEncoding(m1))
class_addMethod(self, s2, method_getImplementation(m2), method_getTypeEncoding(m2))
class_addMethod(self, s1!, method_getImplementation(m1!), method_getTypeEncoding(m1!))
class_addMethod(self, s2!, method_getImplementation(m2!), method_getTypeEncoding(m2!))
m1 = class_getInstanceMethod(self, s1)
m2 = class_getInstanceMethod(self, s2)
method_exchangeImplementations(m1, m2)
m1 = class_getInstanceMethod(self, s1!)
m2 = class_getInstanceMethod(self, s2!)
method_exchangeImplementations(m1!, m2!)
return true
}
@discardableResult
public static func cpk_swizzleClass(method1: Any, method2: Any) -> Bool {
return object_getClass(self).cpk_swizzle(method1: method1, method2: method2)
}
// @discardableResult
// public static func cpk_swizzleClass(method1: Any, method2: Any) -> Bool {
// return object_getClass(self).cpk_swizzle(method1: method1, method2: method2)
// }
@discardableResult
public func cpk_safePerform(selector: Selector) -> Any? {
@ -586,6 +583,34 @@ public extension NSObject {
}
extension String {
func cpk_substring(with range: Range<String.Index>) -> String {
#if swift(>=4.0)
return String(self[range])
#else
return self.substring(with: range)
#endif
}
func cpk_substring(to index: String.Index) -> String {
#if swift(>=4.0)
return String(self[..<index])
#else
return self.substring(to: index)
#endif
}
func cpk_substring(from index: String.Index) -> String {
#if swift(>=4.0)
return String(self[index...])
#else
return self.substring(from: index)
#endif
}
}
extension NSMutableAttributedString {
private var cpkSelectedRanges: NSMutableIndexSet {
@ -624,20 +649,28 @@ extension NSMutableAttributedString {
}
}
func cpk_addAttribute(_ name: String, value: Any, range: NSRange) {
#if swift(>=4.0)
addAttribute(NSAttributedStringKey(rawValue: name), value: value, range: range)
#else
addAttribute(name, value: value, range: range)
#endif
}
func cpk_addAttribute(name: String, value: Any) {
self.cpkIsJustSelectingRange = false
self.cpkSelectedRanges.enumerateRanges({ (range, stop) in
if name == CPKLabelLinkAttributeName {
addAttribute(name, value: value, range: range)
addAttribute(NSForegroundColorAttributeName, value: CPKLinkColor, range: range)
cpk_addAttribute(name, value: value, range: range)
cpk_addAttribute("NSColor", value: CPKLinkColor, range: range)
} else {
if self.cpkPreventOverrideAttribute {
cpk_addAttributeIfNotExisted(name: name, value: value, range: range)
} else {
addAttribute(name, value: value, range: range)
cpk_addAttribute(name, value: value, range: range)
}
}
});
@ -645,14 +678,29 @@ extension NSMutableAttributedString {
func cpk_addAttributeIfNotExisted(name: String, value: Any, range: NSRange) {
let indexSets = NSMutableIndexSet(indexesIn: range)
enumerateAttribute(name, in: range, options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (value, range, stop) in
#if swift(>=4.0)
enumerateAttribute(NSAttributedStringKey(rawValue: name),
in: range,
options: NSAttributedString.EnumerationOptions(rawValue: 0))
{ (value, range, stop) in
if value != nil {
indexSets.remove(in: range)
}
}
#else
enumerateAttribute(name,
in: range,
options: NSAttributedString.EnumerationOptions(rawValue: 0))
{ (value, range, stop) in
if value != nil {
indexSets.remove(in: range)
}
}
#endif
indexSets.enumerateRanges({ (range, stop) in
addAttribute(name, value: value, range: range)
cpk_addAttribute(name, value: value, range: range)
})
}
@ -660,7 +708,7 @@ extension NSMutableAttributedString {
let targetRange = range ?? NSMakeRange(0, self.length)
var mutableStyle: NSMutableParagraphStyle
if let paraStyle = attribute(NSParagraphStyleAttributeName,
if let paraStyle = attribute("NSParagraphStyle",
at: targetRange.location,
longestEffectiveRange: nil,
in: targetRange) as? NSParagraphStyle {
@ -673,11 +721,7 @@ extension NSMutableAttributedString {
}
mutableStyle.setValue(value, forKey: key)
addAttribute(NSParagraphStyleAttributeName, value: mutableStyle, range: targetRange)
if key == "lineSpacing" {
addAttribute(CPKFixLineSpacingIssueAttributeName, value: CPKFixLineSpacingIssueAttributeValue, range: targetRange)
}
cpk_addAttribute("NSParagraphStyle", value: mutableStyle, range: targetRange)
}
}
@ -726,12 +770,12 @@ extension UIView {
set { cpk_setAssociated(object: newValue, forKey: #function) }
}
func cpk_setBounds(_ frame: CGRect) {
@objc func cpk_setBounds(_ frame: CGRect) {
cpk_setBounds(frame)
cpk_updateCornerRadiusIfNeed()
}
func cpk_point(inside point: CGPoint, with event: UIEvent?) -> Bool {
@objc func cpk_point(inside point: CGPoint, with event: UIEvent?) -> Bool {
if let insets = self.cpkTouchInsets {
let rect = UIEdgeInsetsInsetRect(self.bounds, insets)
return rect.contains(point)
@ -740,7 +784,7 @@ extension UIView {
}
}
func cpk_masksToBoundsIfNeed() {
@objc func cpk_masksToBoundsIfNeed() {
self.layer.masksToBounds = false
}
@ -776,7 +820,7 @@ extension UIView {
}
}
func cpk_onClickHandler() {
@objc func cpk_onClickHandler() {
let callback = cpk_associatedObjectFor(key: "cpk_OnClick")
if let closure = callback as? ()->() {
@ -883,12 +927,12 @@ extension UITextField {
set { cpk_setAssociated(object: newValue, forKey: #function); cpk_watchOnEndEvent() }
}
public func cpk_textRect(forBounds bounds: CGRect) -> CGRect {
@objc public func cpk_textRect(forBounds bounds: CGRect) -> CGRect {
let rect = self.cpk_textRect(forBounds: bounds)
return UIEdgeInsetsInsetRect(rect, self.cpkPadding)
}
public func cpk_editingRect(forBounds bounds: CGRect) -> CGRect {
@objc public func cpk_editingRect(forBounds bounds: CGRect) -> CGRect {
let rect = cpk_editingRect(forBounds: bounds)
return UIEdgeInsetsInsetRect(rect, self.cpkPadding)
}
@ -905,7 +949,7 @@ extension UITextField {
addTarget(self, action: sel, for: .editingDidEndOnExit)
}
func cpk_textDidChange() {
@objc func cpk_textDidChange() {
let hasMarked = cpk_limitTextInput(self, maxLength: self.cpkMaxLength)
if !hasMarked {
if let closure = self.cpkTextChangedClosure as? (UITextField)->() {
@ -914,7 +958,7 @@ extension UITextField {
}
}
func cpk_didEndOnExit() {
@objc func cpk_didEndOnExit() {
if let closure = self.cpkDidEndOnExistClosure as? (UITextField)->() {
closure(self)
}
@ -948,6 +992,21 @@ extension CPKViewPinOptions : ExpressibleByIntegerLiteral, ExpressibleByFloatLit
}
#if swift(>=4.0)
extension UILayoutPriority : ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral {
public init(integerLiteral value: Int) {
self = UILayoutPriority(rawValue: Float(value))
}
public init(floatLiteral value: Float) {
self = UILayoutPriority(rawValue: value)
}
}
extension NSAttributedStringKey : ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self = NSAttributedStringKey(rawValue: value)
}
}
#endif
@ -1012,7 +1071,7 @@ public class AlertMaker {
if let attTitle = title as? NSAttributedString {
titleString = attTitle.string
titleColor = attTitle.attribute(NSForegroundColorAttributeName, at: 0, effectiveRange: nil) as? UIColor
titleColor = attTitle.attribute("NSColor", at: 0, effectiveRange: nil) as? UIColor
} else {
titleString = String(describing: title)
}
@ -1216,7 +1275,7 @@ public class Cons: ConsAtts {
}
private func priorityValue(atIndex index: Int) -> UILayoutPriority {
var priority: UILayoutPriority = UILayoutPriorityRequired
var priority: UILayoutPriority = 1000
if index < priorityValues.count {
priority = priorityValues[index]
@ -1256,7 +1315,11 @@ public class Cons: ConsAtts {
multiplier: multiplier,
constant: constant)
#if swift(>=4.0)
c.priority = priority
#else
c.priority = Float(priority)
#endif
layoutConstraints.append(c)
}
@ -2212,7 +2275,7 @@ public class StaticRow: UIView {
return CPKTransformLayer.self
}
func switchDidChange() {
@objc func switchDidChange() {
if let callback = onChangeHandler {
callback(self)
}
@ -2511,7 +2574,7 @@ extension UITextView {
return self.viewWithTag(31415) as? UITextViewPlaceholder
}
public func cpk_deinit() {
@objc public func cpk_deinit() {
if let label = self.cpkPlaceholderLabel {
for keyPath in cpkTextViewObservingKeys {
self.removeObserver(label, forKeyPath: keyPath)
@ -2533,7 +2596,7 @@ extension UITextView {
object: self)
}
func cpk_textDidChange() {
@objc func cpk_textDidChange() {
let hasMarked = cpk_limitTextInput(self, maxLength: self.cpkMaxLength)
if !hasMarked {
if let closure = self.cpkTextChangedClosure as? (UITextView)->() {
@ -2774,7 +2837,7 @@ extension UILabel {
set { cpk_setAssociated(object: newValue, forKey: #function) }
}
public func ner_setText(_ text: String) {
@objc public func ner_setText(_ text: String) {
self.ner_setText(text)
cpk_updateAttributedString()
}
@ -2791,7 +2854,7 @@ extension UILabel {
extension UILabel {
func cpk_handleLinkGesture(_ gesture: LinkGestureRecognizer) {
@objc func cpk_handleLinkGesture(_ gesture: LinkGestureRecognizer) {
func cpk_calculateTextYOffset() -> CGFloat {
let layoutManager = self.cpkLayoutManager
@ -2832,7 +2895,7 @@ extension UILabel {
}
}
if let ps = layoutManager.textStorage?.attribute(NSParagraphStyleAttributeName,
if let ps = layoutManager.textStorage?.attribute("NSParagraphStyle",
at: range.location,
longestEffectiveRange: nil,
in: range) as? NSParagraphStyle {
@ -2877,7 +2940,7 @@ extension UILabel {
}
}
attStr.enumerateAttribute(CPKLabelLinkAttributeName,
attStr.enumerateAttribute("CPKLabelLink",
in: fullRange,
options: NSAttributedString.EnumerationOptions.init(rawValue: 0),
using: handler)
@ -2919,7 +2982,7 @@ extension UILabel {
}
}
func cpk_addHighlightedLayers(for linkInfo: LinkInfo) {
@objc func cpk_addHighlightedLayers(for linkInfo: LinkInfo) {
for rect in linkInfo.boundingRects {
if rect.size.width > 0 && rect.size.height > 0 {
var color = self.cpkLinkSelectionColor ?? UIColor.darkGray

View File

@ -3,7 +3,8 @@
</p>
<p align="center">
<img src="https://camo.githubusercontent.com/085452dbb9eb9e76211db29ac23078836560bbae/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c616e67756167652d7377696674253230332d3442433531442e7376673f7374796c653d666c6174" alt="Language" />
<img src="https://camo.githubusercontent.com/11874553c483b3630dff4ac1e54f1d0fb030bb86/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53776966742d332e302532422d6f72616e67652e737667" alt="Language" />
<img src="https://camo.githubusercontent.com/987c5cc551bc4988195dffc73b6822e11eb451cc/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7377696674342d636f6d70617469626c652d3442433531442e7376673f7374796c653d666c6174" alt="Swift4" />
<img src="https://camo.githubusercontent.com/1d276aa371242346c77c3a5b8db34beaa2014722/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f694f532d382e302532422d626c75652e737667" alt="Platform" />
<img src="http://cocoapod-badges.herokuapp.com/v/Cupcake/badge.png" alt="Version" />
<img src="http://cocoapod-badges.herokuapp.com/l/Cupcake/badge.png" alt="License" />