To get the next text entered as a different font, and not just the selected text, you need to add a run block in RTB and then write to it. I applied a toolbar for RTB that does something like this:
public static void SetFontSize(RichTextBox target, double value)
{
if (target == null)
return;
if (target.Selection != null)
{
if (target.Selection.IsEmpty)
{
if (target.Selection.Start.Paragraph == null)
{
Paragraph p = new Paragraph();
p.FontSize = value;
target.Document.Blocks.Add(p);
}
else
{
TextPointer curCaret = target.CaretPosition;
Block curBlock = target.Document.Blocks.Where
(x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();
if (curBlock != null)
{
Paragraph curParagraph = curBlock as Paragraph;
Run newRun = new Run();
newRun.FontSize = value;
curParagraph.Inlines.Add(newRun);
target.CaretPosition = newRun.ElementStart;
}
}
}
else
{
TextRange selectionTextRange = new TextRange(target.Selection.Start, target.Selection.End);
selectionTextRange.ApplyPropertyValue(TextElement.FontSizeProperty, value);
}
}
target.Focus();
}
source
share