Comments (5)
This is my solution at the moment.
internal extension UITextView {
func find(prefixes: Set<String>, with delimiterSet: CharacterSet) -> (prefix: String, word: String, range: NSRange)? {
guard prefixes.count > 0
else { return nil }
return prefixes.compactMap({
guard let prefix = $0.first else { return nil }
return find(prefix: prefix, with: delimiterSet)
}).last
}
func find(prefix: Character, with delimiterSet: CharacterSet) -> (prefix: String, word: String, range: NSRange)? {
guard let caretRange = self.caretRange,
let cursorRange = Range(caretRange, in: text) else { return nil }
var substring = text[..<cursorRange.upperBound]
guard let prefixIndex = substring.lastIndex(of: prefix) else { return nil }
let wordRange: Range = prefixIndex..<cursorRange.upperBound
substring = substring[wordRange]
let location = wordRange.lowerBound.encodedOffset
let length = wordRange.upperBound.encodedOffset - location
let range = NSRange(location: location, length: length)
return (String(prefix), String(substring), range)
}
func wordAtCaret(with delimiterSet: CharacterSet) -> (word: String, range: NSRange)? {
guard let caretRange = self.caretRange,
let result = text.word(at: caretRange, with: delimiterSet)
else { return nil }
let location = result.range.lowerBound.encodedOffset
let range = NSRange(location: location, length: result.range.upperBound.encodedOffset - location)
return (result.word, range)
}
var caretRange: NSRange? {
guard let selectedRange = self.selectedTextRange else { return nil }
return NSRange(
location: offset(from: beginningOfDocument, to: selectedRange.start),
length: offset(from: selectedRange.start, to: selectedRange.end)
)
}
}
After more testing, I may create a PR for it.
from inputbaraccessoryview.
@jameshays Thanks for helping out with this! Tbh I was in a crunch when I needed this for a project of mine and will be the first to admit it was not robust enough for all cases, but it worked for what I needed
I would accept PRs
from inputbaraccessoryview.
In the autocomleteManager, I have this as well.
public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
// Ensure that the text to be inserted is not using previous attributes
preserveTypingAttributes()
if let session = currentSession {
let textToReplace = (textView.text as NSString).substring(with: range)
let deleteSpaceCount = textToReplace.filter { $0 == .space }.count
let insertSpaceCount = text.filter { $0 == .space }.count
let spaceCountDiff = insertSpaceCount - deleteSpaceCount
session.spaceCounter = spaceCountDiff
}
...
session.spaceCount = spaceCountDiff. it doesn't seem to always be accurate when deleting spaces and I haven't worked through that yet. It seems to be working for now though. More testing is probably warranted.
from inputbaraccessoryview.
I created a PR here. #50
Give it a go and let me know what you find.
from inputbaraccessoryview.
Fixed in 4.2.1
from inputbaraccessoryview.
Related Issues (20)
- Layout issues on iPad when using pagesheets HOT 30
- SwiftUI Example Text Field too high, BottomStack too big HOT 2
- The height of input is changed even have set `maxTextViewHeight`
- Keyboard appearance issue!
- AutoCompleteManager Index out of bounds when using custom prefix
- KeyboardManager conflicts with system keyboard movement: How to resolve?
- Unable to build framework when including this via SPM HOT 4
- Xcode 14: custom inputAccessoryView appears prematurely, causing broken push animation HOT 3
- Update library with main and create tag
- The mentionTextAttributes is used for pasting new text.
- iOS 17 issue with selection HOT 1
- InputBarAccessoryView & inputTextView disappeared after searchbar has focused HOT 1
- Changing access modifier of requiredInputTextViewHeight from public to open๏ผ HOT 5
- scrollIndicatorInsets setter Recursion causes crashes HOT 1
- Privacy manifest HOT 2
- About paste monitoring HOT 3
- Why AttachmentManager extensions methods are 'finals'? HOT 2
- How inputTextView parses custom emoticons?
- topStackView Stretched When Using setShouldForceMaxTextViewHeight HOT 1
- Varying height depending on additional content constraints?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from inputbaraccessoryview.