删除UITextView链接上的下划线

时间:2015-03-18 08:52:34

标签: ios uitextview

我正在尝试以下方法来格式化UITextView上的链接:

self.textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor], NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]};

问题是,链接颜色已更改为白色,但仍有下划线。

我该如何解决?

这是我的attributedString

{
    NSColor = "UIDeviceRGBColorSpace 0.294118 0.254902 0.211765 1";
    NSFont = "<UICTFont: 0x7fd75a196010> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 24.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 24, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
}url.com{
    CTForegroundColorFromContext = 1;
    DTGUID = "44B87A68-642A-41AA-8CBA-48531DB58C57";
    DTLinkHighlightColor = "UIDeviceRGBColorSpace 1 0 0 1";
    NSColor = "UIDeviceRGBColorSpace 0 0 0.933333 1";
    NSFont = "<UICTFont: 0x7fd75a196010> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 24.00pt";
    NSLink = "http://www.url.com";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 24, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
    NSUnderline = 1;
}
{
    NSFont = "<UICTFont: 0x7fd75a196010> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 24.00pt";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 24, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection -1, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0";
}

6 个答案:

答案 0 :(得分:3)

只需将下划线颜色设置为清晰的颜色。

yourTextView.linkTextAttributes = @{NSUnderlineColorAttributeName: [UIColor clearColor]};

答案 1 :(得分:1)

出于不明原因,如果我在NSUnderlineStyleAttributeName中添加linkTextAttributes无效。它应该将NSUnderline = 0添加到链接,而是添加NSUnderline = 1。因此,我找到的唯一解决方案是找到该属性并在找到时替换它。

self.textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};

NSMutableAttributedString *res = [self.textView.attributedText mutableCopy];
[res beginEditing];
[res enumerateAttribute:NSUnderlineStyleAttributeName
                inRange:NSMakeRange(0, res.length)
                options:0
             usingBlock:^(id value, NSRange range, BOOL *stop) {
                 if (value) {
                     [res addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleNone) range:range];
                 }
             }];
[res endEditing];

[self.textView setAttributedText:res];

答案 2 :(得分:1)

似乎您的UITextView NSUnderlineStyle中的linkTextAttributes被CSS的链接样式覆盖。如果您没有为HTML定义text-decoration样式,则UITextView会将下划线样式设置为styleSingle,因为这是链接的默认HTML样式。

要删除下划线,您必须为HTML链接添加下划线样式。您可以通过将以下CSS添加到HTML的head

来实现
<style type='text/css'>
   a { text-decoration: none }
</style>

答案 3 :(得分:1)

对于Swift 4.2,这很简单:

std::shared_ptr

答案 4 :(得分:0)

这是我的代码。我试过这个&amp;这对我有用。请查看它们。可能对你有帮助。

emailTV = [[UITextView alloc] initWithFrame:yourFrame];
emailTV.text = @"https://www.google.co.in/?gfe_rd=cr&ei=eUMJVZrQIsTV8gflkoHwAQ&gws_rd=ssl";
emailTV.textAlignment = NSTextAlignmentLeft;
emailTV.font = [UIFont fontWithName:@"HelveticaNeue-Medium"size:14.0+fontSizeDiff];
emailTV.selectable = NO;
emailTV.scrollEnabled = NO;
emailTV.editable = NO;
emailTV.backgroundColor = [UIColor clearColor];
emailTV.dataDetectorTypes = UIDataDetectorTypeNone;
emailTV.textColor = [UIColor blueColor];
[cellVI addSubview: emailTV];

答案 5 :(得分:0)

以某种方式,“。underlineStyle:0”对我不起作用。所以我必须清除下划线颜色,它对我有用。

textView.linkTextAttributes = [.underlineStyle: 0, .underlineColor: UIColor.clear,]
相关问题