gpt4 book ai didi

ios - Xamarin iOS中使用NSSuperscriptAttributeName的NSAttributedString

转载 作者:行者123 更新时间:2023-12-01 18:11:10 30 4
gpt4 key购买 nike

您好,我试图在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);

here可以看到 MonoMac.Foundation中可用。有谁知道如何在iOS中获取该字符串,因为 UIKit.UIStringAttributeKey似乎不可用。

最佳答案

从这个answer中,我可以像这样使用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);

关于ios - Xamarin iOS中使用NSSuperscriptAttributeName的NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31117890/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com