Here is my context :
We are using WPF to create an new Windows user interface for our product. As we are cross-platform, all information as Label.Content or Button.Content are known in an other part of the application (written in C), and not defined in XAML.
Here is the problem :
We want to handle Strings that we put in a WPF component's content.
I see that we have some attributes as
Label.Content.FontFamily or Label.Content.Size, but graphic attributes are not necessary the same for all the String.
For exemple :
This is my label's content : "Hello guys, thank you to help me". Is it possible to
Underline "thank you"
Change all uppercases in Red color
Change the size of these uppercases
Actually, we are using WinFroms to do that, but it is time to renovate the GUI, because Winforms are just ugly now.
Thanks a lot !
The TextBlock's Content property (Text) expects a collection of Inline elements (InlineUICollection), you have the following available:
Inline
InlineUICollection
LineBreak
Run (defines Text property)
Span (defines Inlines property)
Bold
Italic
Underline
(Inlines is the Content property for span and it's an InlineUICollection, meaning that you can add Run and LineBreaks and other Spans/Bold/Italic inside Span.)
Using this it is possible to "Hello guys, thank you for helping me":
<TextBlock><Run Text="H" FontSize="20" Foreground="Red"/><Span>ello guys, <Underline>thank you</Underline> for helping me</Span></TextBlock>
The H will be red and bigger, and thank you will be underlined.
This is all possible programmatically as well, for example, "hello world" using a run in a textblock:
TextBlock t = new TextBlock();
t.Inlines.Add(new Run{Text="Hello World"});
Related
I want to display a formatted text in Xamarin IOS, c#. That means a text with special font and also links, like in the picture.
http://i.stack.imgur.com/5BSpH.jpg
I want to use just one TextView, or something else, but the lines must be continoulosly in the control.
Which controls, or methods of TextView should I use?
You can style text in iOS with NSAttributedString
You want to used some form of attributed string. You can use either NSAttributedString or NSMutableAttributedString. There may be some other forms. The mutable strings allow you to change the contents and style after creation and have different styles along the length of the string (for example "Hello World" could have "Hello" be in black and "World" be in red). I'm going to assume you're going to want to use a NSMutableString considering you will be using multiple different styles in one line.
Here's a quick example on how to use it.
NSDictionary format = new NSDictionary (
UIStringAttributeKey.Font, UIFont.FromName ("FontName", 20),
UIStringAttributeKey.ForegroundColor, UIColor.Black
);
NSMutableAttributedString nsString = new NSMutableAttributedString("String", format);
UILabel label = new UILabel();
label.AttributedText = nsString;
Obviously for your application, you're going to have to delve into the Xamarin NSMustableString docs (https://developer.xamarin.com/api/type/MonoTouch.Foundation.NSMutableAttributedString/) in order to fully customize it to your liking, but I think I've given you a decent jumpstart.
NSAttributedString definitely works.
To use custom fonts follow these steps.
I am adding dynamically generated textblock in code behind in WPF. I need to set the following properties for this textblock in code behind but I am not able to do so:
TextOptions.TextFormattingMode="Display"
RenderOptions.BitmapScalingMode='NearestNeighbor"
RenderOptions.ClearTypeHint="Enabled"
I cannot find them in code behind or any way to do so.
Can anyone please suggest how these properties can be set in code ?
I was looking for the same solution ... here it is :)
In my case I set the properties for a RichTextBox
richTxtEditor.SetValue(TextOptions.TextRenderingModeProperty, TextRenderingMode.Aliased)
richTxtEditor.SetValue(TextOptions.TextRenderingModeProperty, TextRenderingMode.Auto)
richTxtEditor.SetValue(TextOptions.TextRenderingModeProperty, TextRenderingMode.ClearType)
richTxtEditor.SetValue(TextOptions.TextRenderingModeProperty, TextRenderingMode.Grayscale)
richTxtEditor.SetValue(TextOptions.TextFormattingModeProperty, TextFormattingMode.Display)
richTxtEditor.SetValue(TextOptions.TextFormattingModeProperty, TextFormattingMode.Ideal)
richTxtEditor.SetValue(TextOptions.TextHintingModeProperty, TextHintingMode.Animated)
richTxtEditor.SetValue(TextOptions.TextHintingModeProperty, TextHintingMode.Auto)
richTxtEditor.SetValue(TextOptions.TextHintingModeProperty, TextHintingMode.Fixed)
What is c# code equivalent of following XAML, where I have a RichTextBox and I have selected Paragraph, and I want to enable/disable white space on this paragraph. In XAML I know how to enable, but I need to do this in code.
<Paragraph xml:space=\"preserve\"> Tabbed Code</Paragraph>
There is an equivalent I have found and its here,
void EnableWhiteSpace(Paragraph p, bool enable = true){
if(enable){
System.Windows.Markup.XmlAttributeProperties
.SetXmlSpace(this.Document, "preserve");
}
else{
System.Windows.Markup.XmlAttributeProperties
.SetXmlSpace(this.Document, "default");
}
}
This is still not working !!! I am not getting tabs !!...
Here is my problem, I have a RichTextBox which is used to edit code and which does syntax highlighting. Everything is fine except when I call following I see no tabs in my code.
TextRange tr = new TextRange(
myRichTextBox.Document.ContentStart,
myRichTextBox.Document.ContentEnd);
string text = tr.Text;
The text that I receive contains no tabs, so I thought enabling whitespace on every paragraph before doing text range might give me tabs.
UPDATE
I tried navigating every inlines (run) in the paragraph, none contains tab, I am just loosing all the tabs :(
There is no equivalent. The XML option xml:space="preserve" is valid only for interpreting XML files (in your case, XAML, which is a kind of XML), and has no meaning in C# as there are no XML files involved.
The C# equivalent of your XAML code would be following:
Paragraph p = new Paragraph();
p.Inlines.Add(new Run(" Tabbed Code"));
RichTextBox is buggy, it will just ignore all tabs, however its problem of WPF itself and cant be fixed without doing complex workarounds.
Alernative is to write your own FlowDocument to text converter, however this is very complex as nodes do not directly give any line or tag information.
Could someone demonstrate how to insert text from a text file e.g. test.txt into a Label control on a visual C# form please
You leave much to the imagination as to where you currently are with this and from which point you need help, but in the simplest form, try this:
theLabel.Text = File.ReadAllText(pathToFile);
label.Text = File.ReadAllText("test.txt");
File.ReadAllText Method
The answers for the question are useful so far.
Also you are considering this for WinForms as far as I understood from the tags.
If you would like to do any kind of action like this on Web Forms, you should consider that the text from the file goes without any encoding to label control. So, any kind of JavaScript code can be injected.
Always HtmlEncode your text;
var pathToFile = Server.MapPath("~/poo.txt");
lblPoo.Text = HttpUtility.HtmlEncode(File.ReadAllText(pathToFile));
I've got an application where I need to show a price, for that, I have the following code:
<Label Content="{Binding Prijs}" ContentStringFormat="C"></Label>
However, that gives a stringformat like: $10.00, but i want to show the euro-sign (€) instead of the dollar-sign ($). How do I do that?
You need to make sure that the Language of the control is set correctly.
Tim Heuer has a blog post entitled "StringFormat and CurrentCulture in Silverlight" about this for Silverlight so I expect the same problem occurs in WPF.
The solution for Silverlight is to add the following line to the view constructor:
this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
Now for WPF you might just need to make sure that the CurrentThread.CurrentCulture is set correctly, if not try adding this line too.