Monday, February 01, 2010 #

Changing the FontSize in a RichTextArea

It's nice if a user can set the font size of all text displayed in an application, especially if this user is me and I am giving a demo - the font size that works for me (12.0) at my desk is not necessarily ideal during a presentation. So I have a FontSize property in my ViewModel that all controls observe. For RichTextArea controls that simply display text authored previously, changing the FontSize has no effect, since the FontSize of the text displayed is a property of the text itself, not of the control.

The excellent introduction to the RichTextArea by John Papa taught me a bit on the data structure this control uses to store the text. And so I could let my RichTextArea control react to any change in the ViewModel property by executing:

        public void IncreaseFontSize(double factor)
        {
            var res = from block in StoryTextArea.Blocks
                      from inline in (block as Paragraph).Inlines
                      select inline;
            Array.ForEach(res.ToArray(), (i) =>         // i stands for inline
            {
                i.FontSize = factor * i.FontSize;
                Hyperlink hl = i as Hyperlink;
                if (hl != null)
                {
                    Array.ForEach(hl.Inlines.ToArray(), iHL => iHL.FontSize = factor * iHL.FontSize);
                }
            });
        }

It's fast enough (for the typical texts I have) to adjust the font fluently. Cool!

Posted On Monday, February 01, 2010 9:21 AM | Feedback (0)

Copyright © Marc Schluper

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski