Solución:
Colocar isEditable = false
o la vista de texto entrará en modo de edición de texto cuando el usuario la toque.
Swift 4 y posterior
let attributedString = NSMutableAttributedString(string: "Just click here to register")
let url = URL(string: "https://www.apple.com")!
// Set the 'click here' substring to be the link
attributedString.setAttributes([.link: url], range: NSMakeRange(5, 10))
self.textView.attributedText = attributedString
self.textView.isUserInteractionEnabled = true
self.textView.isEditable = false
// Set how links should appear: blue and underlined
self.textView.linkTextAttributes = [
.foregroundColor: UIColor.blue,
.underlineStyle: NSUnderlineStyle.single.rawValue
]
Si desea utilizar varios hipervínculos, puede utilizar esta alternativa para Rápido 5
extension UITextView {
func addHyperLinksToText(originalText: String, hyperLinks: [String: String]) {
let style = NSMutableParagraphStyle()
style.alignment = .left
let attributedOriginalText = NSMutableAttributedString(string: originalText)
for (hyperLink, urlString) in hyperLinks {
let linkRange = attributedOriginalText.mutableString.range(of: hyperLink)
let fullRange = NSRange(location: 0, length: attributedOriginalText.length)
attributedOriginalText.addAttribute(NSAttributedString.Key.link, value: urlString, range: linkRange)
attributedOriginalText.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: fullRange)
attributedOriginalText.addAttribute(NSAttributedString.Key.font, value: YourFont, range: fullRange)
}
self.linkTextAttributes = [
NSAttributedString.Key.foregroundColor: YourColor,
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,
]
self.attributedText = attributedOriginalText
}
}
Uso:
yourTextView. addHyperLinksToText(originalText: "Testing hyperlinks here and there", hyperLinks: ["here": "someUrl1", "there": "someUrl2"])
La misma solución para Swift 3 usando extensiones:
A. Agregar extensión –
extension UITextView {
func hyperLink(originalText: String, hyperLink: String, urlString: String) {
let style = NSMutableParagraphStyle()
style.alignment = .center
let attributedOriginalText = NSMutableAttributedString(string: originalText)
let linkRange = attributedOriginalText.mutableString.range(of: hyperLink)
let fullRange = NSMakeRange(0, attributedOriginalText.length)
attributedOriginalText.addAttribute(NSLinkAttributeName, value: urlString, range: linkRange)
attributedOriginalText.addAttribute(NSParagraphStyleAttributeName, value: style, range: fullRange)
attributedOriginalText.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 10), range: fullRange)
self.linkTextAttributes = [
NSForegroundColorAttributeName: UIConfig.primaryColour,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue,
]
self.attributedText = attributedOriginalText
}
}
B. Agregar URL de enlace – let linkUrl = "https://www.my_website.com"
C. Implementar UITextViewDelegate
en su ViewController así:
class MyViewController: UIViewController, UITextViewDelegate {
}
D. Agregar método de delegado para manejar eventos de tap –
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
if (URL.absoluteString == linkUrl) {
UIApplication.shared.openURL(URL)
}
return false
}
}
E. Y finalmente, cosas que debe asegurarse para su UITextView
bajo el inspector de atributos –
- Comportamiento: Editable está APAGADO y Seleccionable está ENCENDIDO.
- Detectores de datos: el enlace está encendido.
Uso –
textView.hyperLink(originalText: "To find out more please visit our website", hyperLink: "website", urlString: linkUrl)
¡Salud y codificación feliz!
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)