2

I have a UITextView that contains a lot of text with a number at the end. I am trying to detect the number in the text view but unable to achieve that. I can only find the solution to find the link only.

I am using NSAttributedString to display the text.

.dataDetectorTypes does not seem to work. Can any one help me

I am trying like this.

let middleTextView: UITextView = {
        let tv = UITextView();
        let attributedText = NSMutableAttributedString(string: "Your NetCode will be sent to", attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 14)]);
        attributedText.append(NSAttributedString(string: "\n\nXXXX XXX 252", attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 16)]));
        attributedText.append(NSMutableAttributedString(string: "\n\n To update you mobile number call tel://13 2221", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 12)]));
//        let url = NSURL(string: "tel://\(1234)");
//        tv.text = url;
        tv.attributedText = attributedText;
        tv.textAlignment = .center
        tv.backgroundColor = UIColor.clear;
        tv.dataDetectorTypes = .phoneNumber;
        tv.isEditable = false;
        tv.isSelectable = false;
        tv.translatesAutoresizingMaskIntoConstraints = false;
        return tv;
    }();
Code Different
  • 90,614
  • 16
  • 144
  • 163
Jivan Bhandari
  • 860
  • 1
  • 10
  • 32

1 Answers1

5

I see that you are using Swift 4's NSAttributedStringKey so I added that tag to your question.

You must make the telephone URL manually and set isSelectable = true otherwise no interaction can happen from within the text view:

let middleTextView: UITextView = {
    let attributedText = NSMutableAttributedString(string: "Your NetCode will be sent to", attributes: [.font : UIFont.systemFont(ofSize: 14)])
    attributedText.append(NSAttributedString(string: "\n\nXXXX XXX 252", attributes: [.font: UIFont.boldSystemFont(ofSize: 16)]))
    attributedText.append(NSAttributedString(string: "\n\n To update you mobile number ", attributes: [.font: UIFont.systemFont(ofSize: 12)]))

    let phoneNumber = "13 2221"
    let phoneNumberAttributes: [NSAttributedStringKey: Any] = [
        .font: UIFont.systemFont(ofSize: 12),
        .foregroundColor: UIColor.blue,
        .link: URL(string: "tel://" + phoneNumber.replacingOccurrences(of: " ", with: ""))!,
    ]
    attributedText.append(NSAttributedString(string: phoneNumber, attributes: phoneNumberAttributes))

    tv.attributedText = attributedText
    tv.textAlignment = .center
    tv.backgroundColor = UIColor.clear
    tv.isEditable = false
    tv.isSelectable = true // you must set this to true
    return tv
}()
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • Thank you, It works as intended. The colour of telephone number is blue and foreground colour does not work. Do you know how can we change the foreground color to black. – Jivan Bhandari Jul 16 '17 at 12:56
  • .foregroundColor didn't work. But solved by using TextView.tintColor = UIColor.black – Jivan Bhandari Jul 16 '17 at 13:12
  • Interesting that `.foregroundColor` didn't work. In the olden days I must set the link attributes manually or it just showed up as regular text. Glad you found a solution on your own – Code Different Jul 16 '17 at 13:18