UITextView归属文本有2个链接,每个链接都有不同的颜色不起作用

时间:2017-12-10 17:12:25

标签: swift swift3 uitextview nsattributedstring

我想在属性字符串中显示2个链接,每个链接都有不同的颜色。我不明白该怎么做。它总是只设置一种颜色。我已经挣扎了几天,但仍然无法弄清楚如何让它发挥作用。有人知道吗?我可以设置两种颜色,但不能用于链接!所有链接都是相同的颜色。

这是我的整个实施:(更新)

  var checkIn = ""
  var friends = ""

//MARK: Change Name Color / Font / Add a second LABEL into the same label
    func setColorAndFontAttributesToNameAndCheckIn() {
        let nameSurname = "\(postAddSetup.nameSurname.text!)"
        checkIn = ""
        friends = ""

        if selectedFriends.count == 0 {
            print("we have no friends...")
            friends = ""
        } else if selectedFriends.count == 1 {
            print("we have only one friend...")
            friends = ""
            friends = " is with \(self.firstFriendToShow)"
        } else if selectedFriends.count > 1 {
            print("we have more than one friend...")
            friends = ""
            friends = " is with \(self.firstFriendToShow) and \(self.numberOfFriendsCount) more"
        }

        if checkIn == "" {
            checkIn = ""
        }

        var string = postAddSetup.nameSurname.text
        string = "\(nameSurname)\(friends)\(checkIn) "

        let attributedString = NSMutableAttributedString(string: string!)
        attributedString.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 14), range: (string! as NSString).range(of: nameSurname))

        attributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 13), range: (string! as NSString).range(of: checkIn))
        attributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 13), range: (string! as NSString).range(of: friends))

        attributedString.addLink("checkIn", linkColor: UIColor.darkGray, text: checkIn)
        attributedString.addLink("tagFriends", linkColor: UIColor.red, text: friends)

        //attributedString.addAttribute(NSLinkAttributeName, value: "checkIn", range: (string! as NSString).range(of: checkIn))
        //attributedString.addAttribute(NSLinkAttributeName, value: "tagFriends", range: (string! as NSString).range(of: friends))

        //postAddSetup.nameSurname.linkTextAttributes = [NSForegroundColorAttributeName:UIColor.redIWorkOut(), NSFontAttributeName: UIFont.systemFont(ofSize: 13)]

        //attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGray, range: (string! as NSString).range(of: checkIn))

        postAddSetup.nameSurname.attributedText = attributedString


        print("atribute: \(attributedString)")

    }


func string1Action() {
    print("action for string 1...")
}

func string2Action() {
    print("action for string 2...")
}

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {

    if URL.absoluteString == "string1" {
        string1Action()
    } else if URL.absoluteString == "string2" {
        string2Action()
    }
    return false
}

extension NSMutableAttributedString {
    func addLink(_ link: String, linkColor: UIColor, text: String) {
        let pattern = "(\(text))"
        let regex = try! NSRegularExpression(pattern: pattern,
                                             options: NSRegularExpression.Options(rawValue: 0))
        let matchResults = regex.matches(in: self.string,
                                         options: NSRegularExpression.MatchingOptions(rawValue: 0),
                                         range: NSRange(location: 0, length: self.string.characters.count))

        for result in matchResults {
            self.addAttribute(NSLinkAttributeName, value: link, range: result.rangeAt(0))
            self.addAttribute(NSForegroundColorAttributeName, value: linkColor, range: result.rangeAt(0))
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我在一个项目NSMutableAttributedString extension中使用了这个Article.

使用NSRegularExpression,您可以指定与链接文字范围相匹配的相应颜色:

扩展程序:

extension NSMutableAttributedString {
    func addLink(_ link: String, linkColor: UIColor, text: String) {
        let pattern = "(\(text))"
        let regex = try! NSRegularExpression(pattern: pattern,
                                              options: NSRegularExpression.Options(rawValue: 0))
        let matchResults = regex.matches(in: self.string,
                                options: NSRegularExpression.MatchingOptions(rawValue: 0),
                                range: NSRange(location: 0, length: self.string.characters.count))

        for result in matchResults {
            self.addAttribute(NSLinkAttributeName, value: link, range: result.rangeAt(0))
            self.addAttribute(NSForegroundColorAttributeName, value: linkColor, range: result.rangeAt(0))
        }
    }
}

修改

设置自定义UITextView类以使用此扩展并使用委托函数shouldInteractWith url可以模拟UITextView的超链接逻辑:

 class CustomTextView: UITextView {

    private let linksAttributes = [NSLinkAttributeName]

    override func awakeFromNib() {
        super.awakeFromNib()

        let tapGest = UITapGestureRecognizer(target: self, action: #selector(self.onTapAction))
        self.addGestureRecognizer(tapGest)
    }

    @objc private func onTapAction(_ tapGest: UITapGestureRecognizer) {
        let location = tapGest.location(in: self)
        let charIndex = self.layoutManager.characterIndex(for: location, in: self.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)

        if charIndex < self.textStorage.length {
            var range = NSMakeRange(0, 0)

            for linkAttribute in linksAttributes {
                if let link = self.attributedText.attribute(linkAttribute, at: charIndex, effectiveRange: &range) as? String {
                    guard let url = URL(string: link) else { return }
                    _ = self.delegate?.textView?(self, shouldInteractWith: url, in: range, interaction: .invokeDefaultAction)
                }
            }
        }
    }
}

使用方法:

attributedString.addLink(yourLinkUrl, linkColor: yourLinkColor, text: yourLinkText) let textView = CustomTextView() textView.attributedText = attributedString