删除 UITextView 上的下划线 link
Remove underline on UITextView link
我正在尝试使用以下方法在 UITextView 上格式化 links:
self.textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor], NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]};
问题是,link 颜色已更改为白色,但仍带有下划线。
我该如何解决?
这是我的 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";
}
这是我的代码。我试过了 & 它对我有用。请检查它们。可能对你有帮助。
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];
出于未知原因,如果我在 linkTextAttributes
中添加 NSUnderlineStyleAttributeName
无效。它应该将 NSUnderline = 0
添加到 link,而不是添加 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];
您的 UITextView linkTextAttributes
中的 NSUnderlineStyle
似乎被 CSS 的 link 样式覆盖。如果您没有为 HTML 定义 text-decoration
样式,则 UITextView
会将下划线样式设置为 styleSingle
,因为这是 [=] 的默认 HTML 样式27=].
要删除下划线,您必须为 HTML link 添加下划线样式。您可以通过将以下 CSS 添加到 HTML 的 head
来实现:
<style type='text/css'>
a { text-decoration: none }
</style>
只需将下划线颜色设置为清晰颜色即可。
yourTextView.linkTextAttributes = @{NSUnderlineColorAttributeName: [UIColor clearColor]};
对于Swift,它只是:
textView.linkTextAttributes = [.underlineStyle: 0]
不知为何“.underlineStyle: 0”对我不起作用。所以我必须清除下划线颜色,它对我有用。
textView.linkTextAttributes = [.underlineStyle: 0, .underlineColor: UIColor.clear,]
我正在尝试使用以下方法在 UITextView 上格式化 links:
self.textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor], NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]};
问题是,link 颜色已更改为白色,但仍带有下划线。
我该如何解决?
这是我的 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";
}
这是我的代码。我试过了 & 它对我有用。请检查它们。可能对你有帮助。
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];
出于未知原因,如果我在 linkTextAttributes
中添加 NSUnderlineStyleAttributeName
无效。它应该将 NSUnderline = 0
添加到 link,而不是添加 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];
您的 UITextView linkTextAttributes
中的 NSUnderlineStyle
似乎被 CSS 的 link 样式覆盖。如果您没有为 HTML 定义 text-decoration
样式,则 UITextView
会将下划线样式设置为 styleSingle
,因为这是 [=] 的默认 HTML 样式27=].
要删除下划线,您必须为 HTML link 添加下划线样式。您可以通过将以下 CSS 添加到 HTML 的 head
来实现:
<style type='text/css'>
a { text-decoration: none }
</style>
只需将下划线颜色设置为清晰颜色即可。
yourTextView.linkTextAttributes = @{NSUnderlineColorAttributeName: [UIColor clearColor]};
对于Swift,它只是:
textView.linkTextAttributes = [.underlineStyle: 0]
不知为何“.underlineStyle: 0”对我不起作用。所以我必须清除下划线颜色,它对我有用。
textView.linkTextAttributes = [.underlineStyle: 0, .underlineColor: UIColor.clear,]