Skip to content

Commit c29e8f9

Browse files
committed
Polishes attributedText.
1 parent 9c273c0 commit c29e8f9

File tree

7 files changed

+146
-132
lines changed

7 files changed

+146
-132
lines changed

RxCocoa/iOS/UITextField+Rx.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@ extension Reactive where Base: UITextField {
3737
}
3838

3939
/// Bindable sink for `attributedText` property.
40-
public var attributedText: UIBindingObserver<Base, NSAttributedString?> {
41-
return UIBindingObserver(UIElement: self.base) { textField, attributedText in
42-
if textField.attributedText != attributedText {
43-
textField.attributedText = attributedText
40+
public var attributedText: ControlProperty<NSAttributedString?> {
41+
return UIControl.rx.value(
42+
base,
43+
getter: { textField in
44+
textField.attributedText
45+
}, setter: { textField, value in
46+
// This check is important because setting text value always clears control state
47+
// including marked text selection which is imporant for proper input
48+
// when IME input method is used.
49+
if textField.attributedText != value {
50+
textField.attributedText = value
51+
}
4452
}
45-
}
53+
)
4654
}
4755

4856
}

RxCocoa/iOS/UITextView+Rx.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,15 @@ extension Reactive where Base: UITextView {
7070
// so rebinding a value will cause an exception to be thrown.
7171
.observeOn(MainScheduler.asyncInstance)
7272
.map { _ in
73-
guard let textStorage = textView?.textStorage else { return nil }
74-
return textStorage.attributedSubstring(from: NSRange(location: 0, length: textStorage.length))
73+
return textView?.attributedText
7574
}
7675
?? Observable.empty()
7776

7877
return textChanged
7978
.startWith(attributedText)
8079
}
8180

82-
let bindingObserver = UIBindingObserver(UIElement: self.base) { (textView, attributedText: NSAttributedString?) in
81+
let bindingObserver = Binder(self.base) { (textView, attributedText: NSAttributedString?) in
8382
// This check is important because setting text value always clears control state
8483
// including marked text selection which is imporant for proper input
8584
// when IME input method is used.

RxExample/RxExample-iOSUITests/FlowTests.swift

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ extension FlowTests {
125125
extension FlowTests {
126126
func testControls() {
127127
for test in [
128-
_testDatePicker,
129-
_testBarButtonItemTap,
130-
_testButtonTap,
131-
_testSegmentedControl,
132-
_testUISwitch,
133-
_testUITextField,
134-
_testUITextView,
135-
_testSlider
128+
_testDatePicker,
129+
_testBarButtonItemTap,
130+
_testButtonTap,
131+
_testSegmentedControl,
132+
_testUISwitch,
133+
_testUITextField,
134+
_testUITextView,
135+
_testSlider
136136
] {
137137
goToControlsView()
138138
test()
@@ -148,9 +148,14 @@ extension FlowTests {
148148
tableView.cells.allElementsBoundByIndex[5].tap()
149149
}
150150

151-
func checkDebugLabelValue(_ expected: String) {
151+
func checkDebugLabelValue(_ expected: String, hasPrefix: Bool = false) {
152152
let textValue = app.staticTexts["debugLabel"].value as? String
153-
XCTAssertEqual(textValue, expected)
153+
if hasPrefix {
154+
XCTAssertTrue((textValue ?? "").hasPrefix(expected))
155+
}
156+
else {
157+
XCTAssertEqual(textValue, expected)
158+
}
154159
}
155160

156161
func _testDatePicker() {
@@ -184,9 +189,9 @@ extension FlowTests {
184189

185190
func _testUISwitch() {
186191
let switchControl = app.switches.allElementsBoundByIndex[0]
187-
switchControl.tap()
192+
switchControl.twoFingerTap()
188193
checkDebugLabelValue("UISwitch value false")
189-
switchControl.tap()
194+
switchControl.twoFingerTap()
190195
checkDebugLabelValue("UISwitch value true")
191196
}
192197

@@ -195,19 +200,27 @@ extension FlowTests {
195200
textField.tap()
196201
textField.typeText("f")
197202
checkDebugLabelValue("UITextField text f")
203+
let textField2 = app.textFields.allElementsBoundByIndex[1]
204+
textField2.tap()
205+
textField2.typeText("f2")
206+
checkDebugLabelValue("UITextField attributedText f2{", hasPrefix: true)
198207
}
199208

200209
func _testUITextView() {
201210
let textView = app.textViews.allElementsBoundByIndex[0]
202211
textView.tap()
203212
textView.typeText("f")
204213
checkDebugLabelValue("UITextView text f")
214+
let textView2 = app.textViews.allElementsBoundByIndex[1]
215+
textView2.tap()
216+
textView2.typeText("f2")
217+
checkDebugLabelValue("UITextView attributedText f2{", hasPrefix: true)
205218
}
206219

207220
func _testSlider() {
208221
let slider = app.sliders.allElementsBoundByIndex[0]
209222
slider.adjust(toNormalizedSliderPosition: 0)
210-
checkDebugLabelValue("UISlider value 0.0")
223+
checkDebugLabelValue("UISlider value 0.0", hasPrefix: true)
211224
}
212225
}
213226

0 commit comments

Comments
 (0)