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

Input customization point #54

Merged
merged 2 commits into from
Apr 19, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion MessageViewController/MessageAutocompleteController.swift
Original file line number Diff line number Diff line change
@@ -233,7 +233,7 @@ public final class MessageAutocompleteController: MessageTextViewListener {
preserveTypingAttributes(for: textView)
}

public func willChangeRange(textView: MessageTextView, to range: NSRange) {
public func willChangeText(textView: MessageTextView, inRange range: NSRange, to: String) -> Bool {

// range.length == 1: Remove single character
// range.lowerBound < textView.selectedRange.lowerBound: Ignore trying to delete
@@ -248,6 +248,7 @@ public final class MessageAutocompleteController: MessageTextViewListener {
if let isAutocomplete = attribute[NSAttributedAutocompleteKey] as? Bool, isAutocomplete {
// Remove the autocompleted substring
let lowerRange = NSRange(location: 0, length: range.location + 1)
var shouldPreserveTypedText = true
textView.attributedText.enumerateAttribute(NSAttributedAutocompleteKey, in: lowerRange, options: .reverse, using: { (_, range, stop) in

// Only delete the first found range
@@ -258,9 +259,12 @@ public final class MessageAutocompleteController: MessageTextViewListener {
textView.selectedRange = NSRange(location: range.location, length: 0)
self.textView.textViewDidChange(textView)
self.preserveTypingAttributes(for: textView)
shouldPreserveTypedText = false
})
return shouldPreserveTypedText
}
}
return true
}

}
11 changes: 8 additions & 3 deletions MessageViewController/MessageTextView.swift
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import UIKit
public protocol MessageTextViewListener: class {
func didChange(textView: MessageTextView)
func didChangeSelection(textView: MessageTextView)
func willChangeRange(textView: MessageTextView, to range: NSRange)
func willChangeText(textView: MessageTextView, inRange: NSRange, to: String) -> Bool
}

open class MessageTextView: UITextView, UITextViewDelegate {
@@ -134,8 +134,13 @@ open class MessageTextView: UITextView, UITextViewDelegate {
}

public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
enumerateListeners { $0.willChangeRange(textView: self, to: range) }
return true
// If listener changes text then subsequent listeners will probably get incorrect affected range
// and text as they were changed by previous listener. So be careful playing with textView
var shouldChange = true
// if at least one listener changes text and needs to ignore typed text then this method returns
// that just typed text needs to be ignored
enumerateListeners { shouldChange = shouldChange && $0.willChangeText(textView: self, inRange: range, to: text) }
return shouldChange
}

}
4 changes: 3 additions & 1 deletion MessageViewController/MessageView.swift
Original file line number Diff line number Diff line change
@@ -336,6 +336,8 @@ public final class MessageView: UIView, MessageTextViewListener {
delegate?.selectionDidChange(messageView: self)
}

public func willChangeRange(textView: MessageTextView, to range: NSRange) {}
public func willChangeText(textView: MessageTextView, inRange: NSRange, to: String) -> Bool {
return true
}

}