2017-07-16 12 views
1

最後に数字のついた多くのテキストを含むUITextViewがあります。私はテキストビューで数字を検出しようとしていますが、それを達成することはできません。私はリンクを見つけるための解決策しか見つけることができません。番号をUiTextViewに挿入して番号を検出する

NSAttributedStringを使用してテキストを表示しています。

.dataDetectorTypesが機能していないようです。誰でも助けてくれる

私はこのようにしています。

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; 
    }(); 
+0

あなたが戻って取得するために何をしようとしていますか?電話番号は「13 2221」ですか? –

+0

はい、コールオプションでプロンプトが表示されるように13 2221をクリックします。 – Bikram

答えて

2

スウィフト4のNSAttributedStringKeyを使用していることがわかりましたので、そのタグを質問に追加しました。

手動で電話URLを作成し、そうでなければ相互作用は、テキストビュー内から起こることはできませんisSelectable = trueを設定する必要があります。

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 
}() 
+0

ありがとうございます、それは意図したとおりに動作します。電話番号の色は青色で、前景色は機能しません。フォアグラウンドの色をどのように黒に変えることができるか知っていますか? – Bikram

+0

.foregroundColorが機能しませんでした。しかし、TextView.tintColor = UIColor.blackを使って解決しました。 – Bikram

+0

興味深いことに、 '.foregroundColor'はうまくいきませんでした。昔、私は手動でリンク属性を設定する必要がありますか、それは通常のテキストとして表示されました。あなた自身で解決策を見つけてうれしい –

関連する問題