Skip to content

Commit 910bd22

Browse files
carlosypuntokzaher
authored andcommitted
fix attributtedString: end of range - don’t emit next if equal an tests
1 parent cdda738 commit 910bd22

File tree

4 files changed

+47
-33
lines changed

4 files changed

+47
-33
lines changed

RxCocoa/iOS/UITextField+Rx.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ extension Reactive where Base: UITextField {
3939
/// Bindable sink for `attributedText` property.
4040
public var attributedText: UIBindingObserver<Base, NSAttributedString?> {
4141
return UIBindingObserver(UIElement: self.base) { textField, attributedText in
42-
textField.attributedText = attributedText
42+
if textField.attributedText != attributedText {
43+
textField.attributedText = attributedText
44+
}
4345
}
4446
}
4547

RxCocoa/iOS/UITextView+Rx.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extension Reactive where Base: UITextView {
7171
.observeOn(MainScheduler.asyncInstance)
7272
.map { _ in
7373
guard let textStorage = textView?.textStorage else { return nil }
74-
return textStorage.attributedSubstring(from: NSRange(location: 0, length: textStorage.string.utf8.count))
74+
return textStorage.attributedSubstring(from: NSRange(location: 0, length: textStorage.length))
7575
}
7676
?? Observable.empty()
7777

@@ -84,8 +84,6 @@ extension Reactive where Base: UITextView {
8484
// including marked text selection which is imporant for proper input
8585
// when IME input method is used.
8686
if textView.attributedText != attributedText {
87-
print(textView.attributedText)
88-
print(attributedText)
8987
textView.attributedText = attributedText
9088
}
9189
}

Tests/RxCocoaTests/UITextField+RxTests.swift

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,40 @@ final class UITextFieldTests : RxTest {
2323

2424
func testSettingTextDoesntClearMarkedText() {
2525
let textField = UITextFieldSubclass(frame: CGRect.zero)
26-
2726
textField.text = "Text1"
28-
textField.settedText = false
27+
textField.isSettedText = false
2928
textField.rx.text.on(.next("Text1"))
30-
XCTAssertTrue(!textField.settedText)
29+
XCTAssertTrue(!textField.isSettedText)
3130
textField.rx.text.on(.next("Text2"))
32-
XCTAssertTrue(textField.settedText)
31+
XCTAssertTrue(textField.isSettedText)
3332
}
3433

3534
func testLabel_attributedTextObserver() {
36-
let label = UILabel()
37-
XCTAssertEqual(label.attributedText, nil)
38-
let text = NSAttributedString(string: "Hello!")
39-
_ = Observable.just(text).bind(to: label.rx.attributedText)
40-
41-
XCTAssertEqual(label.attributedText, text)
35+
let textField = UITextField()
36+
XCTAssertEqual(textField.attributedText, "".textFieldAttributedString)
37+
let attributedText = "Hello!".textFieldAttributedString
38+
_ = Observable.just(attributedText).bind(to: textField.rx.attributedText)
39+
XCTAssertEqual(textField.attributedText!, attributedText)
40+
}
41+
}
42+
43+
private extension String {
44+
var textFieldAttributedString: NSAttributedString {
45+
let tf = UITextField()
46+
tf.attributedText = NSAttributedString(string: self)
47+
return tf.attributedText!
4248
}
4349
}
4450

4551
final class UITextFieldSubclass : UITextField {
46-
var settedText = false
52+
var isSettedText = false
4753

4854
override var text: String? {
4955
get {
5056
return super.text
5157
}
5258
set {
53-
settedText = true
59+
isSettedText = true
5460
super.text = newValue
5561
}
5662
}

Tests/RxCocoaTests/UITextView+RxTests.swift

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@ final class UITextViewTests : RxTest {
2727
let textView = UITextViewSubclass2(frame: CGRect.zero)
2828

2929
textView.text = "Text1"
30-
textView.settedText = false
30+
textView.isSettedText = false
3131
textView.rx.text.on(.next("Text1"))
32-
XCTAssertTrue(!textView.settedText)
32+
XCTAssertTrue(!textView.isSettedText)
3333
textView.rx.text.on(.next("Text2"))
34-
XCTAssertTrue(textView.settedText)
34+
XCTAssertTrue(textView.isSettedText)
3535
}
3636

3737
func testSettingTextDoesntClearMarkedAttributtedText() {
3838
let textView = UITextViewSubclass2(frame: CGRect.zero)
3939

40-
let initialAttributedString = NSAttributedString(string: "Test1")
41-
let nextAttributedString = NSAttributedString(string: "Test1")
40+
let testAttributedString = "Test1".textViewAttributedString
41+
let test2AttributedString = "Test2".textViewAttributedString
4242

43-
textView.attributedText = initialAttributedString
44-
let textViewSettedAttributedText = textView.attributedText
45-
textView.settedAttributedText = false
46-
47-
textView.rx.attributedText.on(.next(textViewSettedAttributedText))
48-
XCTAssertTrue(!textView.settedAttributedText)
49-
textView.rx.attributedText.on(.next(nextAttributedString))
50-
XCTAssertTrue(textView.settedAttributedText)
43+
textView.attributedText = testAttributedString
44+
textView.isSettedAttributedText = false
45+
textView.rx.attributedText.on(.next(testAttributedString))
46+
XCTAssertTrue(!textView.isSettedAttributedText)
47+
textView.rx.attributedText.on(.next(testAttributedString))
48+
XCTAssertTrue(!textView.isSettedAttributedText)
49+
textView.rx.attributedText.on(.next(test2AttributedString))
50+
XCTAssertTrue(textView.isSettedAttributedText)
5151
}
5252

5353
func testDidBeginEditing() {
@@ -131,16 +131,24 @@ final class UITextViewTests : RxTest {
131131
}
132132
}
133133

134+
private extension String {
135+
var textViewAttributedString: NSAttributedString {
136+
let tf = UITextView()
137+
tf.attributedText = NSAttributedString(string: self)
138+
return tf.attributedText!
139+
}
140+
}
141+
134142
final class UITextViewSubclass2 : UITextView {
135-
var settedText = false
136-
var settedAttributedText = false
143+
var isSettedText = false
144+
var isSettedAttributedText = false
137145

138146
override var text: String? {
139147
get {
140148
return super.text
141149
}
142150
set {
143-
settedText = true
151+
isSettedText = true
144152
super.text = newValue
145153
}
146154
}
@@ -150,7 +158,7 @@ final class UITextViewSubclass2 : UITextView {
150158
return super.attributedText
151159
}
152160
set {
153-
settedAttributedText = true
161+
isSettedAttributedText = true
154162
super.attributedText = newValue
155163
}
156164
}

0 commit comments

Comments
 (0)