This is a migrated thread and some comments may be shown as answers.

Setting Default Font and Font Size Programatically

5 Answers 962 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Anthony
Top achievements
Rank 1
Anthony asked on 28 Jul 2011, 02:02 PM
Hi

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/Fonts.zip#Bliss Regular">
               <telerik:RadRichTextBox.Document>
                   <telerik:RadDocument LayoutMode="Flow" PageViewMargin="0,0,0,0">
                       <telerik:Section PageMargin="0,0,0,0,">
                             
                       </telerik:Section>
                   </telerik:RadDocument>
               </telerik:RadRichTextBox.Document>
           </telerik:RadRichTextBox>


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.

Thanks

Anthony

5 Answers, 1 is accepted

Sort by
0
Kammen
Telerik team
answered on 02 Aug 2011, 04:02 PM
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,
Kammen
the Telerik team

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

0
Pablo
Top achievements
Rank 1
answered on 09 May 2013, 02:18 PM
How can I set the font size programatically just to the span elements which are beetween InsertRanges
0
Petya
Telerik team
answered on 10 May 2013, 08:51 AM
Hello Pablo,

You can select an annotation range like this:
this.radRichTextBox.Document.Selection.SelectAnnotationRange(rangeStart);
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.
 
Regards,
Petya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Mandar
Top achievements
Rank 1
answered on 05 Mar 2018, 02:49 PM
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.
0
Tanya
Telerik team
answered on 08 Mar 2018, 01:32 PM
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. 

Regards,
Tanya
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.
Tags
RichTextBox
Asked by
Anthony
Top achievements
Rank 1
Answers by
Kammen
Telerik team
Pablo
Top achievements
Rank 1
Petya
Telerik team
Mandar
Top achievements
Rank 1
Tanya
Telerik team
Share this question
or