Skip to content

Commit

Permalink
Merge pull request #33 from kmcgill88/21-maintian-toolbar-settings
Browse files Browse the repository at this point in the history
Allow setting toolbar items properties before setToolbarItems
  • Loading branch information
kmcgill88 authored Mar 8, 2018
2 parents 55f1200 + 25f5e69 commit dc7705e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
85 changes: 84 additions & 1 deletion Example/Tests/McPickerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class McPickerTests: XCTestCase {
XCTAssertNil(mcPicker.showsSelectionIndicator)
}

func testSetToolbarButtonsColorSetsCancelAndDone() {
func testSetToolbarButtons_colorSetsCancelAndDone() {
// Given
//
let mcPicker = McPicker(data: data)
Expand All @@ -106,6 +106,66 @@ class McPickerTests: XCTestCase {
XCTAssertEqual(UIColor.purple, doneBarButton.tintColor)
}

func testSetToolbarButtonsColorSetsCancelAndDone_setColorBeforeToolBarItems() {
// Given
//
let mcPicker = McPicker(data: data)
mcPicker.toolbarButtonsColor = UIColor.purple

// When
//
let fixedSpace = McPickerBarButtonItem.fixedSpace(width: 20.0)
let flexibleSpace = McPickerBarButtonItem.flexibleSpace()
let fireButton = McPickerBarButtonItem.done(mcPicker: mcPicker, title: "Fire!!!")
let cancelButton = McPickerBarButtonItem.cancel(mcPicker: mcPicker, barButtonSystemItem: .cancel)
mcPicker.setToolbarItems(items: [fixedSpace, cancelButton, flexibleSpace, fireButton, fixedSpace])

// Then
//
XCTAssertEqual(UIColor.purple, fireButton.tintColor)
XCTAssertEqual(UIColor.purple, cancelButton.tintColor)
}

func testSetToolbarDoneButtonsColor_setColorBeforeToolBarItems() {
// Given
//
let mcPicker = McPicker(data: data)
mcPicker.toolbarDoneButtonColor = UIColor.purple

// When
//
let fixedSpace = McPickerBarButtonItem.fixedSpace(width: 20.0)
let flexibleSpace = McPickerBarButtonItem.flexibleSpace()
let fireButton = McPickerBarButtonItem.done(mcPicker: mcPicker, title: "Fire!!!")
let cancelButton = McPickerBarButtonItem.cancel(mcPicker: mcPicker, barButtonSystemItem: .cancel)
mcPicker.setToolbarItems(items: [fixedSpace, cancelButton, flexibleSpace, fireButton, fixedSpace])

// Then
//
XCTAssertEqual(UIColor.purple, fireButton.tintColor)
XCTAssertNotEqual(UIColor.purple, cancelButton.tintColor)
}

func testSetToolbarCancelButtonsColor_setColorBeforeToolBarItems() {
// Given
//
let mcPicker = McPicker(data: data)
mcPicker.toolbarCancelButtonColor = UIColor.purple

// When
//
let fixedSpace = McPickerBarButtonItem.fixedSpace(width: 20.0)
let flexibleSpace = McPickerBarButtonItem.flexibleSpace()
let fireButton = McPickerBarButtonItem.done(mcPicker: mcPicker, title: "Fire!!!")
let cancelButton = McPickerBarButtonItem.cancel(mcPicker: mcPicker, barButtonSystemItem: .cancel)
mcPicker.setToolbarItems(items: [fixedSpace, cancelButton, flexibleSpace, fireButton, fixedSpace])

// Then
//
XCTAssertNotEqual(UIColor.purple, fireButton.tintColor)
XCTAssertEqual(UIColor.purple, cancelButton.tintColor)
}

func testSetToolbarDoneButtonsColorSetsDone() {
// Given
//
Expand Down Expand Up @@ -175,6 +235,29 @@ class McPickerTests: XCTestCase {
XCTAssertEqual(expectedFont, doneBarButton.titleTextAttributes(for: .selected)?[NSAttributedStringKey.font.rawValue] as! UIFont)
}

func testSetToolbarItemsFont_afterToolbarItemsSet() {
// Given
//
let expectedFont = UIFont(name:"American Typewriter", size: 17)!
let mcPicker = McPicker(data: data)
mcPicker.toolbarItemsFont = expectedFont
let fixedSpace = McPickerBarButtonItem.fixedSpace(width: 20.0)
let flexibleSpace = McPickerBarButtonItem.flexibleSpace()
let fireButton = McPickerBarButtonItem.done(mcPicker: mcPicker, title: "Fire!!!")
let cancelButton = McPickerBarButtonItem.cancel(mcPicker: mcPicker, barButtonSystemItem: .cancel)

// When
//
mcPicker.setToolbarItems(items: [fixedSpace, cancelButton, flexibleSpace, fireButton, fixedSpace])

// Then
//
XCTAssertEqual(expectedFont, cancelButton.titleTextAttributes(for: .normal)?[NSAttributedStringKey.font.rawValue] as! UIFont)
XCTAssertEqual(expectedFont, fireButton.titleTextAttributes(for: .normal)?[NSAttributedStringKey.font.rawValue] as! UIFont)
XCTAssertEqual(expectedFont, cancelButton.titleTextAttributes(for: .selected)?[NSAttributedStringKey.font.rawValue] as! UIFont)
XCTAssertEqual(expectedFont, fireButton.titleTextAttributes(for: .selected)?[NSAttributedStringKey.font.rawValue] as! UIFont)
}

func testSetPickerBackgroundColor() {
// Given
//
Expand Down
26 changes: 23 additions & 3 deletions McPicker/Classes/McPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ open class McPicker: UIView {
}
}
}
public var toolbarBarTintColor: UIColor? {
didSet { toolbar.barTintColor = toolbarBarTintColor }
}
public var toolbarItemsFont: UIFont? {
didSet {
applyToolbarButtonItemsSettings { (barButtonItem) in
Expand All @@ -72,6 +69,9 @@ open class McPicker: UIView {
}
}
}
public var toolbarBarTintColor: UIColor? {
didSet { toolbar.barTintColor = toolbarBarTintColor }
}
public var pickerBackgroundColor: UIColor? {
didSet { picker.backgroundColor = pickerBackgroundColor }
}
Expand Down Expand Up @@ -259,6 +259,8 @@ open class McPicker: UIView {

open func setToolbarItems(items: [McPickerBarButtonItem]) {
toolbar.items = items

setToolbarProperties()
}

open override func willMove(toWindow newWindow: UIWindow?) {
Expand Down Expand Up @@ -368,6 +370,24 @@ open class McPicker: UIView {
}
}

private func setToolbarProperties() {
if let _toolbarButtonsColor = toolbarButtonsColor {
toolbarButtonsColor = _toolbarButtonsColor
}

if let _toolbarDoneButtonColor = toolbarDoneButtonColor {
toolbarDoneButtonColor = _toolbarDoneButtonColor
}

if let _toolbarCancelButtonColor = toolbarCancelButtonColor {
toolbarCancelButtonColor = _toolbarCancelButtonColor
}

if let _toolbarItemsFont = toolbarItemsFont {
toolbarItemsFont = _toolbarItemsFont
}
}

private func applyToolbarButtonItemsSettings(withAction: Selector? = nil, settings: (_ barButton: UIBarButtonItem) -> Void) {
for item in toolbar.items ?? [] {
if let action = withAction, action == item.action {
Expand Down

0 comments on commit dc7705e

Please sign in to comment.