如何格式化单元格的文本标签的某些单词

我有一个Xamarin iOS项目,我正在显示一个单元格的文本行。 我目前定义我的单元格如下所示:

public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath) { UITableViewCell cell = tableView.DequeueReusableCell ("cells"); ... cell.TextLabel.Lines = 0; cell.TextLabel.Text = "Bold: " + info1 + "Bold: " + info2; ... } 

其中显示单元格如下所示:

  +----------------------+ |Bold: Testing | |Bold: Testing2 | +----------------------+ 

我希望能够将Bold文本转换为粗体字体,并将其他信息保存为普通文本字体。 这是可能给我目前的做法?

你可以这样做:

 let attributedText = NSMutableAttributedString(string: "YOUR FIRST TEXT STRING", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(20.0), NSForegroundColorAttributeName: UIColor.redColor()]) let anotherString = NSAttributedString(string: "YOUR SECOND TEXT STRING", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(20.0), NSForegroundColorAttributeName: UIColor.blueColor()]) attributedText.appendAttributedString(anotherString) cell.TextLabel.AttributedText = attributedText 

您可以更改字体大小以及颜色。

看看你使用UITableViewCell ,我来到以下 – 看起来你可能能够通过NSAttributedString操纵文本

感谢桑托斯的答案! 以下是我最终用来在xamarin/c#为我工作的内容:

 var attributedText = new NSMutableAttributedString("Bold:", font: UIFont.BoldSystemFontOfSize(17f)); var infoText = new NSAttributedString(info); var bold2 = new NSAttributedString("\nBold: ", font: UIFont.BoldSystemFontOfSize(17f)); var infoText2 = new NSAttributedString(info2); attributedText.Append(infoText); attributedText.Append(bold2); attributedText.Append(infoText2); cell.TextLabel.AttributedText = attributedText;