Xamarin.ios 我是否可以仅为特定部分更改标签的着色颜色和字体属性?

Xamarin.ios 我是否可以仅为特定部分更改标签的着色颜色和字体属性?,xamarin.ios,label,Xamarin.ios,Label,我需要格式化标签中的字符串,以便仅以不同的颜色和不同的字体属性显示我想要的单词。我不知道是否有可能这样做。我知道我可以在文本视图中更改它,但我不确定是否有标签,在我的谷歌搜索中也没有找到任何东西 有人能告诉我这是否可能吗?如果是这样,我该怎么做呢?有一个AttributedText属性UILable来实现这一点 类型表示一个字符串,该字符串统一应用了一系列属性 配套的类型可用于创建具有重叠属性且其内容可在创建后修改的属性字符串 nsattributestring的示例代码: var labe

我需要格式化标签中的字符串,以便仅以不同的颜色和不同的字体属性显示我想要的单词。我不知道是否有可能这样做。我知道我可以在文本视图中更改它,但我不确定是否有标签,在我的谷歌搜索中也没有找到任何东西


有人能告诉我这是否可能吗?如果是这样,我该怎么做呢?

有一个
AttributedText
属性UILable来实现这一点

类型表示一个字符串,该字符串统一应用了一系列属性

配套的类型可用于创建具有重叠属性且其内容可在创建后修改的属性字符串

nsattributestring
的示例代码:

var label0 = new UILabel(new CGRect(20, 100, 400, 50));
View.AddSubview(label1);
var text = new NSAttributedString(
"Here is the test for NSAttributedString.",
font: UIFont.FromName("HoeflerText-Regular", 24.0f),
foregroundColor: UIColor.Red,
strokeWidth: 4
);
label0.AttributedText = text;
var label1 = new UILabel(new CGRect(20, 200, 400, 50));
View.AddSubview(label);
var mutableStr = new NSMutableAttributedString("Here is the test for NSMutableAttributedString.");
var range = mutableStr.MutableString.LocalizedStandardRangeOfString(new NSString("test"));
mutableStr.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Red, range);
mutableStr.AddAttribute(UIStringAttributeKey.Font, UIFont.BoldSystemFontOfSize(25), range);
label1.AttributedText = mutableStr;
nsmutableAttributeString
的示例代码:

var label0 = new UILabel(new CGRect(20, 100, 400, 50));
View.AddSubview(label1);
var text = new NSAttributedString(
"Here is the test for NSAttributedString.",
font: UIFont.FromName("HoeflerText-Regular", 24.0f),
foregroundColor: UIColor.Red,
strokeWidth: 4
);
label0.AttributedText = text;
var label1 = new UILabel(new CGRect(20, 200, 400, 50));
View.AddSubview(label);
var mutableStr = new NSMutableAttributedString("Here is the test for NSMutableAttributedString.");
var range = mutableStr.MutableString.LocalizedStandardRangeOfString(new NSString("test"));
mutableStr.AddAttribute(UIStringAttributeKey.ForegroundColor, UIColor.Red, range);
mutableStr.AddAttribute(UIStringAttributeKey.Font, UIFont.BoldSystemFontOfSize(25), range);
label1.AttributedText = mutableStr;
其效果是:

非常感谢!:)那很有效