Change relative text size of all characters in current selection

I have an application that needs to change the relative size of each character in the SfRichTextBoxAdv control's currently selected text. For example, say the selection is "abc" where font sizes are a=10, b=20, c=30; I'd want to increase each by 10% so that I'd end up with font sizes of a=11, b=22, c=33. I tried using "rtfControl.Selection.CharacterFormat.Size *= 1.1" but this doesn't work in such cases as where the current selection isn't identical formatting/size/etc (presumably because the Selection would contain multiple Inline objects in this case).

Is there any way to perform a resize action on such a selection?



3 Replies 1 reply marked as answer

KG Kalaivannan Ganesan Syncfusion Team October 29, 2024 11:14 AM UTC

Rochelle, we can increase the font size of the selected text in two ways:

  1. Using the IncreaseFontSizeCommand.
  2. Iterating through the inlines (SpanAdv) within the selection and increasing the font size.

Using the Increase Font Size Command:
This command is used to increase the font size of the text within the selection. We need to pass the parameter values as doubles so that the existing values will be incremented by the passed values.

However, it cannot be used to achieve a percentage increase in font size. Below is the code snippet to use the command.

SfRichTextBoxAdv.IncreaseFontSizeCommand.Execute(1.1, richTextBoxAdv);



Iterating through the inlines (SpanAdv) within the selection and increasing the font size:
In this scenario, we iterate through the inlines that lie within the selected range and multiply the font size by 1.1 to increase it by 10% of the existing size.

We have also created a sample application that demonstrates the above two methods. Based on your requirements, please use them.


Attachment: SfRichTextBoxAdv_FontSize_Increase_42af1014.zip

Marked as answer

RB Rochelle Boggs October 29, 2024 04:55 PM UTC

Thanks for the quick help, that's exactly what I needed! I will put this one change here for anyone who finds this later & wants to use the sample: the increaseFontSizebypercentage_Click method needs this block inserted just before the "// Check if the end index is within the length of the text" line. Otherwise the size changes affect the entire Inline that the selection start falls in, even if the selection starts in the middle of the Inline's text.


// Check if the start index is in the middle of a span
if (startIndex > 0 && startInline is SpanAdv startSpan)
{
// Create a new span containing the text prior to the split point
SpanAdv splitSpan = new() { Text = startSpan.Text[..startIndex] };
CopyFormat(startSpan.CharacterFormat, splitSpan.CharacterFormat);


// Modify the existing span to contain only text after the split point
startSpan.Text = startSpan.Text[startIndex..];


// Insert the new span immediately before the modified span in the paragraph
if (startSpan.Owner is ParagraphAdv paragraph)
{
paragraph.Inlines.Insert(paragraph.Inlines.IndexOf(startSpan), splitSpan);
}


// Refresh endInline to updated inline elements (startInline is still correct)
endInline = GetInline(endOffset, ref endIndex, richTextBoxAdv.Selection.End.Paragraph);
}


KG Kalaivannan Ganesan Syncfusion Team October 30, 2024 04:58 AM UTC

Rochelle, Thank you for the update! Please feel free to reach out to us if you need any further assistance.


Loader.
Up arrow icon