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

Invalid range selection for set up links #498

Closed
hemangshah opened this issue Feb 6, 2015 · 14 comments
Closed

Invalid range selection for set up links #498

hemangshah opened this issue Feb 6, 2015 · 14 comments

Comments

@hemangshah
Copy link

I'm trying to set links on a string with specific range.

Here's the example:
My string looks like this:

H started tic tac toe event

(which is joining of these three strings, H + started + tic tac toe event)

I'm setting links on H and tic tac toe event. With following range.

2015-02-06 11:40:34.210 Event[5968:38268] {0, 2} --> This is for first string i.e. H
2015-02-06 11:40:35.305 Event[5968:38268] {11, 17} --> This is for last string i.e.tic tac toe event

However when I touch on it, - (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url would call even if I touched on started word on the label. It should only call if I'll tap on H or tic tac toe event.

What's wrong? Please help.

@getaaron
Copy link
Contributor

getaaron commented Feb 6, 2015

This might be if you're touxhing close to the link. If you're within 15 points or so of a link, it'll select.

@jhersh
Copy link
Contributor

jhersh commented Feb 6, 2015

@getaaron is correct; there is a ~15pt tolerance around links to match UIWebView's measured link tolerance. This is relatively new and might still need some adjustment or customization.

@hemangshah
Copy link
Author

@jhersh @getaaron , Ok, but in my case I would figure out that there were only two links created
first is for text, Hemang started
second is for text, tic tac toe event
and the main thing is that, I've not set link for text, started
as per you, ~15pt tolerance around links, but I think this is more than ~15pt?
What you guys say?

@hemangshah hemangshah reopened this Feb 7, 2015
@jhersh
Copy link
Contributor

jhersh commented Feb 8, 2015

Does your label have link styling? If not, can you add link styling and visually verify that the word started is not hyperlinked? Where in the displayed area of the word started do the links (not) fire?

@hemangshah
Copy link
Author

@jhersh , Yes, I've applied styling to the links, I've removed underline from it and also changed text color for the link. And yes, as I've specified in my question, the word "started" is also in link, I mean it calls the delegate.

@jhersh
Copy link
Contributor

jhersh commented Feb 8, 2015

I'm not sure I am following you. Can you verify the word started is NOT visually styled as a link while the words surrounding it ARE visually styled as links?

@hemangshah
Copy link
Author

Yes, in this case entire string is become a link.

@getaaron
Copy link
Contributor

Can you upload a sample project that demonstrates the issue?

@jhersh
Copy link
Contributor

jhersh commented Feb 11, 2015

Sorry, still not sure I am following you. I understand that tapping the word started in your label is activating a link.

Which of these two looks most like your label?

A:

Hemang started tic tac toe event

B:

Hemang started tic tac toe event

@hemangshah
Copy link
Author

@jhersh, I want exactly A string but it gives me string with links like this.
Hemang started tic tac toe event

@getaaron, I'll try to upload a demo with this.

@jhersh
Copy link
Contributor

jhersh commented Feb 11, 2015

@hemangshah We understand that tapping every word acts as a hyperlink. I am still trying to understand if the word started in your label is VISUALLY STYLED AS A LINK.

@hemangshah
Copy link
Author

Let me share some code with you.

- (TTTAttributedLabel *) addTTTAttributedLabelInView:(UIView *)view withFrame:(CGRect)frame {
    TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:frame];
    label.delegate = self;
    label.numberOfLines = 2;
    label.userInteractionEnabled = YES;
    label.minimumScaleFactor = 10.0f/14.0f;
    label.clipsToBounds = YES;
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.linkAttributes = @{(id)kCTForegroundColorAttributeName:[UIColor colorWithRed:108/255 green:108/255 blue:110/255 alpha:1],(id)kCTUnderlineStyleAttributeName:[NSNumber numberWithInt:kCTUnderlineStyleNone]};
    label.activeLinkAttributes = nil;
    label.inactiveLinkAttributes = nil;
    [view addSubview:label];
    return label;
}

- (NSAttributedString *) addAttributesInLabel:(TTTAttributedLabel *)label {

    NSString *one= @"Hemang";
    NSString *two = @"started";
    NSString *three = @"tic tac toe event";

    NSString *newString = [NSString stringWithFormat:@"%@ %@ %@",one, two, three];

    //Adding links on specific range
    [label addLinkToURL:[NSURL URLWithString:@"http://google.com"] withRange:[newString rangeOfString:one]];
    [label addLinkToURL:[NSURL URLWithString:@"http://google.com"] withRange:[newString rangeOfString:three]];

    return [[NSMutableAttributedString alloc] initWithString:newString];
}

Let me know if you need to know more.

@hemangshah
Copy link
Author

screen shot 2015-02-11 at 11 46 25 am This is the output of the label. @jhersh @getaaron

And, @jhersh, commented, I was wrong. Now its linked at two places.
1st link on -> Hemang
2nd link on -> started tic tac toe event

@hemangshah hemangshah changed the title Invalid range selection for setting links on TTTAttributedLabel Invalid range selection for set up links Feb 11, 2015
@jhersh
Copy link
Contributor

jhersh commented Feb 11, 2015

And, @jhersh, commented, I was wrong. Now its linked at two places.
1st link on -> Hemang
2nd link on -> started tic tac toe event

You have added a link to every word in your label. It sounds to me like your label is working correctly.

I also tried your sample code. It does not actually assign any text to the label, so I added some to match your code; it adds a link to every word except for started in the label. I was able to fire a link only by tapping at the outer left or right edges of the word started. Anywhere else within that word did not fire a link.

We probably still need to tune the link hit testing from #480 and #481, but I don't really see any problems here.

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

3 participants