Skip to content

Commit

Permalink
Add UIButton’s reactive wrapper for `setAttributedTitle(_:controlStat…
Browse files Browse the repository at this point in the history
…e:)`
  • Loading branch information
yusayusa authored and kzaher committed Feb 13, 2017
1 parent 889150b commit f29e580
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
19 changes: 19 additions & 0 deletions RxCocoa/iOS/UIButton+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,22 @@ extension Reactive where Base: UIButton {

}
#endif

#if os(iOS) || os(tvOS)

#if !RX_NO_MODULE
import RxSwift
#endif
import UIKit

extension Reactive where Base: UIButton {

/// Reactive wrapper for `setAttributedTitle(_:controlState:)`
public func attributedTitle(for controlState: UIControlState = []) -> UIBindingObserver<Base, NSAttributedString?> {
return UIBindingObserver<Base, NSAttributedString?>(UIElement: self.base) { (button, attributedTitle) -> () in
button.setAttributedTitle(attributedTitle, for: controlState)
}
}

}
#endif
24 changes: 24 additions & 0 deletions Tests/RxCocoaTests/UIButton+RxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ extension UIButtonTests {
_ = Observable.just("normal").subscribe(button.rx.title())
XCTAssertTrue(button.title(for: []) == "normal")
}

func testAttributedTitleNormal() {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))

XCTAssertFalse(button.attributedTitle(for: []) == NSAttributedString(string: "normal"))
_ = Observable.just(NSAttributedString(string: "normal")).subscribe(button.rx.attributedTitle(for: []))
XCTAssertTrue(button.attributedTitle(for: []) == NSAttributedString(string: "normal"))
}

func testAttributedTitleSelected() {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))

XCTAssertFalse(button.attributedTitle(for: .selected) == NSAttributedString(string: "normal"))
_ = Observable.just(NSAttributedString(string: "normal")).subscribe(button.rx.attributedTitle(for: .selected))
XCTAssertTrue(button.attributedTitle(for: .selected) == NSAttributedString(string: "normal"))
}

func testAttributedTitleDefault() {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 1, height: 1))

XCTAssertFalse(button.attributedTitle(for: []) == NSAttributedString(string: "normal"))
_ = Observable.just(NSAttributedString(string: "normal")).subscribe(button.rx.attributedTitle())
XCTAssertTrue(button.attributedTitle(for: []) == NSAttributedString(string: "normal"))
}
}

#if os(iOS)
Expand Down

0 comments on commit f29e580

Please sign in to comment.