What to use to dispaly formatted text in Xamarin IOS? - c#

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.

Related

Referencing symbols of a font that don't have a unicode in c#

I'm trying to use an Icon Font inside a Xamarin application. It is actually quite straight forward and i got the font to work.
However it works fine with using Unicode to reference the symols, like this:
Label iconLabel = new Label
{
Text = "\u0078"
};
But i discovered that only a few symbols of this font have a unicode assigned, the other symbols only have a name. That icon font is usually supposed to be used in html where these symbols can be referenced by the name.
If i try to do this in C#: Text = "arrow_down" it won't work, as it only displays the plain text.
Is there a way this could work with the names of the symbols or maybe a way to automatically add unicodes to every symbol in this font?
I ended up using the fantastic tool fontello and uploaded the open source icon font, selected all the symbols from it and downloaded it again - which generated new unicodes for every single symbol and i got it to work like a charm now. (Additionally fontello generates a great spreadsheet showing every code for every symbol).
In the end i guess this is the smartest way to work around this - if you need to access symbols from a font via unicode but the font isn't laid-out for this, use a tool like fontello to generate your own.

How to dynamically change font weight in UITextView in iOS Mono

I have a question how to change font weight in UITextView in iOS Mono?
For example i have text= "My name Foo.I Like to eat. #eat I love cookie";
i want to change display "#hashtag" to bold
My name Foo.I Like to eat. #hashtag I love cookie
You can achive this by using attributed strings. Below iOS6 you have to use CATextLayer.
Follow this great post:
Bold & Non-Bold Text In A Single UILabel?

Handle Font in WPF

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"});

.Xaml doesn't render unicode that write in the code?

In .xaml of windows store app, If I write like this:
<TextBlock Text="☆" />
It will render a star.
But If I write like this in C# code:
TextBlock tb = new TextBlock
{
Text = "☆"
};
it won't render, why? How to render the unicode write in c# code??
The &...; notation is an XML encoding, there is no decoding applied to C# strings. Use
TextBlock tb = new TextBlock
{
Text = "\x2606";
// Or just:
// Text = "☆";
};
You are using what is called an xmlunicode glyph... see this list of unicode glyphs
To output a stress outlined white star in c# you would specify the unicode as follows:
char c = '\u2729';
System.Console.WriteLine(c.ToString());
See this link for a better description
See this link for a list of unicode characters and their respective codes

Color Text in Rich Text Box in .NET

I have Rich TextBox to Edit XML Text
What i want how to color the XML Tags Names inside the RichTextBox
I want the tags names in RED or Green color.
Any Way to do that?
Work out what your desired regex is using this page. Once you have this you could use something like the following method to update the RichTextBox
public static void HighlightSyntax(RichTextBox richTextBox, Regex yourRegex, Color someColor)
{
richTextBox.BeginUpdate();
int selPos = richTextBox.SelectionStart;
richTextBox.SelectAll();
richTextBox.SelectionColor = normTextColor;
richTextBox.Select(selPos, 0);
// For each match from the regex, highlight the word.
foreach (Match keyWordMatch in yourRegex.Matches(richTextBox.Text))
{
richTextBox.Select(keyWordMatch.Index, keyWordMatch.Length);
richTextBox.SelectionColor = someColor;
richTextBox.Select(selPos, 0);
richTextBox.SelectionColor = normTextColor;
}
richTextBox.EndUpdate();
}
You could also adopt a timer to update this automatically after a set time.
I hope this helps.
Note. For large text files, and approach like this will be slow! In this case I would adopt Sinctilla.NET as a full syntax highlighter as stated in one of the answers below...
there are articles which explain or suggest possible approaches to syntax colouring, for example: How To Implement Syntax Highlighting In A WinForms Application
I think the best and easiest way is to use Scintilla.NET to handle that so you can focus on what really matters to you instead of reinventing the wheel again :)
MSDN created a simple C# function that formats the text content from a RichTextBox:
LINK
Using a simple regex find the location (start and end) of each tag and colorize it like below:
richtextbox1.Select(start, end-start);
richtextbox1.SelectionColor = Color.Green;
richtextbox1.Select(start, 0);
Check out scintilla, a nice source code editing component for Windows which supports Syntax Highlighting too. And there's a .NET wrapper for it called ScintillaNET.
I think for small syntax highlighting projects, roll your own! There are some examples of highlighting in a syntax editor already posted.
https://stackoverflow.com/a/13007641/1033686
https://stackoverflow.com/a/13007710/1033686
For larger projects that demand better highlighting, use Scintilla.NET (but be warned, it it a bit heavy and can be tricky to get working!)
http://scintillanet.codeplex.com/
For enterprise projects use a commercial product like actipro syntax editor.
http://www.actiprosoftware.com/products/controls/windowsforms/syntaxeditor?gclid=CI6rrqmglLMCFSfMtAodbE8AhA

Categories

Resources