簡體   English   中英

Xamarin iOS中使用NSSuperscriptAttributeName的NSAttributedString

[英]NSAttributedString using NSSuperscriptAttributeName in Xamarin iOS

您好,我試圖在Xamarin iOS應用中獲取上標屬性以與UILabel一起使用。 我有以下代碼,Im現在正在使用草圖,只是看它的外觀,因此應該易於復制/粘貼: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);

這里可以看到,它在MonoMac.Foundation中可用。 有誰知道如何在iOS中獲取該字符串,因為UIKit.UIStringAttributeKey似乎不可用。

從這個答案,我能夠這樣使用UIStringAttributeKey.BaselineOffset:

// 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM