Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

add helper method so autocomplete can replace with attributed strings #657

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion Source/SLKTextViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
///------------------------------------------------
Expand Down
26 changes: 26 additions & 0 deletions Source/SLKTextViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down