Hi Im trying to get the superscript attribute to work with a UILabel in a xamarin iOS app. I have the following code, Im using a sketches right now just to see how it looks, so should be easy to copy/paste :D :
using UIKit;
using Foundation;
using CoreGraphics;
var label = new UILabel( new CGRect(0,0,UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height));
label.Text = "100.0o";
label.TextAlignment = UITextAlignment.Center;
var prettyString = new NSMutableAttributedString ("UITextField is not pretty!");
prettyString.AddAttribute (UIStringAttributeKey.SuperscriptAttributeName, NSNumber.FromDouble (1.0), new NSRange (prettyString.Length -1 , 1));
label.AttributedText = prettyString;
RootView.Add(label);
I can see from here that it is available in MonoMac.Foundation. Does anyone know how to get that string in iOS as it doesn't seem to be available from UIKit.UIStringAttributeKey.
From this answer I was able to use UIStringAttributeKey.BaselineOffset like so:
// Sketch your next great idea!
using UIKit;
using Foundation;
using CoreGraphics;
using CoreText;
var slider = new UISlider (new CGRect(0,UIScreen.MainScreen.Bounds.Height-30,UIScreen.MainScreen.Bounds.Width, 30.0f));
slider.MinValue = 10.0f;
slider.MaxValue = 120.0f;
var h = 65.0f;
var label = new UILabel( new CGRect(0,h,UIScreen.MainScreen.Bounds.Width, h*2));
var firstAttributes = new UIStringAttributes ();
var secondAttributes = new UIStringAttributes ();
var prettyString = new NSMutableAttributedString ("99.99°");
var label2 = new UILabel( new CGRect(0,h*3,UIScreen.MainScreen.Bounds.Width, h));
slider.ValueChanged += (object sender, EventArgs e) =>
{
h = slider.Value;
label.Text = slider.Value.ToString ();
label.Frame = new CGRect(0,h,UIScreen.MainScreen.Bounds.Width, h*2);
label.TextAlignment = UITextAlignment.Center;
label.Font = UIFont.FromName("Helvetica", h);
firstAttributes = new UIStringAttributes (new NSDictionary(
// Can set this to >1.0 to make it higher but was too high for me.
// CTStringAttributeKey.Superscript, NSNumber.FromDouble (0.0),
UIStringAttributeKey.Font ,UIFont.FromName("Helvetica", h),
UIStringAttributeKey.BackgroundColor ,UIColor.FromRGBA (0.0f, 1.0f, 1.0f, 0.2f)
));
secondAttributes = new UIStringAttributes (new NSDictionary(
UIStringAttributeKey.Font ,UIFont.FromName("Helvetica", h/2),
UIStringAttributeKey.BackgroundColor ,UIColor.FromRGBA (1.0f, 0.0f, 1.0f, 0.2f),
UIStringAttributeKey.BaselineOffset, NSNumber.FromDouble (h/3)
));
prettyString.SetAttributes (firstAttributes.Dictionary, new NSRange (1, prettyString.Length-1));
prettyString.SetAttributes (secondAttributes.Dictionary, new NSRange (prettyString.Length-1, 1));
label.AttributedText = prettyString;
label2.Frame = new CGRect(0,h*3,UIScreen.MainScreen.Bounds.Width, h);
label2.Text = "99.99°";
label2.TextAlignment = UITextAlignment.Center;
label2.Font = UIFont.FromName("Helvetica", h);
};
RootView.Add(slider);
RootView.Add(label);
RootView.Add(label2);