Skip to content

Commit

Permalink
Always insert text when calling insertText(_:) programmatically (#87)
Browse files Browse the repository at this point in the history
* Always insert text when calling `insertText(_:)` programmatically

* Remove trailing white space

* Removes alwaysInsert

Co-authored-by: Simon Støvring <mail@simonbs.dk>
  • Loading branch information
nighthawk and simonbs authored Jun 13, 2022
1 parent 5e6f666 commit 6133cae
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Sources/Runestone/TextView/TextInput/TextInputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,9 @@ extension TextInputView {
extension TextInputView {
func insertText(_ text: String) {
let preparedText = prepareTextForInsertion(text)
guard let selectedRange = markedRange ?? selectedRange, shouldChangeText(in: selectedRange, replacementText: preparedText) else {
// If there is no marked range or selected range then we fallback to appending text to the end of our string.
let selectedRange = markedRange ?? selectedRange ?? NSRange(location: stringView.string.length, length: 0)
guard shouldChangeText(in: selectedRange, replacementText: preparedText) else {
return
}
// If we're inserting text then we can't have a marked range. However, UITextInput doesn't always clear the marked range
Expand Down
4 changes: 2 additions & 2 deletions Sources/Runestone/TextView/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,8 @@ public final class TextView: UIScrollView {
textInputView.setLanguageMode(languageMode, completion: completion)
}

/// Inserts text at the location of the caret.
/// - Parameter text: A text to insert.
/// Inserts text at the location of the caret or, if no selection or caret is present, at the end of the text.
/// - Parameter text: A string to insert.
public func insertText(_ text: String) {
textInputView.insertText(text)
// Called in TextView since we only want to force the text selection view to update when editing text programmatically.
Expand Down

0 comments on commit 6133cae

Please sign in to comment.