From f29e58003b87016dfd9374031bd3545af659caf8 Mon Sep 17 00:00:00 2001 From: yusayusa Date: Sat, 11 Feb 2017 01:09:29 +0900 Subject: [PATCH] =?UTF-8?q?Add=20UIButton=E2=80=99s=20reactive=20wrapper?= =?UTF-8?q?=20for=20`setAttributedTitle(=5F:controlState:)`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RxCocoa/iOS/UIButton+Rx.swift | 19 ++++++++++++++++++ Tests/RxCocoaTests/UIButton+RxTests.swift | 24 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/RxCocoa/iOS/UIButton+Rx.swift b/RxCocoa/iOS/UIButton+Rx.swift index e40e499c..ea26da2f 100644 --- a/RxCocoa/iOS/UIButton+Rx.swift +++ b/RxCocoa/iOS/UIButton+Rx.swift @@ -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 { + return UIBindingObserver(UIElement: self.base) { (button, attributedTitle) -> () in + button.setAttributedTitle(attributedTitle, for: controlState) + } + } + + } +#endif diff --git a/Tests/RxCocoaTests/UIButton+RxTests.swift b/Tests/RxCocoaTests/UIButton+RxTests.swift index 7fa64b45..78471725 100644 --- a/Tests/RxCocoaTests/UIButton+RxTests.swift +++ b/Tests/RxCocoaTests/UIButton+RxTests.swift @@ -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)