UITextView with hyperlink text
Set isEditable = false
or the text view will go into text-editing mode when user taps on it.
Swift 4 and later
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
]
UITextView with Term and Privacy Hyperlink open in different UIViewController
You can use the following UITextView delegate Method and Attributed string Tested on swift 5.1 :
let attributedString = NSMutableAttributedString(string: "By continueing you agree terms and conditions and the privacy policy")
attributedString.addAttribute(.link, value: "terms://termsofCondition", range: (attributedString.string as NSString).range(of: "terms and conditions"))
attributedString.addAttribute(.link, value: "privacy://privacypolicy", range: (attributedString.string as NSString).range(of: "privacy policy"))
textView.linkTextAttributes = [ NSAttributedString.Key.foregroundColor: UIColor.blue]
textView.attributedText = attributedString
textView.delegate = self
textView.isSelectable = true
textView.isEditable = false
textView.delaysContentTouches = false
textView.isScrollEnabled = false
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if URL.scheme == "terms" {
//push view controller 1
return false
} else if URL.scheme == "privacy"{
// pushViewcontroller 2
return false
}
return true
// let the system open this URL
}
The UITextView call this function if the user taps or longPresses the URL link. Implementation of this method is optional. By default, the UITextview opens those applications which are responsible for handling the URL type and pass them the URL. You can use this method to trigger an alternative action
Trigger hyper link text click and and normal text click in UITextView
A possible solution is to add a "fake URL" (like a "internal URL Scheme look alike) where there is no link attribute:
attributedText.enumerateAttribute(.link, in: NSRange(location: 0, length: attributedText.length), options: []) { (attribute, range, pointee) in
//If there is no URL => Set our custom one
if attribute == nil {
attributedText.addAttribute(.link, value: "com.myapp.custom", range: range)
}
}
And in this delegate method, check the value of URL
.
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
print("url: \(URL)")
if url == ... {} else { ... }
return false
}
Hyperlinks in a UITextView
Use NSAttributedString
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Google"
attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@"http://www.google.com"] }];
self.textView.attributedText = attributedString;
Sure, you can set just a portion of the text to be the link. Please read more about the NSAttributedString
here.
If you want to have more control and do something before opening the link. You can set the delegate to the UITextView
.
- (void)viewDidLoad {
...
self.textView.delegate = self; // self must conform to UITextViewDelegate protocol
}
...
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
// Do whatever you want here
NSLog(@"%@", URL); // URL is an instance of NSURL of the tapped link
return YES; // Return NO if you don't want iOS to open the link
}
How to display clickable links in UITextView
Just select the UITextView
in your storyboard and go to "Show Attributes inspector" and select selectable
and links
. As the image below shows. Make sure Editable
is unchecked.
Link in UITextView not working in Swift 4
You should enable isSelectable
and disable isEditable
for your text view.
textView.isSelectable = true
textView.isEditable = false
You can also set up these behaviors inside Interface Builder in the Behavior section of the text view's Attributes inspector:
Related Topics
Separating Multiple If Conditions With Commas in Swift
Knowing Where Retain Cycles Are and Removing Them
Deletable Table With Textfield on Swiftui
Delete Folder With Contents from Firebase Storage
Wrong Specialized Generic Function Gets Called in Swift 3 from an Indirect Call
How to Return an Object That I Create in My Data Service Class Through Firebase
Changing Text Color of Datepicker
How to Set Up Scenekit Collision Detection
Swift @Escaping and Completion Handler
Downcasting Optionals in Swift: As? Type, or As! Type
Generic Function Taking a Type Name in Swift
How to Multiply Two Arrays Element-Wise
What Is Difference Between Optional and Decodeifpresent When Using Decodable For Json Parsing
How Does Collisionbitmask Work? Swift/Spritekit
Nehotspothelper.Register Not Received Call Back Ios11
How to Lock Orientation of One View Controller to Portrait Mode Only in Swift