【问题标题】:How to format certain words of a cell's text label如何格式化单元格文本标签的某些单词
【发布时间】:2016-06-22 19:54:15
【问题描述】:

我有一个 xamarin iOS 项目,我在其中显示一个带有 x 行文本的单元格。我目前这样定义我的单元格:

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 文本转换为粗体字体,并将其他信息保留为普通文本字体。考虑到我目前的方法,这可能吗?

【问题讨论】:

    标签: c# ios swift xamarin fonts


    【解决方案1】:

    你可以这样做:

    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
    

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

    【讨论】:

    • 谢谢!为了后代,我将以 xamarin 风格发布我自己的答案,但你的答案是正确的!
    • 很高兴它帮助了你!
    【解决方案2】:

    看看你对UITableViewCell的使用,我遇到了following——看起来你可以通过NSAttributedString操作文本

    【讨论】:

    • 这是正确的答案。 UITextViewUITextFieldUILabel 都具有 attributedText 属性。您可以构建一个属性字符串,其中您想要的单词以粗体显示,然后将该字符串也分配给该字段的 attributedText 属性
    【解决方案3】:

    感谢桑托斯的回答!这是我最终用来让它在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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 2019-11-26
      • 2017-06-16
      相关资源
      最近更新 更多