UITextView with one word as clickable like link also disfferent color and size iOS swift
Solution:
You can achieve that by using attributed text for text with different color and size.Also it will be underlined.
You can achieve the link preview by adding gesture recogniser.
let attributedString = NSMutableAttributedString(string: lbl.text)
// Add a hyperlink to the attributed string
attributedString.addAttribute(.link, value: "link", range: NSRange(location: 36, length: 5))
attributedString.addAttribute(NSAttributedString.Key.underlineStyle,value: NSUnderlineStyle.single.rawValue,range: NSRange(location: 36, length: 5))
// Add a tap gesture recognizer to the UITextView
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
lbl.addGestureRecognizer(tapGesture)
// Assign the attributed string to the UITextView
lbl.attributedText = attributedString
// Set the delegate to handle the hyperlink tap
lbl.delegate = self
// Make the UITextView clickable
lbl.isSelectable = true
lbl.isEditable = false
Comments
Post a Comment