I am trying to achieve highlighting a wrapped text in the given bounds. I can achieve the expected behavior. If the search text is in single line and i am facing issue if the search text is rendered in two lines (wrapped) like below,
I have used character range and MeasureCharacterRanges method to find the regions of the text area and exlcuded the text regions that doesn't match the given search text, but i couldn't find the region of the empty area that doesn't have text rendered, so empty area also highlighted along with given search text.
StringFormat stringFormat = new StringFormat(StringFormat.GenericDefault);
Font font = new Font("Segoe UI", 9F, FontStyle.Regular);
var searchText = string.IsNullOrEmpty(SearchText) ? string.Empty : SearchText;
int matchIndex = Text.IndexOf(searchText);
if (matchIndex != -1 && searchText.Length > 0)
{
CharacterRange[] characterRange = new CharacterRange[]
{
new CharacterRange(0, matchIndex),
new CharacterRange(matchIndex, searchText.Length),
new CharacterRange(matchIndex + searchText.Length , Text.Length - matchIndex - searchText.Length),
};
//Set the range of characters to be measured.
stringFormat.SetMeasurableCharacterRanges(characterRange);
//Gets the regions of the measurable characters applied to the string format for the given text with in the text bounds.
Region[] regions = e.Graphics.MeasureCharacterRanges(Text, font, e.ClipRectangle, stringFormat);
var clipBounds = e.Graphics.ClipBounds;
for (int i = 0; i < regions.Length; i++)
{
RectangleF bound = regions[i].GetBounds(e.Graphics);
e.Graphics.SetClip(Rectangle.Ceiling(bound));
for (int j = 0; j < regions.Length; j++)
{
if (j != i)
e.Graphics.ExcludeClip(regions[j]);
}
Rectangle rbound = new Rectangle((int)bound.X, (int)bound.Y, (int)bound.Width, (int)bound.Height);
Brush brush = new SolidBrush(Color.Yellow);
if (i == 1)
e.Graphics.FillRectangle(brush, rbound);
}
e.Graphics.SetClip(clipBounds);
}
// Paints the text.
e.Graphics.DrawString(Text, font, Brushes.Black, e.ClipRectangle);
Above is the code used to highlight the search text.
I couldn't find a way to exclude the empty region with MeasureCharacterRanges concept. Can anyone share idea to exclude the remaining empty region?