2015-11-02 72 views
0

在我的应用程序中,显示的是从服务器获取的用户注释。注释包含用户名,标签和一些粗体文本。显示具有多种格式字的显示字符串

例如:Nancy标记Clothing:第2季,情节5.在哪里他们会发现所有的旧衣服”

词语“南希”和“服装:”应该是灰色和橙色颜色分别和“第2季第5集”。应该大胆。

我试过使用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]; 

有人可以帮我一个代码,我怎么才能实现所需格式的例句?

回答

0

NSAttributedString和NSString是两个不同的东西。如果你想添加属性的NSString(如改变文本的颜色),您必须首先做出的NSString:

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

然后你从你的NSString使NSMutableAttributedString和属性添加到它:

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]; 

而当你想显示你的属性串,你应该使用替代的.text .attributedText:

self.tagCommentLabel.attributedText = attString; 
+0

但我不希望整个字符串具有相同的格式。我希望用户名称为灰色,标题为橙色,情节为粗体。用户名,标题,剧集和评论是来自服务器的不同对象。我将这些对象一起显示在标签“self.tagCommentLabel.text”中的一个句子中,如示例中所示。 –

+0

我明白他们是不同的对象。在我的示例中,我只是向您展示了您在发布的代码中必须做的事情,以便为标题着色。您可以对从服务器获取的所有不同对象(字符串)执行相同操作。例如,如果你想为评论着色,你可以这样做:[attString addAttribute ... range:[string rangeOfString:tag.comment] – Preemoz

+0

它完美的工作。万分感谢!! –

0

你将不得不添加逻辑,因为我敢肯定,你不想总是颜色/加粗这个特定的词,但在这里,你去用一个简单的例子:

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]; 
+0

该文本不是硬编码。它来自服务器。是的,这些文本的部分总是需要颜色/粗体。我试过你的代码,它给了我早些时候给出的结果。它打印这个标记为“错误{NSColor =”UIDeviceRGBColorSpace 1 0.5 0 1“; }在文本内 –

+0

请参阅编辑,执行更改以查看结果。 – sunshinejr

+0

感谢您的回答.. –