[ios]删除 UITextView 链接的下划线

标签: UITextView ios
发布时间: 2015/7/17 1:41:34
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想在 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";
}

解决方法 1:

由于未知的原因,如果我添加 NSUnderlineStyleAttributeNamelinkTextAttributes 不工作。它应该添加 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];
官方微信
官方QQ群
31647020