Better late than never I suppose:
It's possible that you are setting the color for the entire containing run.
What you need to do is:
- remove the overflowing text from the current run
- add the overflowing text in the adjacent runs
- Color the current run
Here is an example
private void highlightSubsentence(String currentRunText, String sentence, CharacterRun currentRun, int runIndex, Paragraph p, HighlighterColor color) {
int sentenceBeginIndex = currentRunText.indexOf(sentence, 0);
int sentenceLength = sentence.length();
String leftSentence = currentRunText.substring(0, sentenceBeginIndex);
String rightSentence = currentRunText.substring(sentenceBeginIndex + sentenceLength);
boolean leftOverflow = sentenceBeginIndex > 0;
boolean rightOverflow = sentenceBeginIndex + sentenceLength < currentRunText.length();
boolean isInTable = p.isInTable();
if (rightOverflow && !isInTable && runIndex + 1 < p.numCharacterRuns()) {
currentRun.replaceText(sentence + rightSentence, sentence);
p.getCharacterRun(runIndex + 1).insertBefore(rightSentence);
}
if (leftOverflow && !isInTable && runIndex > 0) {
currentRun.replaceText(leftSentence + sentence, sentence);
p.getCharacterRun(runIndex - 1).insertAfter(leftSentence);
}
currentRun.setColor(6);
}