【问题标题】:NSMutableAttributedString doesn't show string in different color for some specific range in iOS 8 & iOS 9NSMutableAttributedString 在 iOS 8 和 iOS 9 中的某些特定范围内不显示不同颜色的字符串
【发布时间】:2015-10-20 07:08:52
【问题描述】:

我在我的代码中使用属性字符串。为某些特定范围设置文本的字体和颜色。 为此,我使用下面的代码。 字体适用于范围,但未为该范围设置颜色。 NSForegroundColorAttributeName可能有问题。

如果有人有解决方案,请告诉我。

NSDictionary *attrs = @{ NSFontAttributeName:[UIFont systemFontOfSize:12.0f],NSForegroundColorAttributeName : [UIColor redColor] };


NSMutableAttributedString *titleStr = [[NSMutableAttributedString alloc]initWithString:@"New Message from Test"];
                [titleStr addAttributes:attrs range:NSMakeRange(0, 3)];

【问题讨论】:

  • 字体大小有效,但颜色无效。
  • 我没有看到您的代码有任何问题。我刚刚使用了你的代码,它工作正常。 See here
  • @RamarajT 我测试了它,但它不起作用
  • 你如何使用titleStr?您是否将其设置为任何 UILabel?您是否尝试过像我一样调试和悬停 titleStr?
  • 我必须在标签上设置它。是的,我做到了。等等我分享截图

标签: ios objective-c nsstring nsattributedstring


【解决方案1】:

我也遇到了类似的问题。所以我将我的字符串作为 HTML 字符串传递并像这样完成。

NSString *name = @"<center><font color='#cd1c5f' size='4px'>"
                 @"New"
                 @"</font>"
                 @"<font color='#000000' size='4px'>"
                 @"Message from test"
                 @"</font></center>";

NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[name dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

_lbl_nm.attributedText=attrStr;

【讨论】:

  • 为什么需要使用[NSString stringWithFormat:]
  • 传递多个html字符串。代替串联,如果有其他方法可以让我知道..
  • 好吧,您可以简单地拥有一个,而不是拥有 6 个文字字符串。您可以使用 NSString *name = @"first" @"second" ... @"sixth" (每个文字之间有一个换行符)。编译器会自动连接字符串。
  • 嘿,谢谢它确实有效。我是这个领域的新手,我还在学习。非常感谢您提供这么宝贵的信息。
【解决方案2】:

您可以附加两个具有不同属性的属性字符串,红色和文本颜色。让我们尝试使用以下代码块。

NSDictionary *attrs = @{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont systemFontOfSize:12.0]};
NSMutableAttributedString *titleString = [[[NSMutableAttributedString alloc] initWithString:@"New" attributes:attrDict] autorelease];
attrs = @{NSForegroundColorAttributeName:[UIColor lightTextColor], NSFontAttributeName:[UIFont systemFontOfSize:12.0]};
NSAttributedString *messageString = [[[NSAttributedString alloc] initWithString:@" Message from Test" attributes:attrs] autorelease];
[titleString appendAttributedString:messageString];
[self.titleTextView setAttributedText:titleString];

【讨论】:

    【解决方案3】:

    检查您是否在设置属性标题后更改标签的文本颜色。那是你面对这个问题的唯一方法。 设置标题之前进行。

    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.attributedText = titleStr;
    

    我认为您的代码没有任何问题。我刚刚复制了您的代码并进行了测试,一切都很好。

    【讨论】:

    • 这是评论,不是答案。
    • 我和提问者聊天,这是他的问题。他在设置属性标题后重置了 textColor。
    • 认真的吗?他没有用新字符串调用setAttributedText:?这太荒谬了。
    • 这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
    • @JAL 不,请参阅我答案的最后一行,提问者在重置属性标题后确实重置了 textColor。这就是他的问题所在。我和他聊天。
    猜你喜欢
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    相关资源
    最近更新 更多