UITextView with hyperlink text

后端 未结 10 804
抹茶落季
抹茶落季 2020-11-27 19:23

With a non-editable UITextView, I would like to embed text like this in iOS9+:

Just click here to register

I can create a functi

10条回答
  •  独厮守ぢ
    2020-11-27 20:02

    You could use this simple method to add a hyperlink to any set of characters starting with tag

    func addLink(forString string : NSMutableAttributedString
            ,baseURL : String
            ,tag : String){
            let array = string.string.replacingOccurrences(of: "\n", with: " ").components(separatedBy: " ")
            let filterArray = array.filter { (string) -> Bool in
                return string.contains(tag)
            }
            for element in filterArray {
                let removedHashtag = element.replacingOccurrences(of: tag, with: "")
                let url = baseURL + removedHashtag
                let range = NSString.init(string: (string.string)).range(of: element)
                string.addAttributes([NSAttributedStringKey.link : url.replacingOccurrences(of: " ", with: "")], range: range)
            }
        }
    

提交回复
热议问题