2013-03-26 39 views
4

我想在UILabel中强调文本。 我发现下面的代码ObjC:单引号中的UILabel中的下划线文本(移植ObjC代码)

NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @1} 
myLabel.attributedText = [[NSAttributedString alloc] initWithString:@"Test string" 
                attributes:underlineAttribute]; 

我想这个端口到C#,但它不工作。 我想以下几点:

var keys = new object[] { "NSUnderlineStyleAttributeName" }; 
var objects = new object[] { 1 }; 
NSDictionary underlineAttribute = NSDictionary.FromObjectsAndKeys(objects, keys); 
label.AttributedText = new NSAttributedString(@"Test",underlineAttribute); 

,而不是1,我也尝试过 “1” 和NSUnderlineStyle.Single,但没有什么工作

任何想法?

感谢

回答

8

试试这个:

label.AttributedText = new NSAttributedString (
    "Test", 
    underlineStyle: NSUnderlineStyle.Single); 
+0

THX,这个是工作!构造函数中的第二个属性使用格式“name:value”。这是1项物品的简短字典吗? – Matt 2013-03-27 11:36:02

+0

@Matt:不,有一个ctor有很多可选参数,并且该语法表示要为“underlineStyle”参数传递NSUnderlineStyle.Single(以及其余参数的默认值)。 Xamarin Studio应该向你展示intellisense,你可以在其中看到你可以传递的所有参数(这是我们提供的一个辅助构造函数,我们为你创建了字典)。 – 2013-03-27 13:12:18

+0

感谢Rolf。我看到了构造函数,但我不知道我不是被迫提供所有这些构造函数。很高兴知道 – Matt 2013-03-27 18:49:45