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

textView(_:shouldChangeTextIn:replacementString:) not been called #65

Closed
wonderhero opened this issue Aug 3, 2018 · 8 comments
Closed
Assignees
Labels

Comments

@wonderhero
Copy link

Hi szweier,

I have added SZMentionsListener in my code. I also implemented textView(_:shouldChangeTextIn:replacementString:) in my controller. I realised that if I paste string from external app into the textview that has implemented this library. My callback not getting called. I have stepped into the library code.

else if text.count > 1 {
            //Pasting
            if let editedMention = mentions.mentionBeingEdited(atRange: range) {
                clearMention(editedMention)
            }

            textView.delegate = nil
            if let mutableAttributedString = textView.attributedText.mutableCopy() as? NSMutableAttributedString {
                mutableAttributedString.replaceCharacters(in: range, with: NSAttributedString(string: text))
                mutableAttributedString.apply(defaultTextAttributes, range: NSRange(location: range.location, length: text.count))
                mentionsTextView.selectedRange = NSRange(location: 0, length: 0)
                textView.attributedText = mutableAttributedString
                mentionsTextView.selectedRange = NSRange(location: range.location + text.count, length: 0)
            }
            
            mentions.adjustMentions(forTextChangeAtRange: range, text: text)
            textView.delegate = self
            
            return false
        }
        _ = delegate?.textView?(textView, shouldChangeTextIn: range, replacementText: text)

The delegate callback will not get called if it fulfilling the text pasting action. However, I need to do some adjustment to my UITextView (To resize the UI) if received the callback.

Is there any way I can get my delegate get called in the event of pasting text?

@szweier
Copy link
Owner

szweier commented Aug 3, 2018

@wonderhero please check out this PR and let me know if it solves your problem. #66

@szweier szweier self-assigned this Aug 3, 2018
@szweier szweier closed this as completed Aug 4, 2018
@szweier szweier added the bug label Aug 4, 2018
@wonderhero
Copy link
Author

@szweier I have tested the code and tried to step into the implementation. The defer statement still not getting called. Would you mind to have a check on this again?

@szweier
Copy link
Owner

szweier commented Aug 7, 2018

@wonderhero are other delegate methods being called? I find it strange that the defer wouldn’t be hit. Any chance you can push a sample app to a public repo that demonstrates this problem?

@szweier szweier reopened this Aug 7, 2018
@wonderhero
Copy link
Author

@szweier yes, other delegate methods working well. Only textView(_:shouldChangeTextIn:replacementString:) not been called. Let me try setup a sample app for this.

@szweier
Copy link
Owner

szweier commented Aug 7, 2018

@wonderhero I just placed the following code into the ExampleViewController

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        print("called")
        return true
    }

And it appears to be getting called (print statement is shown in console). It is worth noting that, due to the way the listener works, what you return from this method is ignored.

@wonderhero
Copy link
Author

@szweier I have setup a sample project to test this. Would you mind to check it out? https://github.com/wonderhero/SampleMentionApp

If I paste a text into the textview. The print text won't get executed.

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange,
                  replacementText text: String) -> Bool {
        print("TextView change text in")
        return true
    }

@wonderhero
Copy link
Author

@szweier You are right, I have tested it. The listener is working. It is my bad.

@DavonMars
Copy link

DavonMars commented Feb 12, 2020

In my case I forgot to initialize listener with viewController delegate
public init( mentionsTextView: UITextView, delegate: UITextViewDelegate? = nil, ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants