fix collissions in uitextview (#3017)
This commit is contained in:
parent
d679ae7dbb
commit
5cb45f9c16
1 changed files with 16 additions and 3 deletions
|
@ -108,14 +108,27 @@ class RNUITextView: UIView {
|
||||||
fractionOfDistanceBetweenInsertionPoints: nil
|
fractionOfDistanceBetweenInsertionPoints: nil
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var lastUpperOffset: Int = 0
|
||||||
for child in self.reactSubviews() {
|
for child in self.reactSubviews() {
|
||||||
if let child = child as? RNUITextViewChild, let childText = child.text {
|
if let child = child as? RNUITextViewChild, let childText = child.text {
|
||||||
let fullText = self.textView.attributedText.string
|
let fullText = self.textView.attributedText.string
|
||||||
let range = fullText.range(of: childText)
|
|
||||||
|
// We want to skip over the children we have already checked, otherwise we could run into
|
||||||
|
// collisions of similar strings (i.e. links that get shortened to the same hostname but
|
||||||
|
// different paths)
|
||||||
|
let startIndex = fullText.index(fullText.startIndex, offsetBy: lastUpperOffset)
|
||||||
|
let range = fullText.range(of: childText, options: [], range: startIndex..<fullText.endIndex)
|
||||||
|
|
||||||
if let lowerBound = range?.lowerBound, let upperBound = range?.upperBound {
|
if let lowerBound = range?.lowerBound, let upperBound = range?.upperBound {
|
||||||
if charIndex >= lowerBound.utf16Offset(in: fullText) && charIndex <= upperBound.utf16Offset(in: fullText) {
|
let lowerOffset = lowerBound.utf16Offset(in: fullText)
|
||||||
|
let upperOffset = upperBound.utf16Offset(in: fullText)
|
||||||
|
|
||||||
|
if charIndex >= lowerOffset,
|
||||||
|
charIndex <= upperOffset
|
||||||
|
{
|
||||||
return child
|
return child
|
||||||
|
} else {
|
||||||
|
lastUpperOffset = upperOffset
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue