I have a collection view. Inside the user name will be displayed, along with that if he checked in to a place and friends that are with him. I am using the username as a TextView, and I am assigning URL's for 2 types of text (friends and checkIn). It works as it should with only one problem.
I cannot figure out a way to get the IndexPath when the user taps a link. It will send to the link and triggers the function but I can't get the indexPath. I looked everywhere but no documentation about it. Forever in your debt for a bit of help cos, I've been struggling a lot with this.
This is the function that makes username show the 2 links:
//GET THE INDEX PATH FOR ASSIGNMENT TO THE LINKS ??
func assignNameFriendsAndCheckIn(name: String, checkIn: String, friends: String, cellName: UITextView) {
let nameSurname = name
let checkIn = checkIn
var string = name
let friendsString = friends
string = "\(nameSurname)\(checkIn)\(friendsString)"
let attributedString = NSMutableAttributedString(string: string)
attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: 14), range: (string as NSString).range(of: nameSurname))
attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 11), range: (string as NSString).range(of: checkIn))
attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 11), range: (string as NSString).range(of: friendsString))
attributedString.addAttribute(NSAttributedString.Key.link, value: "checkIn", range: (string as NSString).range(of: checkIn))
cellName.linkTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 11)]
attributedString.addAttribute(NSAttributedString.Key.link, value: "friends", range: (string as NSString).range(of: friendsString))
cellName.linkTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 11)]
cellName.attributedText = attributedString
}
And this is how I catch the links:
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if URL.absoluteString == "checkIn" {
print("Check In")
return true
} else if URL.absoluteString == "friends" {
print("Friends")
return true
} else {
print("No Urls set")
return false
}
}
Based on the suggestion Larme gave me I came up with this, and it works.
In didSelectRow, I am assigning a tap gesture and then I make the NSAttributedString with the links. In the second part, I am retrieving the point of gesture in collectionView and get the indexPath. Now the actions will be assigned. Because the first click will be on the String and not the Label the indexPath will be assigned with a delay. So I am delaying the assignment with DispatchQueue :
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
cell.nameSurname.delegate = self
................................................
cell.nameSurname.isUserInteractionEnabled = true
cell.nameSurname.tag = (indexPath.section * 100) + indexPath.item
let tapName : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTapName))
cell.nameSurname.addGestureRecognizer(tapName)
tapName.delegate = self
assignNameLocationAndCheckIn(name: nameSurname, checkIn: checkIn, city: city, date: date, friends: friendsString, cellName: cell.nameSurname, postID: cell.postID)
}
This is the function to calculate the indexPath (urlIndexPath is just a varaible):
@objc func didTapName(sender: UITapGestureRecognizer) {
let pointInCollectionView = sender.location(in: collectionView)
let indexPath = collectionView?.indexPathForItem(at: pointInCollectionView)
urlIndexPath = indexPath!.item
print("Name was tapped: \(indexPath!.item) : \(posts[(indexPath?.row)!].postID!)")
}
And finally I am using the indexPath:
//MARK:
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if URL.absoluteString == "checkIn" {
print("Check In")
checkInText()
return true
} else if URL.absoluteString == "friends" {
print("Friends")
tagFriends()
return true
} else {
print("No Urls set")
return false
}
}
//MARK:
func checkInText() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
print("Send to: \(self.urlIndexPath)")
}
}
//MARK:
func tagFriends() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
print("Send to Friends: \(self.urlIndexPath)")
}
}