How to dynamically change font weight in UITextView in iOS Mono - c#

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?

Related

What to use to dispaly formatted text in Xamarin IOS?

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.

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

Equivalent method to "Reset to Match Style"

I am looking for a c# method equivalent to the "Reset to Match Style" function when you right click on a Chart. In fact I change Datalabels font size in my code but the font size is always 18. The good font size is applied when I use the "Reset to Match Style" function" but I need to do it in code.
Thanks in advance,
Leep
After 2 days of developement, I have solved my issue.
It was not obvious because the Api is not clear with that. You have to use the Paste method of your chart Object which is inherited from _Chart.
Here is the Api link : http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._chart.paste(v=office.14).aspx

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

C# Button Text Unicode characters

C# doesn't want to put Unicode characters on buttons. If I put \u2129 in the Text attribute of the button, the button displays the \u2129, not the Unicode character, (example - I chose 2129 because I could see it in the font currently active on the machine).
I saw this question before, link text, but the question isn't really answered, just got around. I am working on applications which are going all over the world, and don't want to install all the fonts, more then "don't want", there are that many that I doubt the machine I am working on has sufficient disk space. Our overseas sales agents supply the Unicode character "numbers". Is there another way forward with this?
As an aside, (curiosity), why does it not work?
The issue is:
C# will let you put Unicode in, like button1.Text = "Hello \u2129";, no problem
but the Visual Studio Forms designer will not recognize '\u2129' as anything special. By design.
So just paste in the '℩' in the Properties Window or use code.
Change the "Font" of the button to the "Font" (From google:Arial Unicode MS) which supports "u2129". It may help you
have you tried entering the characters manually? also, have you tried using a literal string with #"blahblahblah" ?
I was trying to include copyright symbol (\u00a9) in the form title. Using escape characters or changing fonts didn't work for me. I simply copy-pasted the symbol from text editor.

Categories

Resources