4

我有一个包含纯文本和斜体文本的文本字符串。我如何使用 UILabel 来表示它?

“我是纯文本,而我是斜体文本

var someText = "我是纯文本,而我是斜体文本

UILabel myLabel = new UILabel(new RectangleF(0,0,100,40));
myLabel.Text = someText;
4

2 回答 2

5

我更喜欢这样连接:

var text = new NSMutableAttributedString (
               str: "I am plain text whereas ", 
               font: UIFont.SystemFontOfSize (14f)
               );

text.Append (new NSMutableAttributedString (
               str: "I am italic text.", 
               font: UIFont.ItalicSystemFontOfSize (14f)
               ));

var label = new UILabel () { AttributedText = text };

似乎更容易维护这种方式而不是计算字符。但我完全同意——所有这些 AttributedString 的东西都是 hacky。杰森的文档链接很有帮助,谢谢。

于 2014-09-19T03:15:33.377 回答
3

UILabel 有一个 AttributedText 属性,您可以使用它来设置文本样式。

var prettyString = new NSMutableAttributedString ("UITextField is not pretty!");
prettyString.SetAttributes (firstAttributes.Dictionary, new NSRange (0, 11));
prettyString.SetAttributes (secondAttributes.Dictionary, new NSRange (15, 3));
prettyString.SetAttributes (thirdAttributes.Dictionary, new NSRange (19, 6));

Xamarin Docs中提供了完整的示例。

于 2014-01-22T14:12:56.610 回答