I want to make a UILabel
or UITextView
with some text with 2 clickable links in it. Not links to webpages but I want to link those 2 links with actions like i would do with an UIButton
. All the examples i've seen are links to webviews but I dont want that. As well, the text will be translated in other languages so the positions have to be dynamic.
Want to make this:
Use NSMutableAttributedString. NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@"Google"]; [str addAttribute: NSLinkAttributeName value: @"http://www.google.com" range: NSMakeRange(0, str. length)]; yourTextView.
TTTAttributedLabel is a drop-in replacement for UILabel providing a simple way to performantly render attributed strings. As a bonus, it also supports link embedding, both automatically with NSTextCheckingTypes and manually by specifying a range for a URL, address, phone number, event, or transit information.
I needed to solve this exact same problem: very similar text with those two links in it, over multiple lines, and needing it to be able to be translated in any language (including different word orders, etc). I just solved it, so let me share how I did it.
Initially I was thinking that I should create attributed text and then map the tap's touch location to the regions within that text. While I think that is doable, I also think it's a much too complicated approach.
This is what I ended up doing instead:
SUMMARY:
DETAIL:
In the view controller's viewDidLoad
I placed this:
[self buildAgreeTextViewFromString:NSLocalizedString(@"I agree to the #<ts>terms of service# and #<pp>privacy policy#", @"PLEASE NOTE: please translate \"terms of service\" and \"privacy policy\" as well, and leave the #<ts># and #<pp># around your translations just as in the English version of this message.")];
I'm calling a method that will build the message. Note the markup I came up with. You can of course invent your own, but key is that I also mark the ends of each clickable region because they span over multiple words.
Here's the method that puts the message together -- see below. First I break up the English message over the #
character (or rather @"#"
string). That way I get each piece for which I need to create a label separately. I loop over them and look for my basic markup of <ts>
and <pp>
to detect which pieces are links to what. If the chunk of text I'm working with is a link, then I style a bit and set up a tap gesture recogniser for it. I also strip out the markup characters of course. I think this is a really easy way to do it.
Note some subtleties like how I handle spaces: I simply take the spaces from the (localised) string. If there are no spaces (Chinese, Japanese), then there won't be spaces between the chunks either. If there are spaces, then those automatically space out the chunks as needed (e.g. for English). When I have to place a word at the start of a next line though, then I do need to make sure that I strip of any white space prefix from that text, because otherwise it doesn't align properly.
- (void)buildAgreeTextViewFromString:(NSString *)localizedString { // 1. Split the localized string on the # sign: NSArray *localizedStringPieces = [localizedString componentsSeparatedByString:@"#"]; // 2. Loop through all the pieces: NSUInteger msgChunkCount = localizedStringPieces ? localizedStringPieces.count : 0; CGPoint wordLocation = CGPointMake(0.0, 0.0); for (NSUInteger i = 0; i < msgChunkCount; i++) { NSString *chunk = [localizedStringPieces objectAtIndex:i]; if ([chunk isEqualToString:@""]) { continue; // skip this loop if the chunk is empty } // 3. Determine what type of word this is: BOOL isTermsOfServiceLink = [chunk hasPrefix:@"<ts>"]; BOOL isPrivacyPolicyLink = [chunk hasPrefix:@"<pp>"]; BOOL isLink = (BOOL)(isTermsOfServiceLink || isPrivacyPolicyLink); // 4. Create label, styling dependent on whether it's a link: UILabel *label = [[UILabel alloc] init]; label.font = [UIFont systemFontOfSize:15.0f]; label.text = chunk; label.userInteractionEnabled = isLink; if (isLink) { label.textColor = [UIColor colorWithRed:110/255.0f green:181/255.0f blue:229/255.0f alpha:1.0]; label.highlightedTextColor = [UIColor yellowColor]; // 5. Set tap gesture for this clickable text: SEL selectorAction = isTermsOfServiceLink ? @selector(tapOnTermsOfServiceLink:) : @selector(tapOnPrivacyPolicyLink:); UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:selectorAction]; [label addGestureRecognizer:tapGesture]; // Trim the markup characters from the label: if (isTermsOfServiceLink) label.text = [label.text stringByReplacingOccurrencesOfString:@"<ts>" withString:@""]; if (isPrivacyPolicyLink) label.text = [label.text stringByReplacingOccurrencesOfString:@"<pp>" withString:@""]; } else { label.textColor = [UIColor whiteColor]; } // 6. Lay out the labels so it forms a complete sentence again: // If this word doesn't fit at end of this line, then move it to the next // line and make sure any leading spaces are stripped off so it aligns nicely: [label sizeToFit]; if (self.agreeTextContainerView.frame.size.width < wordLocation.x + label.bounds.size.width) { wordLocation.x = 0.0; // move this word all the way to the left... wordLocation.y += label.frame.size.height; // ...on the next line // And trim of any leading white space: NSRange startingWhiteSpaceRange = [label.text rangeOfString:@"^\\s*" options:NSRegularExpressionSearch]; if (startingWhiteSpaceRange.location == 0) { label.text = [label.text stringByReplacingCharactersInRange:startingWhiteSpaceRange withString:@""]; [label sizeToFit]; } } // Set the location for this label: label.frame = CGRectMake(wordLocation.x, wordLocation.y, label.frame.size.width, label.frame.size.height); // Show this label: [self.agreeTextContainerView addSubview:label]; // Update the horizontal position for the next word: wordLocation.x += label.frame.size.width; } }
And here are my methods that handle the detected taps on those links.
- (void)tapOnTermsOfServiceLink:(UITapGestureRecognizer *)tapGesture { if (tapGesture.state == UIGestureRecognizerStateEnded) { NSLog(@"User tapped on the Terms of Service link"); } } - (void)tapOnPrivacyPolicyLink:(UITapGestureRecognizer *)tapGesture { if (tapGesture.state == UIGestureRecognizerStateEnded) { NSLog(@"User tapped on the Privacy Policy link"); } }
Hope this helps. I'm sure there are much smarter and more elegant ways to do this, but this is what I was able to come up with and it works nicely.
Here's how it looks in the app:
Good luck! :-)
Erik
UITextView
:The key principles:
NSAttributedString
as a way of defining a link to tap.UITextViewDelegate
to catch the press of the link.Define a URL string:
private let kURLString = "https://www.mywebsite.com"
Add a link to your attributed string:
let originalText = "Please visit the website for more information." let attributedOriginalText = NSMutableAttributedString(string: originalText) let linkRange = attributedOriginalText.mutableString.range(of: "website") attributedOriginalText.addAttribute(.link, value: kURLString, range: linkRange)
Assign attributed string to a text view:
textView.attributedText = attributedOriginalText
Implement UITextViewDelegate
(this is really the key piece a prevents the URL from opening some website and where you can define your custom action instead):
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { if (URL.absoluteString == kURLString) { // Do whatever you want here as the action to the user pressing your 'actionString' } return false }
You can also customize how your link looks:
textView.linkTextAttributes = [ NSAttributedStringKey.foregroundColor.rawValue : UIColor.red, NSAttributedStringKey.underlineStyle.rawValue : NSUnderlineStyle.styleSingle]
UILabel
:I usually end up using TTTAttributedLabel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With