【问题标题】:Two hyperlinks in UITextView with different colors?UITextView 中两个颜色不同的超链接?
【发布时间】:2018-09-27 13:10:56
【问题描述】:

我正在尝试制作一个 UITextView 以在其中包含两个链接作为文本,但颜色不同。我不确定这是否可能。

我已将 UITextView 设置为检测链接,不可编辑,但只能选择。

到目前为止,我所做的如下:

NSMutableAttributedString *termsAndConditionsTitle = [[NSMutableAttributedString alloc] initWithString:@"I have read the " attributes:@{NSLinkAttributeName: @"https://apple.com", NSForegroundColorAttributeName: [UIColor greenColor]}];
NSMutableAttributedString *someString2 = [[NSMutableAttributedString alloc] initWithString:@"terms and conditions" attributes:@{NSLinkAttributeName: @"https://microsoft.com", NSForegroundColorAttributeName: [UIColor redColor]}];
[termsAndConditionsTitle appendAttributedString:someString2];
[self.termsAndConditionsView setAttributedText:termsAndConditionsTitle];

但最后链接只有 UITextView 的色调颜色。是否可以使两个链接具有不同的颜色?提前致谢!

附:如果可能,我不想使用库/框架。

【问题讨论】:

标签: ios objective-c hyperlink uitextview nsattributedstring


【解决方案1】:

是的,有可能,问题是 TextView 有一些预定义的链接属性。

要修复它,您只需以这种方式删除它们:

yourTextView.linkTextAttributes = [:]

使用这行代码,如果 textview 检测到链接,它将不会对其应用任何特殊属性。所以它会像往常一样工作,但不会改变链接的颜色。如果要更改颜色,则必须在添加属性时手动进行。

【讨论】:

    【解决方案2】:

    您需要分配您的文本视图的linkTextAttributes - 基本上您只需告诉您的文本视图识别链接并按照您指定的方式对它们进行样式化:

    目标-C

    textView.linkTextAttributes = @{
        NSForegroundColorAttributeName:[UIColor someColor],
        NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)
    };
    

    您还应该能够分配tintColor(因为UITextView 继承了与UIView 相同的行为,并将使用此颜色作为链接)。 但是如果在您的情况下由于某种原因这不起作用,则分配链接属性将是解决方案。


    如果您的意图是为每个链接设置两种不同颜色,那么您必须根据每个链接在字符串中出现的范围手动分配每个链接的属性。 p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 2015-02-28
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多