Skip to content

Commit

Permalink
Nested block function for NKButtonStack for cleaner code
Browse files Browse the repository at this point in the history
  • Loading branch information
kennic committed Feb 27, 2023
1 parent 1c09e11 commit 0045bc3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 102 deletions.
7 changes: 3 additions & 4 deletions Example/NKButton/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class ViewController: UIViewController {
buttonStack.shadowRadius = 4
buttonStack.shadowOpacity = 1.0

buttonStack.configurationBlock = { (button, item, index) in
buttonStack.configuration { button, item, index in
button.backgroundColors[.normal] = .lightGray
button.backgroundColors[.highlighted] = .gray
button.backgroundColors[.selected] = .red
Expand All @@ -156,9 +156,8 @@ class ViewController: UIViewController {
button.setTitleFont(.systemFont(ofSize: 15, weight: .bold), for: .selected)
button.extendSize = CGSize(width: 20, height: 20)
}

buttonStack.selectionBlock = { (button, item, index) in
print("Selected: \(button)")
.selection { button, item, index in
print("Selected: \(index) - \(item)")
}

buttonStack.items = [NKButtonItem(title: "Section A"),
Expand Down
2 changes: 1 addition & 1 deletion NKButton.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'NKButton'
s.version = '4.6'
s.version = '4.7'
s.summary = 'A fully customizable UIButton'
s.description = <<-DESC
A fully customizable button that fills all lacked functions from UIButton like:
Expand Down
90 changes: 19 additions & 71 deletions NKButton/Classes/NKButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,27 +299,17 @@ open class NKButton: UIButton {
fileprivate var borderDashDict : [String : [NSNumber]] = [:]
fileprivate var titleFontDict : [String : UIFont] = [:]

fileprivate var labelFrame: FrameLayout {
return contentFrameLayout.leftFrameLayout.targetView == labelFrameLayout ? contentFrameLayout.leftFrameLayout : contentFrameLayout.rightFrameLayout
}
fileprivate var labelFrame: FrameLayout { contentFrameLayout.leftFrameLayout.targetView == labelFrameLayout ? contentFrameLayout.leftFrameLayout : contentFrameLayout.rightFrameLayout }

// MARK: -

public convenience init(title: String, titleColor: UIColor? = nil, buttonColor: UIColor? = nil, shadowColor: UIColor? = nil) {
self.init()
self.title = title

if let color = titleColor {
setTitleColor(color, for: .normal)
}

if let color = buttonColor {
setBackgroundColor(color, for: .normal)
}

if let color = shadowColor {
setShadowColor(color, for: .normal)
}
if let color = titleColor { setTitleColor(color, for: .normal) }
if let color = buttonColor { setBackgroundColor(color, for: .normal) }
if let color = shadowColor { setShadowColor(color, for: .normal) }
}

public init() {
Expand Down Expand Up @@ -458,19 +448,13 @@ open class NKButton: UIButton {
gradientLayer.colors = nil
}

if let titleFont = titleFont(for: currentState) {
titleLabel?.font = titleFont
}

if underlineTitleDisabled {
removeLabelUnderline()
}
if let titleFont = titleFont(for: currentState) { titleLabel?.font = titleFont }
if underlineTitleDisabled { removeLabelUnderline() }
}

override open func layoutSubviews() {
super.layoutSubviews()

let bounds = self.bounds
let viewSize = bounds.size

shadowLayer.frame = bounds
Expand Down Expand Up @@ -1006,59 +990,23 @@ public class UIControlStateValue<T> {
}

public subscript(state: UIControl.State) -> T? {
get {
return getter(state)
}
set {
setter(newValue, state)
}
get { getter(state) }
set { setter(newValue, state) }
}
}

public extension NKButton {

var attributedTitles: UIControlStateValue<NSAttributedString> {
return UIControlStateValue<NSAttributedString>(getter: self.attributedTitle(for:), setter: self.setAttributedTitle(_:for:))
}

var titles: UIControlStateValue<String> {
return UIControlStateValue<String>(getter: self.title(for:), setter: self.setTitle(_:for:))
}

var titleColors: UIControlStateValue<UIColor> {
return UIControlStateValue<UIColor>(getter: self.titleColor(for:), setter: self.setTitleColor(_:for:))
}

var titleFonts: UIControlStateValue<UIFont> {
return UIControlStateValue<UIFont>(getter: self.titleFont(for:), setter: self.setTitleFont(_:for:))
}

var images: UIControlStateValue<UIImage> {
return UIControlStateValue<UIImage>.init(getter: self.image, setter: self.setImage(_:for:))
}

var backgroundColors: UIControlStateValue<UIColor> {
return UIControlStateValue<UIColor>(getter: self.backgroundColor(for:), setter: self.setBackgroundColor(_:for:))
}

var borderColors: UIControlStateValue<UIColor> {
return UIControlStateValue<UIColor>(getter: self.borderColor(for:), setter: self.setBorderColor(_:for:))
}

var borderSizes: UIControlStateValue<CGFloat> {
return UIControlStateValue<CGFloat>(getter: self.borderSize(for:), setter: self.setBorderSize(_:for:))
}

var borderDashPatterns: UIControlStateValue<[NSNumber]> {
return UIControlStateValue<[NSNumber]>(getter: self.borderDashPattern(for:), setter: self.setBorderDashPattern(_:for:))
}

var shadowColors: UIControlStateValue<UIColor> {
return UIControlStateValue<UIColor>(getter: self.shadowColor(for:), setter: self.setShadowColor(_:for:))
}

var gradientColors: UIControlStateValue<[UIColor]> {
return UIControlStateValue<[UIColor]>(getter: self.gradientColor(for:), setter: self.setGradientColor(_:for:))
}
var attributedTitles: UIControlStateValue<NSAttributedString> { UIControlStateValue<NSAttributedString>(getter: self.attributedTitle(for:), setter: self.setAttributedTitle(_:for:)) }
var titles: UIControlStateValue<String> { UIControlStateValue<String>(getter: self.title(for:), setter: self.setTitle(_:for:)) }
var titleColors: UIControlStateValue<UIColor> { UIControlStateValue<UIColor>(getter: self.titleColor(for:), setter: self.setTitleColor(_:for:)) }
var titleFonts: UIControlStateValue<UIFont> { UIControlStateValue<UIFont>(getter: self.titleFont(for:), setter: self.setTitleFont(_:for:)) }
var images: UIControlStateValue<UIImage> { UIControlStateValue<UIImage>.init(getter: self.image, setter: self.setImage(_:for:)) }
var backgroundColors: UIControlStateValue<UIColor> { UIControlStateValue<UIColor>(getter: self.backgroundColor(for:), setter: self.setBackgroundColor(_:for:)) }
var borderColors: UIControlStateValue<UIColor> { UIControlStateValue<UIColor>(getter: self.borderColor(for:), setter: self.setBorderColor(_:for:)) }
var borderSizes: UIControlStateValue<CGFloat> { UIControlStateValue<CGFloat>(getter: self.borderSize(for:), setter: self.setBorderSize(_:for:)) }
var borderDashPatterns: UIControlStateValue<[NSNumber]> { UIControlStateValue<[NSNumber]>(getter: self.borderDashPattern(for:), setter: self.setBorderDashPattern(_:for:)) }
var shadowColors: UIControlStateValue<UIColor> { UIControlStateValue<UIColor>(getter: self.shadowColor(for:), setter: self.setShadowColor(_:for:)) }
var gradientColors: UIControlStateValue<[UIColor]> { UIControlStateValue<[UIColor]>(getter: self.gradientColor(for:), setter: self.setGradientColor(_:for:)) }

}
51 changes: 25 additions & 26 deletions NKButton/Classes/NKButtonStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public enum NKButtonStackSelectionMode {
case multiSelection
}

public typealias NKButtonCreationBlock<T> = ((NKButtonItem, Int) -> T)
public typealias NKButtonSelectionBlock<T> = ((T, NKButtonItem, Int) -> Void)
public typealias NKButtonCreationBlock<T> = (NKButtonItem, Int) -> T
public typealias NKButtonSelectionBlock<T> = (T, NKButtonItem, Int) -> Void

open class NKButtonStack<T: UIButton>: UIControl {

Expand All @@ -41,17 +41,9 @@ open class NKButtonStack<T: UIButton>: UIControl {
}
}

public var buttons: [T] {
return frameLayout.frameLayouts.map( { return $0.targetView as! T })
}

public var firstButton: T? {
return frameLayout.firstFrameLayout?.targetView as? T
}

public var lastButton: T? {
return frameLayout.lastFrameLayout?.targetView as? T
}
public var buttons: [T] { frameLayout.frameLayouts.map( { return $0.targetView as! T }) }
public var firstButton: T? { frameLayout.firstFrameLayout?.targetView as? T }
public var lastButton: T? { frameLayout.lastFrameLayout?.targetView as? T }

open var spacing: CGFloat {
get { frameLayout.spacing }
Expand Down Expand Up @@ -165,19 +157,8 @@ open class NKButtonStack<T: UIButton>: UIControl {
}

public var selectedIndexes: [Int] {
get {
var results = [Int]()
for button in buttons {
if button.isSelected {
results.append(button.tag)
}
}

return results
}
set {
buttons.forEach { $0.isSelected = newValue.contains($0.tag) }
}
get { buttons.filter { $0.isSelected }.map { $0.tag } }
set { buttons.forEach { $0.isSelected = newValue.contains($0.tag) } }
}

public var axis: NKLayoutAxis {
Expand Down Expand Up @@ -333,6 +314,24 @@ open class NKButtonStack<T: UIButton>: UIControl {
self.shadowOffset = offset
}

@discardableResult
public func creation(_ block: @escaping NKButtonCreationBlock<T>) -> Self {
creationBlock = block
return self
}

@discardableResult
public func configuration(_ block: @escaping NKButtonSelectionBlock<T>) -> Self {
configurationBlock = block
return self
}

@discardableResult
public func selection(_ block: @escaping NKButtonSelectionBlock<T>) -> Self {
selectionBlock = block
return self
}

// MARK: -

fileprivate func updateLayout() {
Expand Down

0 comments on commit 0045bc3

Please sign in to comment.