I need to implement clickable hashtags in UILabel. The conditions are as follows: 1. UILabel is implemented through a storyboard, not programmatically (most of the githabe libraries use their own UILabel implementation programmatically and define known sizes there) 2. Swift 2

All the examples that I found in Google do not satisfy these two conditions. That language is not a swift or third swift, the UILabel or TextView is not programmed from a storyboard, but programmatically. And my UI is built on autolayut, and these are table cells, with different heights.

  • find library on swift 3 and rewrite to swift 2? - Max Mikheyenko
  • Frankly, for me it is complicated. I tried to redo one. There, after conversion, there are 30 errors. - cheerful_weasel
  • one
    What answer format do you expect? - Max Mikheyenko
  • or here's an option: find the library on swift3, and look at the githabe kommaty and find when it was converted to swift3, and take one commit before this - Max Mikheyenko
  • @MaxMikheyenko Well, I thought maybe someone came across this and tells the library under swift 2. But I have already found a way out, I am writing a method myself. - cheerful_weasel

1 answer 1

Since there were no ready-made solutions satisfying all my requests, I had to write the method itself.

enter image description here

Maybe someone will come in handy.

import UIKit class ViewController: UIViewController { @IBOutlet weak var TestTextView: UITextView! @IBOutlet weak var edittext: UITextView! @IBAction func buttonPressed(sender: UIButton) { TestTextView.attributedText = getColoredText(edittext.text) } override func viewDidLoad() { super.viewDidLoad() //НастраиваСм распознаваниС Π½Π°ΠΆΠ°Ρ‚ΠΈΠΉ TestTextView.userInteractionEnabled = true let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.textViewTap(_:))) TestTextView.addGestureRecognizer(tapGesture) //Π Π°ΡΠΊΡ€Π°ΡˆΠΈΠ²Π°Π΅ΠΌ тСкст TestTextView.attributedText = getColoredText(TestTextView.text) } let hashtagCalloutColor = UIColor(colorLiteralRed: 0, green: 0.73, blue: 0.66, alpha: 1) //00BAA9 func getColoredText(text:String) -> NSMutableAttributedString{ let string = NSMutableAttributedString(string: text) //Находим всС Ρ…Π΅ΡˆΡ‚Π΅Π³ΠΈ ΠΈ упоминания ΠΈ красим ΠΈΡ… Π² Π½ΡƒΠΆΠ½Ρ‹ΠΉ Ρ†Π²Π΅Ρ‚ let pat = "[#]\\w+|[@]\\w+" let regex = try! NSRegularExpression(pattern: pat, options: []) let matches = regex.matchesInString(string.string, options: [], range: NSRange(location: 0, length: string.length)) //Π—Π΄Π΅ΡΡŒ ΠΌΡ‹ ΠΏΡ€ΠΎΡ…ΠΎΠ΄ΠΈΠΌ ΠΏΠΎ всСм Π½Π°ΠΉΠ΄Π΅Π½Ρ‹ΠΌ словам ΠΈ красим ΠΈΡ… if matches.count > 0 { for match in matches { string.addAttribute(NSForegroundColorAttributeName, value: hashtagCalloutColor, range: match.range) } } return string } func getWordForIndex(string string: String, index: Int) -> String { var startInd = index var endInd = 0 let stringLngt = string.characters.count if stringLngt == index + 1 {endInd = index} else if stringLngt > index + 1 { endInd = index for i in index + 2 ... stringLngt { let x = i - 1 let charctr = string[string.startIndex.advancedBy(x)] if charctr != " " && charctr != "," && charctr != "\n" && i != stringLngt {endInd += 1} else { endInd = x break } } } if index > 0 { for i in 1 ... index { let x = index - i let charctr = string[string.startIndex.advancedBy(x)] if charctr != " " && charctr != "\n" {startInd -= 1} else {break} } } if endInd < stringLngt - 1 { return string.substringWithRange(string.startIndex.advancedBy(startInd)..<string.startIndex.advancedBy(endInd)) } else { return string.substringWithRange(string.startIndex.advancedBy(startInd)..<string.endIndex) } } func textViewTap(gestureRecognizer: UIGestureRecognizer) { let myTextView = gestureRecognizer.view as! UITextView let layoutManager = myTextView.layoutManager // location of tap in myTextView coordinates and taking the inset into account var location = gestureRecognizer.locationInView(myTextView) //.location(in: myTextView) location.x -= myTextView.textContainerInset.left location.y -= myTextView.textContainerInset.top // character index at tap location let characterIndex = layoutManager.characterIndexForPoint(location, inTextContainer: myTextView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil) let word = getWordForIndex(string: myTextView.text, index: characterIndex) if word.hasPrefix("#") { print("hashtag = \(word)") } if word.hasPrefix("@") { print("mention = \(word)") } } }