首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取URL - UITextView应与IndexPath处的indexPath交互

获取URL - UITextView应与IndexPath处的indexPath交互
EN

Stack Overflow用户
提问于 2019-04-05 03:59:58
回答 1查看 115关注 0票数 0

我有一个集合视图。里面将显示用户名,以及他签到的地方和与他在一起的朋友。我使用用户名作为一个TextView,我正在为2种类型的文本(朋友和checkIn)分配网址。它就像它应该的那样工作,只有一个问题。

当用户点击链接时,我想不出一种方法来获取IndexPath。它将发送到链接并触发函数,但我无法获取indexPath。我到处都找过了,但没有相关的文档。永远欠你一点人情,因为我一直在为这件事而挣扎。

这是让username显示两个链接的函数:

代码语言:javascript
复制
  //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
    }

这就是我捕捉链接的方法:

代码语言:javascript
复制
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
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-06 04:16:09

基于Larme给我的建议,我想出了这个,并且它起作用了。

在didSelectRow中,我分配了一个点击手势,然后我制作了带有链接的NSAttributedString。在第二部分中,我将检索collectionView中的手势点并获取indexPath。现在将分配操作。因为第一次单击是在字符串上,而不是在标签上,所以indexPath将被分配一个延迟。因此,我推迟了与DispatchQueue的任务:

代码语言:javascript
复制
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只是一个变量):

代码语言:javascript
复制
@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:

代码语言:javascript
复制
//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)")
        }

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55524009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档