Ios 显示具有多种格式的单词的字符串

Ios 显示具有多种格式的单词的字符串,ios,objective-c,nsattributedstring,Ios,Objective C,Nsattributedstring,在我的应用程序中,我显示从服务器获取的用户评论。注释包含用户名、标记和一些粗体文本 例如: Nancy标记了衣服:第二季第五集。他们在哪里找到所有的旧衣服 “南希”和“衣服:”应分别为灰色和橙色,“第二季第五集”应为粗体 我曾尝试使用NSAttributedString,但未能实现上述目标 下面是我试图改变颜色但没有改变的代码。我不太清楚如何使用NSAttributedString NSMutableAttributedString *title = [[NSMutableAttributedS

在我的应用程序中,我显示从服务器获取的用户评论。注释包含用户名、标记和一些粗体文本

例如:
Nancy
标记了
衣服:
第二季第五集。他们在哪里找到所有的旧衣服

“南希”和“衣服:”应分别为灰色和橙色,“第二季第五集”应为粗体

我曾尝试使用
NSAttributedString
,但未能实现上述目标

下面是我试图改变颜色但没有改变的代码。我不太清楚如何使用NSAttributedString

NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:", tag.title]];
    [title addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:246/255.0 green:139/255.0 blue:5/255.0 alpha:1.0f] range:NSMakeRange(0,[title length])];
    self.tagCommentLabel.text = [NSString stringWithFormat:@"%@ tagged %@: %@ %@",tag.user.name , title, episode, tag.comment];

有人能帮我编写一个代码,告诉我如何用所需的格式实现示例句子吗?

你必须添加逻辑,因为我很确定你不想总是给这个特定的单词涂上颜色或加粗,但这里有一个简单的示例:

NSString *title = @"Nancy tagged Clothing: Season 2, Episode 5. where do they find all the old clothes";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:title];

// Color text for range of string
[attributedString addAttribute:NSForegroundColorAttributeName
                         value:[UIColor grayColor]
                         range:[title rangeOfString:@"Nancy"]];
[attributedString addAttribute:NSForegroundColorAttributeName
                         value:[UIColor grayColor]
                         range:[title rangeOfString:@"Clothing"]];

// Bold (be careful, I have used system font with bold and 14.0 size, change the values as for yourself)
[attributedString addAttribute:NSFontAttributeName
                         value:[UIFont systemFontOfSize:14.0 weight:UIFontWeightBold]
                         range:[title rangeOfString:@"Season 2"]];
[attributedString addAttribute:NSFontAttributeName
                         value:[UIFont systemFontOfSize:14.0 weight:UIFontWeightBold]
                         range:[title rangeOfString:@"Season 5"]];

self.tagCommentLabel.attributesText = attributedString;
编辑:要使其与您的代码配合使用,请删除以下行:

self.tagCommentLabel.text = [NSString stringWithFormat:@"%@ tagged %@: %@ %@",tag.user.name , title, episode, tag.comment];
将其更改为,而不是使用静态文本指定标题:

NSString *title = [NSString stringWithFormat:@"%@ tagged %@: %@ %@",tag.user.name , title, episode, tag.comment];

NSAttributedString和NSString是两种不同的东西。如果要向NSString添加属性(例如,更改文本的颜色),必须首先使NSString:

NSString *string = [NSString stringWithFormat:@"%@ tagged %@: %@ %@",tag.user.name , title, episode, tag.comment];
然后从NSString生成NSMutableAttributeString并向其添加属性:

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string]; 
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:246/255.0 green:139/255.0 blue:5/255.0 alpha:1.0f] range:[string rangeOfString:title];
如果要显示属性字符串,则应使用.attributedText而不是.text:

self.tagCommentLabel.attributedText = attString;

文本不是硬编码的。它来自服务器。是的,文本的这些部分总是要求颜色/粗体。我试过你的代码,它给我的结果和之前给我的一样。它在文本中打印带标签的
错误{NSColor=“UIDeviceRGBColorSpace 1 0.5 0 1”}
,但我不希望整个字符串具有相同的格式。我希望用户名为灰色,标题为橙色,插曲为粗体。用户名、标题、剧集和评论是来自服务器的不同对象。正如我在示例中所示,我将这些对象一起显示在标签
self.tagCommentLabel.text
中的一个句子中。我知道它们是不同的对象。在我的示例中,我刚刚向您展示了为了给标题上色,您必须在发布的代码中做些什么。您可以对从服务器获得的所有不同对象(字符串)执行相同的操作。例如,如果要为注释着色,可以执行以下操作:[attString addAttribute…range:[string rangeOfString:tag.comment]