Skip to content

Commit c7e4f72

Browse files
committed
fix(android): extend highlight background to cover spaces between words
1 parent 0be1c99 commit c7e4f72

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

android/src/main/java/com/advancedtext/AdvancedTextView.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,20 @@ class AdvancedTextView : TextView {
177177

178178
val positions = mutableListOf<WordPosition>()
179179
val regex = "\\S+".toRegex()
180-
181-
regex.findAll(text).forEachIndexed { index, match ->
182-
positions.add(WordPosition(
183-
index = index,
184-
start = match.range.first,
185-
end = match.range.last + 1,
186-
word = match.value
187-
))
180+
val matches = regex.findAll(text).toList()
181+
182+
matches.forEachIndexed { index, match ->
183+
val wordEnd = match.range.last + 1
184+
val extendedEnd = if (index + 1 < matches.size) matches[index + 1].range.first else text.length
185+
positions.add(
186+
WordPosition(
187+
index = index,
188+
start = match.range.first,
189+
end = wordEnd,
190+
extendedEnd = extendedEnd,
191+
word = match.value
192+
)
193+
)
188194
}
189195

190196
wordPositions = positions
@@ -205,7 +211,7 @@ class AdvancedTextView : TextView {
205211
spannableString.setSpan(
206212
BackgroundColorSpan(color),
207213
wordPos.start,
208-
wordPos.end,
214+
wordPos.extendedEnd,
209215
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
210216
)
211217
}
@@ -219,7 +225,6 @@ class AdvancedTextView : TextView {
219225
)
220226
}
221227

222-
// Add clickable span for word clicks
223228
spannableString.setSpan(
224229
WordClickableSpan(wordPos.index, wordPos.word),
225230
wordPos.start,
@@ -369,6 +374,7 @@ class AdvancedTextView : TextView {
369374
val index: Int,
370375
val start: Int,
371376
val end: Int,
377+
val extendedEnd: Int,
372378
val word: String
373379
)
374380
}

0 commit comments

Comments
 (0)