C # Monotouch / Xamarin.iOS - UILabel with simple and italic text in one line

I have a line of text containing plain and italic text. How to present this using UILabel?

"I'm plain text, while text is in italics

var someText = "I am plain text, while text is in italics

UILabel myLabel = new UILabel(new RectangleF(0,0,100,40));
myLabel.Text = someText;
+4
source share
2 answers

I prefer concatenation as follows:

var text = new NSMutableAttributedString (
               str: "I am plain text whereas ", 
               font: UIFont.SystemFontOfSize (14f)
               );

text.Append (new NSMutableAttributedString (
               str: "I am italic text.", 
               font: UIFont.ItalicSystemFontOfSize (14f)
               ));

var label = new UILabel () { AttributedText = text };

It seems easier to maintain this method instead of counting characters. But I completely agree - all this AttributedString stuff is hacked. Jason's link to the docs was helpful, thanks.

+5
source

UILabel AttributedText, .

var prettyString = new NSMutableAttributedString ("UITextField is not pretty!");
prettyString.SetAttributes (firstAttributes.Dictionary, new NSRange (0, 11));
prettyString.SetAttributes (secondAttributes.Dictionary, new NSRange (15, 3));
prettyString.SetAttributes (thirdAttributes.Dictionary, new NSRange (19, 6));

Xamarin Docs.

+3

All Articles