Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make the text hyperlink or clickable in UITextView

I am having the UITextView and displaying text in it from three tags(message, titleUrl and url). What i need is that i want to make the text of "titleUrl" clickable to open the value of "url" in web view. I managed to open the link directly from url, but i need to open the link by clicking "titleUrl". I have tried to achieve the following from this code.

[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.")];

But i am not getting in this how to modify this to achieve the functionality. I want to enter the value have in string, don't have the static text to enter. Can anyone guide me to handle this?

Update:

NSString *message = [NSString stringWithFormat:@"%@\n ", tempStr1];
        NSString *message1 = [NSString stringWithFormat:@"\n#<pp>%@#", titlStr1];
        NSString *localizedString = NSLocalizedString(message1, nil);

        NSRange ppRange = [localizedString rangeOfString:NSLocalizedString(message1, nil) options:NSCaseInsensitiveSearch];


        NSURL *ppURL = [NSURL URLWithString:strUrl];



        NSDictionary *attribute1 = @{NSForegroundColorAttributeName: [UIColor whiteColor],
                                     NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15.0],
                                     };
        NSMutableAttributedString *newAttString = [[NSMutableAttributedString alloc] initWithString:message attributes:attribute1];

        //

        NSMutableAttributedString *finalMessage = [[NSMutableAttributedString alloc] initWithString:localizedString];
        [finalMessage beginEditing];

        [finalMessage addAttributes:attribute1 range:ppRange];
        [finalMessage addAttribute:NSLinkAttributeName value:ppURL range:ppRange];
        [finalMessage endEditing];

        [newAttString appendAttributedString:finalMessage];

        self.txtView.attributedText = newAttString;
like image 204
iPhone 7 Avatar asked Aug 14 '26 17:08

iPhone 7


2 Answers

This is as simple as using an NSMutableAttributedString. Note: This is not the only way, this can be done with searching for ranges etc, this is just a simple implementation to get you in the right direction since you do have the static message, because your localizing all of them, which means you have the static english form of it.

NSString *tosString = @"Terms of Service";
NSString *ppString = @"Privacy Policy";
NSString *message = [NSString stringWithFormat:@"I agree to the #<ts>%@# and #<pp>%@#", tosString, ppString];
NSString *localizedString = NSLocalizedString(message, nil);

NSRange tosRange = [localizedString rangeOfString:NSLocalizedString(tosString, nil) options:NSCaseInsensitiveSearch];
NSRange ppRange = [localizedString rangeOfString:NSLocalizedString(ppString, nil) options:NSCaseInsensitiveSearch];

NSURL *tosURL = [NSURL URLWithString:@"http://toslink.com"];
NSURL *ppURL = [NSURL URLWithString:@"http://pplink.com"];

NSMutableAttributedString *finalMessage = [[NSMutableAttributedString alloc] initWithString:localizedString];
[finalMessage beginEditing];
[finalMessage addAttribute:NSLinkAttributeName value:tosURL range:tosRange];
[finalMessage addAttribute:NSLinkAttributeName value:ppURL range:ppRange];
[finalMessage endEditing];

self.yourTextView.attributedText = finalMessage;
like image 199
soulshined Avatar answered Aug 16 '26 19:08

soulshined


Swift 3.0

in your view did load...

let tosString = "Terms of Service" let ppString = "Privacy Policy" let message = "By logging in, you agree to our (tosString) and that you have read our (ppString)"

    let localizedString = NSMutableAttributedString(string: message)

    let tosRange = localizedString.mutableString.range(of: tosString)
    let ppRange = localizedString.mutableString.range(of: ppString)

    let tosURL = URL(string: "http://toslink.com")!
    let ppURL = URL(string: "http://pplink.com")!

    localizedString.addAttribute(NSLinkAttributeName, value: tosURL, range: tosRange)
    localizedString.addAttribute(NSLinkAttributeName, value: ppURL, range: ppRange)

    demoTextView.delegate = self
    demoTextView.isSelectable = true
    demoTextView.isUserInteractionEnabled = true
    localizedString.endEditing()
    self.demoTextView.attributedText = localizedString

and using textview delegate method func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool { // Handle your Control return true }

like image 37
Nitin Avatar answered Aug 16 '26 18:08

Nitin