删除 UITextView 链接上的下划线 [英] Remove underline on UITextView link
本文介绍了删除 UITextView 链接上的下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下方法来格式化 UITextView 上的链接:
I'm trying the following to format links on UITextView:
self.textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor], NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]};
问题在于,链接颜色已更改为白色,但仍带有下划线.
The issue is that, the link color was changed to white, but it's still underlined.
我该如何解决?
这是我的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";
}
推荐答案
由于未知原因,如果我在 linkTextAttributes
中添加 NSUnderlineStyleAttributeName
不起作用.它应该添加 NSUnderline = 0
到链接,而不是添加 NSUnderline = 1
.因此,我找到的唯一解决方案是找到该属性并在找到时替换它.
For an unknow reason, if I add the NSUnderlineStyleAttributeName
in linkTextAttributes
didn't work. It supposed to add NSUnderline = 0
to link, instead it add NSUnderline = 1
. So, the only solution I found is to locate that attribute and replace it when found.
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];
这篇关于删除 UITextView 链接上的下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文