在Xamarin Mac中用于AttributeName的内容

tof*_*tim 9 c# xamarin

我正在尝试着色Xamarin中的NSMutableAttributedString中的子字符串,但似乎缺少正确的常量,

在此输入图像描述

我该放什么?

更新.这更接近:

var s = new NSMutableAttributedString ("hello");
s.AddAttribute (CTStringAttributeKey.ForegroundColor , NSColor.Red, new NSRange (0, 3));
wordLabel.AttributedStringValue = s;
Run Code Online (Sandbox Code Playgroud)

并给出

在此输入图像描述

虽然屏幕上的颜色仍然是黑色文字!

在此输入图像描述

Update2可能CTStringAttributeKey是错误的,但没有NSStringAttributeKey

在此输入图像描述

Mik*_*rds 14

如果人们想知道Xamarin iOS的等价物是什么,这里是:前景颜色属性名称可以在这里找到:UIStringAttributeKey.ForegroundColor

NSColor.Red应该是UIColor.Red

因此,添加属性应如下所示:

s.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Red, new NSRange(0,3));
Run Code Online (Sandbox Code Playgroud)


ryr*_*ich 7

好的,所以我查看了API,它似乎就在那里,就在下面 NSAttributedString

ForegroundColorAttributeName

所以使用类似的东西:

s.AddAttribute(NSAttributedString.ForegroundColorAttributeName, NSColor.Red, new NSRange(0,3));
Run Code Online (Sandbox Code Playgroud)

  • 看起来现在是“ NSStringAttributeKey.ForegroundColor” (3认同)
  • 可悲的是,这对Xamarin.iOS无效. (2认同)

小智 6

重要:

Xamarin.Mac中的键已从此更改NSAttributedStringNSStringAttributeKey:

s.AddAttribute(NSAttributedString.ForegroundColorAttributeName, NSColor.Red, new NSRange(0,3));
Run Code Online (Sandbox Code Playgroud)

应该:

s.AddAttribute(NSStringAttributeKey.ForegroundColorAttributeName, NSColor.Red, new NSRange(0,3));
Run Code Online (Sandbox Code Playgroud)