Search code examples
iosswiftuitextviewarabic

Links not working in UITextView Attributed String when language is Arabic


We have implemented a code to add a link to the whole attributed text inside a UITextView. 

The strings we are loading are:

English String: "Click Here To View", Arabic String:  "انقر هنا للعرض"

func setupTextView() {
  let mutableAttributedString = NSMutableAttributedString(string: "")
  let attributedString = NSAttributedString(string: {string value}, attributes: 
  [NSAttributedString.Key.foregroundColor:Colors.gray_3_748499])
  mutableAttributedString.append(attributedString)
  mutableAttributedString.addAttribute(NSAttributedString.Key.link, value: 
    "https://www.google.com", range: NSMakeRange(0, self.attributedString.length))

  self.textView.attributedText = mutableAttributedString
}

Result:We can see that for all other required languages, on clicking the text, the delegate  textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) is called but not for Arabic.

Looking forward for valuable suggestions.


Solution

  • I got the issue. I have been overriding the

    func point(inside point: CGPoint, with event: UIEvent?) -> Bool

    method to ignore the taps anywhere but not the links in a textview.

    I was using a UITextLayoutDirection as left inside this method. I had to change it according to the alignment for different languages. That is for Right to Left languages, I will use the UITextLayoutDirection.Right.

    Thanks