Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rx.attributedText to UITextView and UITextField #1249

Merged
merged 5 commits into from
Oct 1, 2017

Conversation

carlosypunto
Copy link
Contributor

Requested in issue #1246

@RxPullRequestBot
Copy link

RxPullRequestBot commented May 12, 2017

2 Warnings
⚠️ Please target PRs to develop branch
⚠️ No CHANGELOG changes made

Generated by 🚫 Danger

// when IME input method is used.
if textView.attributedText != attributedText {
print(textView.attributedText)
print(attributedText)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are accidental prints.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed in las commit

.observeOn(MainScheduler.asyncInstance)
.map { _ in
guard let textStorage = textView?.textStorage else { return nil }
return textStorage.attributedSubstring(from: NSRange(location: 0, length: textStorage.string.utf8.count))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what is the length here. Why do you think it's textStorage.string.utf8.count?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In last commit is changed to NSRange(location: 0, length: textStorage.length) the full range

/// Bindable sink for `attributedText` property.
public var attributedText: UIBindingObserver<Base, NSAttributedString?> {
return UIBindingObserver(UIElement: self.base) { textField, attributedText in
textField.attributedText = attributedText
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to find way to make sure this isn't updated in case attributed text is the same and test this.

if textField.attributedText != attributedText {
    textField.attributedText = attributedText
}

We had issues with IME.

// This check is important because setting text value always clears control state
// including marked text selection which is imporant for proper input 
// when IME input method is used.

XCTAssertTrue(textField.settedText)
}

func testLabel_attributedTextObserver() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is copied code from UILabel, but nothing is updated, it doesn't test UITextField.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in last commit

}
}

final class UITextFieldSubclass : UITextField {
var set: Bool = false

var settedText = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe didSetText would be more descriptive.

var set: Bool = false

var settedText = false
var settedAttributedText = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same like for UITextField. This should probably be didSetText and didSetAttributedText.

@carlosypunto
Copy link
Contributor Author

carlosypunto commented May 14, 2017

I think now is correct.
Maybe you want APIWrapper RxExample will log attributedText changes, I can add to PR changing this with:

        let attributedTextValue = textView.rx.attributedText.asObservable()
        Observable.zip(textViewValue.asObservable(), attributedTextValue, resultSelector: { text, attributedString -> String in
            return "UITextView text: \(text)\nUITextView attributedText: \(attributedString?.description ?? "nil")"
        })
            .subscribe(onNext: { [weak self] x in
                self?.debug("UITextView text \(x)")
            })
            .disposed(by: disposeBag)

@kzaher
Copy link
Member

kzaher commented May 15, 2017

Yeah, we can do this in a separate PR.

XCTAssertTrue(textField.isSettedText)
}

func testLabel_attributedTextObserver() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test title says testLabel. It should probably be just test because there is no label that we are testing inside this test.

textField.text = "Text1"
textField.set = false
textField.isSettedText = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think isSettedText is formulated correctly. Maybe use didSetText.

var textFieldAttributedString: NSAttributedString {
let tf = UITextField()
tf.attributedText = NSAttributedString(string: self)
return tf.attributedText!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this code. UITextField is being created and then discarded immediately.

Is there something wrong with just using NSAttributedText(string: "Hello!")?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NSAttributedText(string: "Hello!") style can't have style info. When we set this to a Textfield or Textview, they overwrite its internal NSAttributedString properties in the case only text, then, if we get the control's attributedStyle is not equal to the setted. So this function returns and NSAttributedText with all properties with effect in these controls being it comparable with getted style from control.

override var text: String? {
get {
return super.text
}
set {
set = true
isSettedText = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has the same grammar issue.

var textViewAttributedString: NSAttributedString {
let tf = UITextView()
tf.attributedText = NSAttributedString(string: self)
return tf.attributedText!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same problem with redundant extension.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same explanation than TextField case


final class UITextViewSubclass2 : UITextView {
var isSettedText = false
var isSettedAttributedText = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has the same grammar issue.

Copy link
Member

@kzaher kzaher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. I think some of the previous comments weren't addressed, and there are some new ones.

/// Bindable sink for `attributedText` property.
public var attributedText: UIBindingObserver<Base, NSAttributedString?> {
return UIBindingObserver(UIElement: self.base) { textField, attributedText in
if textField.attributedText != attributedText {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Why do we need this check?

@kzaher kzaher force-pushed the addAttributedString branch from 9147175 to bd3e2d5 Compare October 1, 2017 11:46
@kzaher kzaher changed the base branch from develop to rxswift4.0-swift4.0 October 1, 2017 11:47
@kzaher kzaher force-pushed the addAttributedString branch 2 times, most recently from 9dfb3e6 to d2ccc22 Compare October 1, 2017 12:43
@kzaher kzaher force-pushed the addAttributedString branch from d2ccc22 to 981b98a Compare October 1, 2017 12:45
@kzaher kzaher merged commit c29e8f9 into rxswift4.0-swift4.0 Oct 1, 2017
@kzaher kzaher deleted the addAttributedString branch October 1, 2017 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants