如何格式化单元格的文本标签的某些单词 [英] How to format certain words of a cell's text label

查看:205
本文介绍了如何格式化单元格的文本标签的某些单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xamarin的iOS项目,我正在用x行文本显示一个单元格。我现在定义我的单元格是这样的:

pre $ 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;





$ p
$ b $ p :

  + ---------------------- + 
|粗体:测试|
| Bold:Testing2 |
+ ---------------------- +

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

解决方案

您可以这样做:
$ b $ (NSFontAttributeName:UIFont.boldSystemFontOfSize(20.0),NSForegroundColorAttributeName:UIColor.redColor()])$ b {pre> let attributedText = NSMutableAttributedString(字符串:您的第一个TEXT字符串 $ b let anotherString = NSAttributedString(string:YOUR SECOND TEXT STRING,attributes:[NSFontAttributeName:UIFont.systemFontOfSize(20.0),NSForegroundColorAttributeName:UIColor.blueColor()])
属性文字。 b cell.TextLabel.AttributedText = attributedText

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

I have a xamarin iOS project where I am displaying a cell with x lines of text. I currently define my cells like so:

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;

        ...
    }

Which displays the cell like:

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

I want to be able to turn just the Bold text to the bold font and keep the other info in normal text font. Is this possible given my current approach?

解决方案

You could do like this:

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

You can change your font size as well as color.

这篇关于如何格式化单元格的文本标签的某些单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆