diff --git a/Source/SLKTextViewController.h b/Source/SLKTextViewController.h index aa66d0af..b674a517 100644 --- a/Source/SLKTextViewController.h +++ b/Source/SLKTextViewController.h @@ -509,7 +509,7 @@ NS_CLASS_AVAILABLE_IOS(7_0) @interface SLKTextViewController : UIViewController /** Accepts the autocompletion, replacing the detected word with a new string, keeping the prefix. - This method is a convinience of -acceptAutoCompletionWithString:keepPrefix: + This method is a convenience of -acceptAutoCompletionWithString:keepPrefix: @param string The string to be used for replacing autocompletion placeholders. */ @@ -523,6 +523,13 @@ NS_CLASS_AVAILABLE_IOS(7_0) @interface SLKTextViewController : UIViewController */ - (void)acceptAutoCompletionWithString:(NSString *_Nullable)string keepPrefix:(BOOL)keepPrefix; +/** + Accepts the autocompletion, replacing the detected word with a new attributed string, and optionally replacing the prefix too. + + @param attributedString The attributed string to be used for replacing autocompletion placeholders. + @param keepPrefix YES if the prefix shouldn't be overidden. + */ +- (void)acceptAutoCompletionWithAttributedString:(NSAttributedString *_Nullable)attributedString keepPrefix:(BOOL)keepPrefix; #pragma mark - Text Caching ///------------------------------------------------ diff --git a/Source/SLKTextViewController.m b/Source/SLKTextViewController.m index 60a3fc18..7a784bd8 100644 --- a/Source/SLKTextViewController.m +++ b/Source/SLKTextViewController.m @@ -1760,6 +1760,32 @@ - (void)acceptAutoCompletionWithString:(NSString *)string keepPrefix:(BOOL)keepP [self cancelAutoCompletion]; } +- (void)acceptAutoCompletionWithAttributedString:(NSAttributedString *)attributedString keepPrefix:(BOOL)keepPrefix +{ + if (attributedString.length == 0) { + return; + } + + NSUInteger location = self.foundPrefixRange.location; + if (keepPrefix) { + location += self.foundPrefixRange.length; + } + + NSUInteger length = self.foundWord.length; + if (!keepPrefix) { + length += self.foundPrefixRange.length; + } + + NSRange range = NSMakeRange(location, length); + NSRange insertionRange = [self.textView slk_insertAttributedText:attributedString inRange:range]; + + self.textView.selectedRange = NSMakeRange(insertionRange.location, 0); + + [self.textView slk_scrollToCaretPositonAnimated:NO]; + + [self cancelAutoCompletion]; +} + - (void)cancelAutoCompletion { [self slk_invalidateAutoCompletion];