From d1c2ac2a6f4830aa7a697ec55d24d58cf8f4602b Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Fri, 23 Jun 2017 10:42:10 +0100 Subject: [PATCH 1/2] Use the correct range for undo. --- Aztec/Classes/TextKit/TextView.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Aztec/Classes/TextKit/TextView.swift b/Aztec/Classes/TextKit/TextView.swift index 48037bc7f..37cf510ff 100644 --- a/Aztec/Classes/TextKit/TextView.swift +++ b/Aztec/Classes/TextKit/TextView.swift @@ -909,7 +909,8 @@ open class TextView: UITextView { open func setLink(_ url: URL, title: String, inRange range: NSRange) { let originalText = attributedText.attributedSubstring(from: range) - let finalRange = range + let attributedTitle = NSAttributedString(string: title) + let finalRange = NSRange(location: range.location, length: attributedTitle.length) undoManager?.registerUndo(withTarget: self, handler: { [weak self] target in self?.undoTextReplacement(of: originalText, finalRange: finalRange) From 838789efe1072544b8209f77f3f22af218f36013 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Fri, 23 Jun 2017 11:03:52 -0300 Subject: [PATCH 2/2] Pushes a small fix to the final selection after inserting a link. --- Aztec/Classes/TextKit/TextView.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Aztec/Classes/TextKit/TextView.swift b/Aztec/Classes/TextKit/TextView.swift index 37cf510ff..8aee21a70 100644 --- a/Aztec/Classes/TextKit/TextView.swift +++ b/Aztec/Classes/TextKit/TextView.swift @@ -919,11 +919,11 @@ open class TextView: UITextView { let formatter = LinkFormatter() formatter.attributeValue = url let attributes = formatter.apply(to: typingAttributes) - let linkWasPresent = formatter.present(in: storage, at: range) + storage.replaceCharacters(in: range, with: NSAttributedString(string: title, attributes: attributes)) - if range.length == 0 && !linkWasPresent { - selectedRange = NSMakeRange(range.location + (title as NSString).length, 0) - } + + selectedRange = NSRange(location: finalRange.location + finalRange.length, length: 0) + delegate?.textViewDidChange?(self) }