获取 IndexPath - UITextView 应该在 indexPath 处与 URL 交互
Get IndexPath for - UITextView should interact with URL at indexPath
我有一个集合视图。里面会显示用户名,如果他签到一个地方和和他在一起的朋友。我将用户名用作 TextView,并为 2 种类型的文本(朋友和签到)分配 URL。它的工作原理只有一个问题。
当用户点击 link 时,我无法找到获取 IndexPath 的方法。它将发送到 link 并触发函数,但我无法获取 indexPath。我到处都看了,但没有关于它的文档。永远欠你一点帮助,我为此苦苦挣扎。
这是使用户名显示 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
}
这就是我捕捉 link 的方式:
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
}
}
根据 Larme 给我的建议,我想到了这个,并且有效。
在 didSelectRow 中,我分配了一个点击手势,然后我使用链接创建了 NSAttributedString。在第二部分中,我在 collectionView 中检索手势点并获取 indexPath。现在将分配动作。因为第一次点击是在 String 而不是 Label 上,所以 indexPath 会被延迟分配。所以我用 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)
}
这是计算indexPath的函数(urlIndexPath只是一个变量):
@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!)")
}
最后我使用了 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)")
}
}
我有一个集合视图。里面会显示用户名,如果他签到一个地方和和他在一起的朋友。我将用户名用作 TextView,并为 2 种类型的文本(朋友和签到)分配 URL。它的工作原理只有一个问题。
当用户点击 link 时,我无法找到获取 IndexPath 的方法。它将发送到 link 并触发函数,但我无法获取 indexPath。我到处都看了,但没有关于它的文档。永远欠你一点帮助,我为此苦苦挣扎。
这是使用户名显示 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
}
这就是我捕捉 link 的方式:
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
}
}
根据 Larme 给我的建议,我想到了这个,并且有效。
在 didSelectRow 中,我分配了一个点击手势,然后我使用链接创建了 NSAttributedString。在第二部分中,我在 collectionView 中检索手势点并获取 indexPath。现在将分配动作。因为第一次点击是在 String 而不是 Label 上,所以 indexPath 会被延迟分配。所以我用 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)
}
这是计算indexPath的函数(urlIndexPath只是一个变量):
@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!)")
}
最后我使用了 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)")
}
}