- ajsmith595
- Posts: 8
- Joined:
Unity XAML Editing at Runtime
I am working with NoesisGUI with Unity, and I am trying to add Run tags into an existing TextBlock at runtime. Is this possible? I am trying to get it so that specific parts of the text are certain colours and bold/italic when needed.
If it was a Grid then you could just add a Run tag to its children, but I want to do this with a TextBlock, but this does not have this option. I have tried adding the tags as you would in XAML literally as in setting its text value to:
<Run Text="whatever" Foreground="SomeColour"></Run>
But all this does is display the literal text. Any ideas?
And by the way, I did put actual colours into the Foreground property, this was just a demo of what I did
If it was a Grid then you could just add a Run tag to its children, but I want to do this with a TextBlock, but this does not have this option. I have tried adding the tags as you would in XAML literally as in setting its text value to:
<Run Text="whatever" Foreground="SomeColour"></Run>
But all this does is display the literal text. Any ideas?
And by the way, I did put actual colours into the Foreground property, this was just a demo of what I did
- ajsmith595
- Posts: 8
- Joined:
Re: Unity XAML Editing at Runtime
I have solved this by instead of using one TextBlock, using a StackPanel with WrapPanels for each line, with TextBlocks in those WrapPanels for each of the different text styles.
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Unity XAML Editing at Runtime
Hi,
TextBlock exposes the Inlines property. It is a collection where you can add inline elements like Run, Span, LineBreak...
This is better than using a WrapPanel with several TextBlocks inside.
TextBlock exposes the Inlines property. It is a collection where you can add inline elements like Run, Span, LineBreak...
Code: Select all
TextBlock txt = (TextBlock)FindName("txt");
txt.Inlines.Add(new Run { Text = "Hello", Foreground = Brushes.Red });
txt.Inlines.Add(new Run { Text = " World", Foreground = Brushes.Blue });
Who is online
Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 67 guests