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
}
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
}
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.
Related Topics
Conditionally Import a Framework (Such as Speech) Based on iOS Version in Swift
Disable Bounce Effect in Uipageviewcontroller
Error: Uicollectionview Must Be Initialized with a Non-Nil Layout Parameter
Uislider That Snaps to a Fixed Number of Steps (Like Text Size in the iOS 7 Settings App)
Uiview Touch Event in Controller
Facebook Graph API Get Request - Should Contain "Fields" Parameter (Swift, Facebook Sdk V4.5.1)
Turn a Page Like a Book with Uiview
How to Calculate the Uilabel Height Dynamically
How to Disable the Back Navigation Menu in iOS 14+
Swift Override Function in Extension
Are Headphones Plugged In? iOS7
Pods-Resources.Sh Permission Denied in iOS Project
How to Change the Color of an Uiimage