Skip to content
This repository

.linkAttributes not applied when inheriting label styles #175

Closed
mjhecht opened this Issue · 3 comments

3 participants

Michael Bill Williams Mattt Thompson
Michael

I'm in a category on TTTAttributedLabel, thus the assignment to self.

This works; I can cantrol the link attributes:

NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithString:str];

self.text = as;

self.linkAttributes = @{
    NSForegroundColorAttributeName: UIColor.redColor,
    NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)
};

for (MYHashtagInstance *h in hashtagInstances) {
    [self addLinkToURL:[NSURL URLWithString:Str(@"app:///whatever", h.text)] withRange:h.range];
}

This does not work; link attributes, if set to anything, always cause linked text to appear as black / default. If not set, linked text is the default (underlined blue):

[self setText:str afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
    return mutableAttributedString;
}];

self.linkAttributes = @{
    NSForegroundColorAttributeName: UIColor.redColor,
    NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)
};

for (MYHashtagInstance *h in hashtagInstances) {
    [self addLinkToURL:[NSURL URLWithString:Str(@"app:///whatever", h.text)] withRange:h.range];
}

I've experimented with setting link attributes at various places in the second instance, but links are always shown without any style.

I wonder if I'm doing something wrong, but this behavior seems strange/undocumented.

Bill Williams

Try replacing NSForegroundColorAttributeName with (__bridge NSString*)kCTForegroundColorAttributeName (and equivalent for underlines). It seems like that attribute, which is new in iOS 6 (along with UITextAttributeTextColor, added in iOS 5) aren’t supported by TTTAttributedLabel.

I wonder if @mattt would be open to a more broad pull request that switched over to using the attributes provided in <UIKit/UIStringDrawing.h> or <UIKit/NSAttributedString.h>? Using the Core Text stuff directly was necessary on iOS 4, but seems somewhat archaic at this point.

Michael

Thanks, looks like that would work for inheriting styles. I was able to get it to work by not inheriting and instead manually applying the style, before adding links:

NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithString:str];

[as addAttribute:NSForegroundColorAttributeName value:self.textColor range:NSMakeRange(0, [str length])];

self.text = as;

I know that in iOS 6 we can use attributed strings directly with labels, but I've been relying on this project for the link support it provides.

Mattt Thompson
Owner
mattt commented

@asmallteapot Following up: yes, I'd be interested in this if it were backwards-compatible with iOS 4.3.

Mattt Thompson mattt closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.