在Xamarin Mac中用于AttributeName的内容

时间:2014-03-28 18:01:34

标签: c# xamarin

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

enter image description here

我该放什么?

更新。这就越近了:

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

并给出

enter image description here

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

enter image description here

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

enter image description here

5 个答案:

答案 0 :(得分:11)

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

同样NSColor.Red应为UIColor.Red

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

s.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Red, new NSRange(0,3));

答案 1 :(得分:6)

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

下面

ForegroundColorAttributeName

所以使用类似的东西:

s.AddAttribute(NSAttributedString.ForegroundColorAttributeName, NSColor.Red, new NSRange(0,3));

答案 2 :(得分:5)

重要:

Xamarin.Mac中的密钥已从NSAttributedString更改为NSStringAttributeKey,因此:

s.AddAttribute(NSAttributedString.ForegroundColorAttributeName, NSColor.Red, new NSRange(0,3));

应该是:

s.AddAttribute(NSStringAttributeKey.ForegroundColorAttributeName, NSColor.Red, new NSRange(0,3));

答案 3 :(得分:1)

我可以确认上面所说的是正确答案:

s.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Red, new NSRange(0,3));

NSRange值似乎与字符串的长度有关。因此,如果字符串长度为5个字符,则NSRange应为NSRange(0, 5)

答案 4 :(得分:0)

我无法使上述解决方案有效,因为它无法从NSMutableAttributedString转换为UIStringAttributes。所以在玩了一些代码后,我得到了这个有效的解决方案:

UINavigationBar.Appearance.TitleTextAttributes = new UIStringAttributes
{
    ForegroundColor = UIColor.White
};

这是针对Xamarin.iOS,补充了Mike Richards的回答。我从来没有为Xamarin.Mac开发,所以我不知道它是否可以在那里工作。