Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link colors are not set #792

Closed
georgschuster opened this issue Jul 25, 2014 · 13 comments
Closed

Link colors are not set #792

georgschuster opened this issue Jul 25, 2014 · 13 comments

Comments

@georgschuster
Copy link

Hi,

I'm trying to use colors for links in an attributed string made of HTML.

A simplified version of what I'm doing is:

NSString *htmlString = @"<b>My Text</b><br><a href=\"http://example.com\">Example</a>";
NSData *htmlData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
DTCSSStylesheet *css = [[DTCSSStylesheet alloc] initWithStyleBlock:@"a{color:blue;}"];
NSDictionary *options = [NSDictionary dictionaryWithObjects:@[css, [NSNumber numberWithBool:NO], @"green"]
                                                forKeys:@[DTDefaultStyleSheet, DTDefaultLinkDecoration, DTDefaultLinkColor]];
return [[NSAttributedString alloc] initWithHTMLData:htmlData options:options documentAttributes:nil];

I can see the link in default color (black). While the DTDefaultLinkDecoration is working, the DTDefaultLinkColor shows no effect.
Also, setting the link color via the DTCSSStylesheet doesn't work. However, if i change the "a" to a "b", the bold text gets the right color.

It seems to me that no color operation on hrefs are working.

Am I missing somtehing obvious here?

@georgschuster
Copy link
Author

Seems that in my case, there was the attribute "CTForegroundColorFromContext" set to 1.

Removing that attribute by using

[as removeAttribute:@"CTForegroundColorFromContext" range:NSMakeRange(0, as.length)];

did the trick. Now the link colors are visible.

@pjechris
Copy link

Same issue and same trick worked for me

@odrobnik
Copy link
Collaborator

You are welcome to provide a fix. IIRC the CTForegroundColorFromContext is necessary to allow for hiding the links if you have link buttons instead of them being drawn. So the correct override color for the HREF needs to be set on the context before they are drawn.

@bmunge
Copy link

bmunge commented Jan 17, 2015

I'm having a similar issue but removing the CTForegroundColorFromContext isn't resolving the issue. Everything looks to be set correctly but the links are still appearing as blue and underlined. I've tried using the stylesheet and the link attributes to no avail. Here's my console output for the string in question:

(lldb) po mutableAttributedString
Terms, conditions and exclusions apply.{
    DTGUID = "313412B4-8BE7-44B6-9450-543A7710B668";
    DTLinkHighlightColor = "UIDeviceRGBColorSpace 0.0196078 0.423529 0.756863 1";
    NSColor = "UIDeviceRGBColorSpace 0.0196078 0.423529 0.756863 1";
    NSFont = "<UICTFont: 0x7f93f710e5c0> font-family: \"HelveticaNeue-Medium\"; font-weight: normal; font-style: normal; font-size: 9.00pt";
    NSLink = "http://www.someurl.com";
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, 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";
} 

Here's the code:

    NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObject:@(YES) forKey:DTUseiOS6Attributes]; // Attribute required to prevent crash

    if (font) {
        options[DTDefaultFontName] = font.fontName;
        options[DTDefaultFontSize] = @(font.pointSize);
        options[DTDefaultFontFamily] = font.familyName;
    }

    if (colorStyle) {
        options[DTDefaultTextColor] = colorStyle;
    }

    if (linkColor) {
        DTCSSStylesheet *css = [[DTCSSStylesheet alloc] initWithStyleBlock:[NSString stringWithFormat:@"a {color:%@;}", DTHexStringFromDTColor(linkColor)]];
        options[DTDefaultStyleSheet] = css;
        options[DTDefaultLinkColor] = linkColor;
        options[DTDefaultLinkHighlightColor] = linkColor;
        options[DTDefaultLinkDecoration] = @(NO);
    }
    NSAttributedString *result = [[NSAttributedString alloc] initWithHTMLData:[string dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil];
    NSMutableAttributedString *mutableAttributedString = [result mutableCopy];
    // Remove the CTForegroundColorFromContext attribute as it may suppress the link color
    [mutableAttributedString removeAttribute:@"CTForegroundColorFromContext" range:NSMakeRange(0, mutableAttributedString.length)];
    return [mutableAttributedString copy];

I really appreciate any help. Thanks!

@odrobnik
Copy link
Collaborator

@bmunge where are you trying to use such an attributed string? I found that UILabel forces the color to blue and underlined. You have to do the drawing yourself with NSLayoutManager if you want those to be different.

@bmunge
Copy link

bmunge commented Jan 17, 2015

Ah yes, I am using it in an UILabel. I was able to get it working by removing the NSLink attribute. It seems that as long as the NSLink attribute is in the dictionary, the UILabel will use the default link styling. Thanks for the quick response.

I'm not sure that this is ideal but since I'm using a tap gesture recognizer to trigger the "link" I was able to get away with removing that attribute.

@nin9tyfour
Copy link

This was helpful, fortunately a quick Google search of CTForegroundColorFromContext returned 4 results and this was the first :p. CTForegroundColorFromContext causes odd behaviour in conjunction with TTTAttributedLabel and will result in text selection and sizing settings to be ignored. Hopefully this can serve to help someone else. Thanks a lot @georgschuster!

@cerupcat
Copy link
Contributor

@bmunge @nin9tyfour Were you able to figure this out? I'm also not able to get the color to show up for TTTAttributedLabel even when removing CTForegroundColorFromContext.

@nin9tyfour
Copy link

@cerupcat I'm not too sure, removal of CTForegroundColorFromContext immediately restored all of the attributes in my case.

@ridvank
Copy link

ridvank commented Aug 5, 2015

@cerupcat did you try to set your tintColor?

@amol-c
Copy link
Contributor

amol-c commented Dec 21, 2015

Weird. I had to remove NSLink from the attributed string passed back from
DTAttributedTextContentViewDelegate

 func attributedTextContentView(attributedTextContentView: DTAttributedTextContentView!, viewForAttributedString string: NSAttributedString!, frame: CGRect) -> UIView!

I would expect the attributed string to be already formatted when i set the option DTDefaultLinkDecoration and DTDefaultLinkColor or am i missing something here?

@jklp
Copy link

jklp commented Mar 7, 2016

@cerupcat After removing the CTForegroundColourFromContext, you need to set the following on your TTTAttributedLabel with the colour you're after, e.g

self.tttAttrLabel.linkAttributes = [UIColor redColor];

@odrobnik
Copy link
Collaborator

Since there has not being any activity on this issue in several months we are closing it for now. If you are interested in providing a fix or solution you are encouraged to provide a pull request.

Please read the release notes for the latest 1.6.20 release. If you still require assistance, you can hire me to work on your issues for a reasonable fee.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants