I'm currently working on an UWP app which also deals with math. Therefore i parse formulae and display them as inlines in a RichTextBlock. As you can imagine, I also want to display sub- and superscription - but I haven't yet found a way to do so. I know, there is something called BaselineAlignment in WPF, but it seems that this is not supported in UWP.
I'm currently also about to figure out how to best display fraction lines and roots. I've thought about building my own control for this purpose, but if somebody knows a better approach it would be nice to know.
I'm gladful for any answer.
To apply subscript and superscript to text in RichTextBlock, we can take advantage of Typography.Variants attached property. For example:
<RichTextBlock FontSize="36">
<Paragraph FontFamily="Palatino Linotype">
2<Run Typography.Variants="Superscript">3</Run>
14<Run Typography.Variants="Superscript">th</Run>
</Paragraph>
<Paragraph FontFamily="Palatino Linotype">
H<Run Typography.Variants="Subscript">2</Run>O
Footnote<Run Typography.Variants="Subscript">4</Run>
</Paragraph>
</RichTextBlock>
And the output would like:
You could use a WebBrowser control, and just locally include one of many javascript libraries for rendering LaTex.
Related
I want to create a simple editor like Notepad++ with simple functionality... I need to color a specific word in the rich text box area. How can I do that?
For example: when the user write these word, I want to color them to the blue color. These words are: for, while, if, try, etc.
How can I make the richtextbox to select a specific word and then color them?
And, if I want to make a comment and color everything after the //, how is that done in the richtextbox?
How do I number the line in the text box, so I can now the line number when I'm coding in my editor?
Here's some code you can build on in order to achieve the functionality you want.
private void ColourRrbText(RichTextBox rtb)
{
Regex regExp = new Regex("\b(For|Next|If|Then)\b");
foreach (Match match in regExp.Matches(rtb.Text))
{
rtb.Select(match.Index, match.Length);
rtb.SelectionColor = Color.Blue;
}
}
The CodeProject article Enabling syntax highlighting in a RichTextBox shows how to use RegEx in a RichTextBox to perform syntax highlighting. Specifically, look at the SyntaxRichtTextBox.cs for the implementation.
In general, you have to work on the selection in RichTextBox. You can manipulate the current selection using the Find method or using SelectionStart and SelectionLength properties. Then you can change properties of selected text using SelectionXXX properties. For example, SelectionColor would set the color of current selection, etc. So you have to parse text in richtextbox and then select part of texts and change their properties as per your requirements.
Writing a good text editor using RichTextBox can be quite cumbersome. You should use some library such as Scintilla for that. Have a look at ScintillaNet, a .NET wrapper over Scintilla.
Did you know that Notepad++ uses Scintilla?
You actually do not have to reinvent the wheel by going through all the trouble as there is a .NET port of Scintilla named ScintillaNET which you can freely embed in your application as the source code editor :)
But to answer your question, there are few parts that you need to understand
Finding what to color
When to color
How to color
For the first part, there may be different approaches, but I think using regular expressions would be a good choice. I am sorry, but I don't know regular expressions much so I cannot help you in that case.
When to color is very crucial and if you do it wrong, your application will suffer a heavy performance penalty. I suggest you refer to XPath Visualizer which was done by our own Stack Overflow member, Cheeso. Take a look at the source on how the coloring of the syntax was done. But if you ScintillaNET, everything would be taken care of. Anyway, I really can't seem to find this documentation where he clearly showed how the coloring of the text was done. I would most definitely post it here if I find it.
The third question I think is covered by VinayC. But basically you use SelectionColor along with SelectionStart.
here is a good link on c-sharpcorner.com website on basic richtextbox syntax highlighting. I assume that you and any one visiting this page for similar problem want to do it for learning purpose. But if any one wants to do it for some making some comercial level IDE then it must use scintilla or some similar.
An other approach for this is to directly change the RTF of the richtextbox. Look in codeproject.com there are lot of articles similar to this problem.
I had some problems with that and here is my solution, beats me why it has to be done like this, but it works:
// position on end of control...
richTextBox.UpdateLayout();
richTextBox.ScrollToEnd();
richTextBox.UpdateLayout();
// ...then select text and it will be position on top of control.
richTextBox.Focus();
richTextBox.Selection.Select(foundRange.Start, foundRange.End);
richTextBox.BringIntoView();
Vb.net implementation
Imports System.Text.RegularExpressions
Private Sub formatString()
Dim rg =New Regex("\b(for|while|if|try)\b")
Dim m As Match
For Each m In rg.Matches(RichTextBox1.Text)
RichTextBox1.Select(m.Index,m.Length)
RichTextBox1.SelectionColor=Color.Green
Next
End Sub
I am new to XAML / Xamarin.forms and come from a web-development background.
I am looking to make a box containing with text and images (like that can be done using a <div> in HTML). Please explain how to approach this with XAML & Xamarin.forms.
Also, if anybody knows a resource that lists the available tags in XAML (like w3schools for HTML), please pass that on to me.
Thanks in advance :)
Why not check out the Xamarin documentation on this? It is quite extensive and should help you with the basics!
There isn't a exact equivalent of a div. It depends on what you want to do. It all starts with a Page. You see the types of pages below.
That will be the base of your app. Within this Page you can have one VisualElement, which kind of forces you to use a Layout.
Here you see all the layouts that are available to you;
The good news is you can nest Layouts!
In a Layout you can specify your controls (of type View).
To get back to your question; the behaviour is different than a div. If you take a StackLayout for example your nested controls will be stacked on top of each other and you don't have to worry about spacing, margin, etc. Another option is to go for a Grid which gives you a bit of flexibility.
So it's not just a container it also means something in terms of how it is placed on the screen.
I also explain some basics on my blog here and here but there are a lot of resources out there which can help you get started.
Just use a Frame:
<StackLayout>
<Frame x:Name="MyPanelABC">
<Label Text="My Stuff" />
</Frame>
</StackLayout>
private void setupPanelABC(bool isVisiblePanelABC){
MyPanelABC.IsVisible = isVisiblePanelABC;
}
I'm using AvalonEdit for an editing control and I wanted to customize the word wrapping functionality. Right now by default, word wrapping line breaks are detected for spaces. I wanted it to do wrapping based on commas as well.
Example:
Hello there,testing
If the width of the control isn't long enough, this would wrap to be
Hello
there,testing
However, if the control width is even smaller, the above example would wrap to
Hello
there,test
ing
I'm trying to avoid this situation by having it handle the comma as well.
I looked at AvalonEdit and it uses the standard System.Windows.Media.TextFormatting.TextFormatter class. The TextFormatter has TextParagraphProperties which contains TextWrapping. It doesn't seem like Microsoft is allowing us to customize TextWrapping.
Any ideas?
Have you used the following?
TextWrapping="Wrap" AcceptsReturn="True"
I feel like I'm going to reinvent wheel so I would like to know if WPF has bult-in support for what I'm trying to achieve. I'm building an app that will allow people to enter some text in a textbox, and then see it formatted in a textblock.
I would like that the user be able to format the text himself by inputing things such as
This [BusinessSpecificStyle] is [/BusinessSpecificStyle] a sample text
My purpose is to be able to easily change the presentation of all my documents by simply changing the underlaying rules in BusinessSpecificStyle. However I don't know what is the best way to implement that with WPF. I was thinking of using a BBCode parser like this one but supposing I go that way, I don't see how I will be able to convert the resulting XAML into TextBlock children programatically, and I seriously wonder if there isn't some kind of built in support for that.
Thanks for your help
IValueConverter is what you are looking for.
Create the converter and format your text based on the bindings passed from the XAML.
You can get multiple samples over the net for creating IValueConverter. Refer to the link here and here to get you started.
Not sure if you are asking for Converter here. To me it reads that you want to control the style of a block of text depending on some background and common style?
If that is the case, you want to set the inlines of your text block to seperate your text into run elements, which can reference a specific style resource.
<TextBlock>
<TextBlock.Inlines>
<Run>This</Run>
<Run Foreground="{StaticResource BusinessSpecificStyleForeground}">is</Run>
<Run>a sample text</Run>
...
in this case, you create a resource which defines the binding styles for run or bind the Style in it's entirety.
Apologies if I am making up a new question, I see you've marked an answer but wanted to add this just in case.
I need to store significant amounts of rich text in a SQL database, retrieve it and display it.
One font throughout is OK but I need different font sizes/bold/colors.
For now I am using a RichTextBox (WPF) to display it, and XamlWriter.Save/XamlReader.Parse to serialize it to strings to store in the DB. It works well but the RichTextBox is so HUGELY SLOW at displaying the text that it's basically unusable.
Is there a quick way to do this with acceptable performance?
I'm considering doing it with GlyphRun objects, drawing each character as a bitmap and computing all the alignment requirements to fit the destination image etc... But reinventing the wheel on simple colored/sizable text seems really strange in 2011.
EDIT:
Thanks for the answers, didn't see them until now, sorry.
Text is entered by the user from RichTextBoxes as well, basically I just save the resulting string XamlWriter.Save(richTextBox.Document) in the database. Other fields (double/int etc) are also entered by the user from TextBoxes.
As the user queries the database, pages of read-only rich text with colors and formatting is generated from scratch using the fields in the database, including the saved rich text fields above: these are converted from FlowDocuments to Spans and some replacement is done on them (InlineUIContainers which host a class derived from UIElement which references a database entry, inlined in the text, like "see [thisbook]" where [thisbook] references some database entry's ID). MSDN says all that is far too much text for a TextBlock.
That text rendering is the really slow part but there is no way around it, I need that formatting and it's just how the WPF RichTextBoxes are: even when entering a little simple text in the RichTextBoxes, there is a delay between typing and the character appearing on the screen...
For now I still use RichTextBoxes but I keep lots of rendered layouts in memory (the Paragraph/Section/Span objects) and I am careful to rerender only the least amount of formatted text possible when changes/queries are made or different views of the database data are requested by the user.
It's still not fast but it's OK, changing the whole structure (AvalonEdit or FormattedText or GlyphRun) doesn't seem worth it right now, too much work, the whole serialization API with XamlWriter.Save and XamlReader.Parse simplifies much (for FormattedText and GlyphRun, I'd have to come up with a file format myself to save the formatted text to the database).
There is also the possibility of using the OpenXML SDK to create Microsoft Word .docx documents but google says rendering performance isn't great either, and I don't know if embedding an UIElement in the text within an InlineUIContainer and serializing that to be saved in the database would be possible (same problem with AvalonEdit).
Consider throwing away RichTextBox because it is so HUGELY SLOW (spot on). Instead of writing your own text editor check AvalonEdit. Performance wise it beats RichTextBox like a baby.
Or if you need read-only text you could try a TextBlock - it supports simple formatting:
<TextBlock>
<Run FontWeight="Bold">Hello</Run>
<Run Foreground="Green">World</Run>
<Run FontSize="24">!</Run>
</TextBlock>