RichEditBox : set a style only for the characters that are about to be written - xaml

I'm trying to create a simple text editor in a Windows 8 application, like a simple OneNote. I can set font size, font color and font style, but the problem is that it's applied to the whole content of my RichEditBox, even to the text that has already been written. But I would like that, when I select "Italic" for exemple, only the future characters should be written in Italic, and the words and characters already written remain in the style they were.
It's quite difficult to describe.
Do I use the good XAML element (RichEditBox) ? Is there a method to select the actual position of the writer in the RichEditBox ?
Thanks for your answers !

Try this button event. rebTextNote is RichEditBox object.
using Windows.UI.Text;
public void btnItalic_Click(object sender, RoutedEventArgs e)
{
ITextCharacterFormat format = rebTextNote.Document.Selection.CharacterFormat;
if (format.Italic != FormatEffect.On)
format.Italic = FormatEffect.On;
else
format.Italic = FormatEffect.Off;
}

Related

apply text formatting from string to text for FlowDocument in richtextbox

I know that when adding text/content/DataContext in XAML you refer to resource dictionary or inline mark up for styling around text or in template.
Q:
However I'm having trouble trying to find a way to do the following:
Data is coming from a View Model/Model that is pulled from a database.
(string value)
I am a <Bold>smart</Bold> man.
to show in a flow document like this:
I am a smart man.
Q end
Either by binding to a converter, behavior, or would saving the paragraph/document that I put in the flow document to a .rtf file in memory stream be a better option?
I've tried to utilize the option for behavior listed > here < but that is for text block and unable to redirect for type text instead of text block.
Trying to make it streamlined.
Tried to use data binding and apply the converter but even though I have the resource for the behavior / converter, it work due to the type conversion.
One clever solution is presented by Rockford Lhotka in post Set rich text into RichTextBlock control. His idea is to create a custom control which then creates the RichTextBlock using XamlReader.Load.
This allows you to use code like the following:
<local:RichTextDisplay Xaml="{Binding Hello}" HorizontalAlignment="Center"
VerticalAlignment="Center"/>
Where Hello is:
public string Hello { get; set; } = "I am a <Bold>smart</Bold> man.";
With a result:
If you use UWP/Win 8.1 XAML, you can use the original code from the blog post with the following small change (Paragraphs added):
<UserControl
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006"">
<Grid>
<RichTextBlock><Paragraph>");
xaml.Append(ctl.Xaml);
xaml.Append(#"
</Paragraph></RichTextBlock>
</Grid>
</UserControl>
");
To answer my own question:
My case was creating a Document style display for user to update and save as a PDF, but I didn't want to rely on Office being on our application Server.
So I resolved this in my case by using a full "doc.RTF" document and importing that as a memory stream/string and apply my needed updates for values to that.
i.e. VB.net snippet example
Using uStream = Assembly.GetExecutingAssembly.GetManifestResourceStream("Resourcefilepath.rtf")
Using mStream As system.IO.MemoeryStream = New MemoryStream()
uStream.CopyTo(mStream)
rtfstring = Encoding.UTF8.GetSTring(mStream.toArray())
'--Do the updates to the needed string as needed:
rtfstring.Replace("Value","UpdatedValue")
'--Load Property Memory String this method is returnind
RTFDataProperty = New MemoryStream(Encoding.UTF8.GetBytes(rtfstring))
End Using
End Using
Then I loaded my XAML Rich Text Box with that memory stream as DataFormats.Rtf.
RichTextBox1.SelectAll()
RichTextBox1.Selection.Load(ClassName.RTFDataProperty, DataFormats.Rtf)
This gave me a template for formatting and layout of that document. (More of a case scenario and not a normal practice)
I also wanted to apply a starting selection so here is what I did there:
'--Get my RichTextBox Text
rtbtext As String = New TextRange(RichTextBox1.Document.contentStart, RichTextbox1.Document.ContentEnd).Text
Dim strStartSelection As String = "Comments..."
Dim startTP As TextPointer
Dim endTP As TextPointer
'--Loop through the paragraphs of the richtextbox for my needed selection starting point:
For Each para As Paragraph In RichTextBox1.Document.Blocks
Dim paraText As String = New TextRange(para.ContentStart, para.ContentEnd).Text
If paraText = "" Then
Dim pos As TextPointer = para.ContentStart
startTP = pos
endTP = startTP.GetPositionAtOffset("".Length + 3) '--my string had ... on the end so had to add for avoiding the escape of that on length
RichTextBox1.Selection.Select(startTP, endTP)
RichTextBox1.Focus()
Exit For
End If
Next
This is the simple VB.net code layout, but you can simplify and adjust from there if you find it useful.
Thanks

Correct GTK CSS selector to use from SWT widgets?

I'm trying to apply GTK+ 3 CSS rules to widgets used in the Eclipse UI, specifically for a ComboBox right now. For example: say I want to make the background color of a selected text in a combobox to be red. The Style rule applied to the combobox widget would be
*.entry:selected { background-color: #FF0000; }
I have proven that rule using the Gtk+ CSS Inspector tool in Ubuntu and that works there but I do not find a way to apply that rule in code for ComboBoxes.
Inside SWT, there are places where CSS is used on Linux, such as this snippet to set the background color, however, adding the above rule to the CSS override does not have any apparent impact (yes, the background color does work).
#Override
void setBackgroundColor (long /*int*/ context, long /*int*/ handle, GdkRGBA rgba) {
// CSS to be parsed for various widgets within Combo
background = rgba;
String css = "* {\n";
if (rgba != null) {
String color = display.gtk_rgba_to_css_string (rgba);
css += "background: " + color + ";\n";
}
css += "}\n";
// Cache background color
cssBackground = css;
String finalCss = display.gtk_css_create_css_color_string (cssBackground, cssForeground, SWT.BACKGROUND);
//Injected code to make selected text red
finalCss += "\n.*:selected {background-color: #FF0000}";
if (entryHandle == 0 || (style & SWT.READ_ONLY) != 0) {
// For read only Combos, we can just apply the background CSS to the GtkToggleButton.
gtk_css_provider_load_from_css (OS.gtk_widget_get_style_context(buttonHandle), finalCss);
} else {
if (OS.GTK_VERSION >= OS.VERSION(3, 16, 0)) {
// For GTK3.16+, only the GtkEntry needs to be themed.
gtk_css_provider_load_from_css (OS.gtk_widget_get_style_context(entryHandle), finalCss);
} else {
// Maintain GTK3.14- functionality
setBackgroundColorGradient (OS.gtk_widget_get_style_context (entryHandle), handle, rgba);
super.setBackgroundColor (OS.gtk_widget_get_style_context (entryHandle), entryHandle, rgba);
}
}
// Set the background color of the text of the drop down menu.
OS.g_object_set (textRenderer, OS.background_rgba, rgba, 0);
}
I’ve tried various combinations of selectors and so far have not found a way to have the “selection” background color of the text to take effect.
Here is a link to the corresponding ComboBox class from SWT where it deals with CSS -- see the setBackgroundColor method.
https://github.com/eclipse/eclipse.platform.swt/blob/master/bundles/org.eclipse.swt/Eclipse%20SWT/gtk/org/eclipse/swt/widgets/Combo.java
I have proven that code runs and also managed to change the background for the entire combobox by changing the css rule there. However, if I inject my new rule it gets ignored.
Any help would be appreciated. Thanks!

monotouch dialog cell sizing and section caption color

I am using monotouch dialog to generate a view. I managed to change the background color by subclassing DialogViewController, but the contrast with the section caption makes the text hard to read. How do I change the section caption color?
I am also using a StyledStringElement as a button in the same view. I cant seem to figure out how to shrink this element so that it looks more like a button. Please provide examples. Your help is appreciated.
To solve this issue I used a glassbutton with the it's HandleTapped handler as below.
var butt = new GlassButton(new RectangleF(80,150,150,50))
{
Font = UIFont.BoldSystemFontOfSize(22),
NormalColor = UIColor.Red,
};
void HandleTapped (GlassButton obj)
{
}
as for the colors, I used miguel's solution here. Hope this helps someone down the road.

How can I change text color in CEikRichTextEditor Symbian

Is there anyway to change parts of the text I add into CEikRichTextEditor control without selecting the text first - which shows the green selection rectangle over the text - and then apply text style?
Here is the code I use which gives an ugly and sloppy style when user see the running green selection rectangle over the text especially when I insert the text inside a loop
CDesCArray* temp = new(ELeave) CDesCArrayFlat(4);
temp->AppendL(_L("First"));
temp->AppendL(_L("Second"));
temp->AppendL(_L("Third"));
temp->AppendL(_L("Fourth"));
TBuf<100>iNumbers;
iNumbers.Copy(_L("Here is the numbers"));
iRichText1->SetTextL(&iNumbers); // iRichText1 is a pointer to CEikRichTextEditor object
for(TInt i = 0; i < temp->Count(); i++)
{
TInt x = iRichText1->Text()->DocumentLength();
iRichText1->RichText()->InsertL(x, (*temp)[i]);
iRichText1->SetSelectionL(x,iRichText1->Text()->DocumentLength());
iRichText1->BoldItalicUnderlineEventL(CEikGlobalTextEditor::EItalic);
TInt line = iRichText1->Text()->DocumentLength();
iRichText1->RichText()->InsertL(line, _L("\f\f"));
}
Many thanks in advance.
You need to operate on the CRichText object owned by the editor and apply a paragraph or character format over it (using ApplyCharFormatL() / ApplyParaFormatL()). This avoids any need to select text.
Example applying a paragraph format
Example applying a character format

Bold text for a tab control

I'd like to bold the text for a tab page under certain conditions (not, necessarily, GotFocus). Is it true the only 'er easiest way to do this is by overriding the DrawItem event for the tab control?
http://www.vbforums.com/showthread.php?t=355093
It seems like there should be an easier way.
Like ...
tabControl.TabPages(index).Font = New Font(Me.Font, FontStyle.Bold)
That doesn't work, obviously.
When you set the Font property on a TabPage, you are setting the default font for all controls on that tab page. You are not setting it for the header, however.
When you execute the following code:
tabControl.TabPages(index).Font = New Font(Me.Font, FontStyle.Bold)
Any controls on that page will now be bold by default, which is not (I'm assuming) what you want.
The header's font (that is, the tab itself) is controlled by the TabControl's Font property. If you were to change your code to:
tabControl.Font = New Font(Me.Font, FontStyle.Bold)
You will see that in action. However, it changes the font for all the tabs on display, which is also not, I'm assuming, what you want.
So, using the default WinForms tab control, you are (I believe) limited to the technique in the link you've posted. Alternatively, you can begin looking at 3rd-party controls, such as those discussed in these questions on StackOverflow.
An easy way to give tab controls different labels depending on a field value is to change the caption itself:
For example:
Private Sub Form_Current()
If IsNull(Me.Subform.Form.Field_Name) Then
Me.Tab_Name.Caption = "Tab One"
Else
Me.Tab_Name.Caption = "Tab One +++"
End If
End Sub
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
Font BoldFont = new Font(tabControl1.Font, FontStyle.Bold);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, BoldFont, Brushes.Black, e.Bounds);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
}