Setting Default Font and Font Size Programatically

4 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
    313 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
    983 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.

Back to Top