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.
How can I solve it?
This is my 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";
}
对于 Swift,它很简单:
textView.linkTextAttributes = [.underlineStyle: 0]
Somehow ".underlineStyle: 0" didn't work for me. so i had to clear the Underline Color and it worked for me.
textView.linkTextAttributes = [.underlineStyle: 0, .underlineColor: UIColor.clear,]
It seems that the NSUnderlineStyle
in your UITextView's linkTextAttributes
gets overridden by CSS's link style. If you do not define a text-decoration
style for your HTML the UITextView
sets the underline style to styleSingle
because that is the default HTML style for a link.
To remove the underline you have to add a underline style for your HTML links. You could do that by adding the following CSS to the head
of the HTML:
<style type='text/css'>
a { text-decoration: none }
</style>
只需将下划线颜色设置为清晰颜色即可。
yourTextView.linkTextAttributes = @{NSUnderlineColorAttributeName: [UIColor clearColor]};
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];
Here is my code. I tried this & It's work for me. Please review them. May be it's helpful for you.
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];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.