         let targetRange = formatter.applicationRange(for: selectedRange, in: storage)
 
         performUndoable(at: targetRange) { 
-            formatter.applyAttributes(to: storage, at: targetRange)
+            let finalRange = formatter.applyAttributes(to: storage, at: targetRange)
             typingAttributes = textStorage.attributes(at: targetRange.location, effectiveRange: nil)
+            return finalRange
         }
     }
 

    }
}

//MARK: - Undo implementation

private extension TextView {

    func performUndoable(at range: NSRange, block: (() -> Void)) {
        let originalString = storage.attributedSubstring(from: range)

        block()

        undoManager?.registerUndo(withTarget: self, handler: { [weak self] target in
            self?.undoTextReplacement(of: originalString, finalRange: range)
        })

        delegate?.textViewDidChange?(self)

        let targetRange = formatter.applicationRange(for: selectedRange, in: storage)

        performUndoable(at: targetRange) {
            let finalRange = formatter.removeAttributes(from: storage, at: targetRange)
            typingAttributes = textStorage.attributes(at: targetRange.location, effectiveRange: nil)
            return finalRange
        }
    }

