Setting Default Font and Font Size Programatically

6 posts, 0 answers
  1. Anthony
    Anthony avatar
    26 posts
    Member since:
    Mar 2009

    Posted 28 Jul 2011 Link to this post


    I have a RichTextBox on my page defined as follows

    <telerik:RadRichTextBox x:Name="txtCurrentItem" FontSize="10" HorizontalContentAlignment="Center" IsReadOnly="True" Background="Transparent" IsSpellCheckingEnabled="False" FontFamily="/iNAV-BMS;component/Fonts/ Regular">
                       <telerik:RadDocument LayoutMode="Flow" PageViewMargin="0,0,0,0">
                           <telerik:Section PageMargin="0,0,0,0,">

    I then have a function :
    Public Function ImportToHTML(ByVal content As String) As RadDocument
            Dim provider As New HtmlFormatProvider
            Return provider.Import(content)
        End Function

    This is then set using the following:
    txtCurrentItem.Document = ImportToHTML(HttpUtility.HtmlDecode(NodeDetail.NodeContent))

    This is fine and works however the html being converted does not explicitly define the font family and size so my content is displayed using the Times New Roman size 12. 

    How can i set the default size and font family if not explicitly specified.


  2. Kammen
    Kammen avatar
    377 posts

    Posted 02 Aug 2011 Link to this post

    Hi Anthony,

    You can achieve this kind of behavior by setting ImportSettings property of HtmlFormatProvider to new HtmlImportSettings with UseDefaultStylesheetForFontProperties property set to true. Then set the DocumentInheritsDefaultStyleSettings property of your RadRichTextBox to true and change the desired Font properties ( FontSize, FontFamily, etc...).

    Here is a code snippet of how to do that.

    HtmlFormatProvider provider =
    new HtmlFormatProvider();
    provider.ImportSettings = new HtmlImportSettings();
    provider.ImportSettings.UseDefaultStylesheetForFontProperties = true;
    this.txtCurrentItem.DocumentInheritsDefaultStyleSettings = true;
    this.txtCurrentItem.FontFamily = new FontFamily("Trebuchet MS");
    this.txtCurrentItem.FontSize = 20;
    this.txtCurrentItem.Document = ImportHTML();

    Kind regards,
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  3. Pablo
    Pablo avatar
    7 posts
    Member since:
    May 2013

    Posted 09 May 2013 Link to this post

    How can I set the font size programatically just to the span elements which are beetween InsertRanges
  4. Petya
    Petya avatar
    989 posts

    Posted 10 May 2013 Link to this post

    Hello Pablo,

    You can select an annotation range like this:
    Where rangeStart is the AnnotationRangeStart of the respective annotation. Then, you can apply formatting to the current selection using the methods of RadRichTextBox:
    this.radRichTextBox.ChangeTextHighlightColor(Colors.LightGray);  // will highlight all selected words in LightGray
    this.radRichTextBox.ChangeFontSize(Unit.PointToDip(32));   // will increase the font size of the words to 30 DIP
    this.radRichTextBox.ChangeFontFamily(new FontFamily("Comic Sans MS")); // will change the font family of the spans, containing these words.

    I hope this helps! Let us know if you have other questions.
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  5. Mandar
    Mandar avatar
    2 posts
    Member since:
    Nov 2012

    Posted 05 Mar 2018 in reply to Kammen Link to this post

    I am using 2018 controls for WinForms and the example code does not work.  The only thing that does work is explicitly selecting the document using selection and then calling ChangeFontFamily and ChangeFontSize API.  This method works but consumes lot of memory especially if the document is large.
  6. Tanya
    Tanya avatar
    853 posts

    Posted 08 Mar 2018 Link to this post

    Hello Mandar,

    The Change~() methods of the RadRichTextEditor class work only with the current selection and, if you mean the example code from the last reply, you might observe similar behavior when there is no selection. In case you are experiencing issues using them or they don't fit your requirements, I would like to ask you to submit a support ticket or open a new thread in the forum for UI for WinForms and send more information about the case so you can get a better assistance. 

    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top