Nantes icon indicating copy to clipboard operation
Nantes copied to clipboard

Pass a `linkTappedBlock` handler when adding multiple links to a single label

Open PatrickDotStar opened this issue 5 years ago • 5 comments

This is necessary because the linkTappedBlock can't be set after a link got created using addLink(to:URL) method since the Link DTO is a struct.

It may sound pointless because we already have to pass an URL which then calls a delegate method when being tapped but we have a case where we add multiple links to a single label and each link press is handled differently.

PatrickDotStar avatar Jul 04 '19 09:07 PatrickDotStar

@PatrickDotStar I'm not 100% sure I understand what you're trying to do with your code. Do you have an example you can post in here that will help me understand it?

chansen22 avatar Jul 12 '19 01:07 chansen22

Sure. we have something like this which will get called in a for loop

// NantesLabel attributedString is something like = "one, two, three, four"
// We then want each word separated by ',' to have its own link tapped handler
// Since we only want one NantesLabel instead of adding multiple ones separated by ',' we have to do it like this

public class func addLink(to url: URL, range: NSRange, label: NantesLabel, linkTappedHandler: (() -> Void)? = nil) {
	if let linkTappedHandler = linkTappedHandler {
		label.addLink(to: url, withRange: range, linkTappedBlock: { _, _ in
			linkTappedHandler()
		})
	}
	else {
		label.addLink(to: url, withRange: range)
	}
}

PatrickDotStar avatar Jul 22 '19 08:07 PatrickDotStar

Sorry it's taken me so long to get back to you!

chansen22 avatar Jul 27 '19 17:07 chansen22

LGTM @namolnad do you have any time to check it?

chansen22 avatar Jul 29 '19 18:07 chansen22

thanks @PatrickDotStar sounds like you're planning to add unit tests. will merge when they're good to go.

ngoleo avatar Jul 29 '19 19:07 ngoleo