Xamarin.iOS UIBarButtonItem With Unicode Character

2019-07-28 23:23发布

问题:

With a Xamarin.iOS project, I'm trying to use a Unicode character (gear icon) for the left bar button item in my top menu. I could use a custom icon for this, bit I read on some iOS posts here on SO that you can also use Unicode characters. So I followed an example, translated form Swift and it all works fine, except that my button title is always the literal Unicode string instead of the decoded gear icon.

Also, the code below will not build because that string ("\u{2699}") needs to be escaped. I've used \ and @ escaping methods but I always end up with just the literal string in my button. Any ideas what I'm doing wrong?

var ltButton = new UIBarButtonItem();
ltButton.Title = new NSString("\u{2699}", NSStringEncoding.UTF8);
ltButton.Style = UIBarButtonItemStyle.Plain;

var font = UIFont.FromName("Helvetica", 18.0f);
ltButton.SetTitleTextAttributes(new UITextAttributes { Font = font }, UIControlState.Normal);

this.NavigationItem.SetLeftBarButtonItem(ltButton, true);

回答1:

This is all you need:

ltButton.Title = "\u2699";

ltButton.Title = "\uD83D\uDE03";