将数字插入 UiTextView 并检测该数字
Insert a number into a UiTextView and detect the number
我有一个 UITextView,其中包含大量文本,末尾有一个数字。我正在尝试检测文本视图中的数字,但无法实现。我只能找到 link 的解决方案。
我正在使用 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;
}();
我看到您使用的是 Swift 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
}()
我有一个 UITextView,其中包含大量文本,末尾有一个数字。我正在尝试检测文本视图中的数字,但无法实现。我只能找到 link 的解决方案。
我正在使用 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;
}();
我看到您使用的是 Swift 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
}()