【问题标题】:setting up attributes for links in NIAttributedLabel在 NIAttributedLabel 中为链接设置属性
【发布时间】:2012-09-30 05:27:30
【问题描述】:

我已完成以下操作以使 NIAttributedLabel 中的链接具有不同的颜色:

NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        [attributes setValue:[UIColor colorWithRed:86.0/255.0 green:134.0/255.0 blue:172.0/255.0 alpha:1.0] forKey:NSForegroundColorAttributeName];
        [attributes setValue:[UIColor colorWithRed:0.0 green:136/255.f blue:204/255.f alpha:1.0] forKey:NSForegroundColorAttributeName];
        [self.commentsText_ setAttributesForLinks:attributes];

但我没有在链接中看到两种不同的颜色,而只是看到一种。我在这里做错了什么?基本上我有一个通过 addLink 添加的链接,如下所示:

[self.commentsText_ addLink:[NSURL URLWithString:url] range:usernameRange];

我希望它具有红色。我该怎么做?

【问题讨论】:

  • 您为什么希望看到 2 种颜色?您的第二个 setValue: 调用只是替换了第一个,因为您正在设置相同键的值。
  • 那么我该如何做才能看到不同链接的两种颜色呢?

标签: iphone objective-c ios ipad nimbus-ios


【解决方案1】:

如果你想为不同的链接使用不同的颜色,那么你需要创建单独的属性字典,并使用不同的字典调用 setAttributesForLinks:。

        NSMutableDictionary *attributes1 = [NSMutableDictionary dictionary];
        [attributes setValue:[UIColor colorWithRed:86.0/255.0 green:134.0/255.0 blue:172.0/255.0 alpha:1.0] forKey:NSForegroundColorAttributeName];  
        [self.commentsText1_ setAttributesForLinks:attributes1];

        NSMutableDictionary *attributes2 = [NSMutableDictionary dictionary];
        [attributes setValue:[UIColor colorWithRed:0.0 green:136/255.f blue:204/255.f alpha:1.0] forKey:NSForegroundColorAttributeName];  
        [self.commentsText2_ setAttributesForLinks:attributes2];

【讨论】:

  • 所以你是说一个 NIAttributedLabel 只能有 1 个链接颜色?那会破坏整个目的
  • 不知道你的目的是什么,所以无法评论。我在 Nimbus 文档中没有看到任何对 setAttributes:forLinks: 的引用,所以我不确定该方法有什么可能。
【解决方案2】:

如果您想为不同的链接设置不同的颜色,那么您应该通过将 linkColor 设置为 nil 来禁用自动链接样式,然后将不同的样式显式应用于您的链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2013-07-20
    • 1970-01-01
    相关资源
    最近更新 更多