如何使 NSAttributedString 链接可点击
我需要将数字显示为来自字符串输入(包含字母和数字)的可点击链接,点击该号码后,它应该打开一个 iOS 呼叫拨号器并拨打该号码。
下面是我目前所尝试的,为了使号码看起来像一个可点击的号码,并将所有文本保留为正常的默认文本,下面有黑色的 formattedText var 已被定义,
var formattedText: NSAttributedString{
let message = "Hey you can dial up {88 22 333} number for further assistance"
let linkRange = (message as NSString).range(of: fetchNumber) //fetchNumber method returns the content residing inside flower braces..
guard linkRange.location != NSNotFound else {return nil}
let finalString = NSMutableAttributedString(string: message, attributes:[.font: UIFont.scaledFont(from: .baseText16), foregroundColor: .black])
finalString.addAttributes([.foregroundColor: .blue], range: linkRange)
return finalString
}
上述函数的输出是:嘿,你可以拨号 < code>88 22 333 号码以获得进一步帮助
并使号码可点击,我尝试使用此属性文本输入到 UITextView 并启用 dataDetectorType 属性,但它对我不起作用:( 有没有什么办法可以让 UILabel 本身或任何类型的 UIKit 元素实现这一点,提前感谢任何建议,谢谢
我尝试使用此属性文本输入到 UITextView 并启用 dataDetectorType 属性,但它对我不起作用:( 有没有什么办法可以让 UILabel 本身或任何类型的 UIKit 元素实现这一点,任何建议都非常感谢,谢谢
I have a requirement to display the numbers as clickable link from a string input(contains alphabets and numerals) and on tap of that number it should open an iOS call dialler and should dials that number.
Below is what I have tried at the moment, to make the number to look like a tappable one and rest all text as normal default text with black color below formattedText var has been defined,
var formattedText: NSAttributedString{
let message = "Hey you can dial up {88 22 333} number for further assistance"
let linkRange = (message as NSString).range(of: fetchNumber) //fetchNumber method returns the content residing inside flower braces..
guard linkRange.location != NSNotFound else {return nil}
let finalString = NSMutableAttributedString(string: message, attributes:[.font: UIFont.scaledFont(from: .baseText16), foregroundColor: .black])
finalString.addAttributes([.foregroundColor: .blue], range: linkRange)
return finalString
}
output of above function is: Hey you can dial up 88 22 333
number for further assistance
and to make the number tappable I have tried using this attributed text input to UITextView with dataDetectorType property enabled but it is not working for me :(
Is there any way to make it happen with UILabel itself or any sort of UIKit elements, any suggestions is much appreciated in advance, thanks
I have tried using this attributed text input to UITextView with dataDetectorType property enabled but it is not working for me :(
Is there any way to make it happen with UILabel itself or any sort of UIKit elements, any suggestions is much appreciated in advance, thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要在全球声明手机号码。
为文本设置属性字符串,并创建可敲击标签。
之后,声明龙头手势的方法。
您需要uitapgestureRecognize伸展来检测点击事件。
First of all you need to declare mobile number globally.
Set attributed string for your text and create tappable label.
After that declare method of tap gesture.
You need UITapGestureRecognizer extension for detect tap events.