user440096
user440096

Reputation:

Having a url appear in a UITextview

This code works well to make a url appear in a UITextview

UITextView * descriptionTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 50, 300, 300)];
descriptionTextView.textColor = [UIColor whiteColor]; 
descriptionTextView.dataDetectorTypes = UIDataDetectorTypeLink;
descriptionTextView.font = [UIFont fontWithName:@"Helvetica" size:16];  
descriptionTextView.backgroundColor = [UIColor clearColor]; 
descriptionTextView.text = @"Click to go to the google website, http://www.google.com ";
descriptionTextView.autocorrectionType = UITextAutocorrectionTypeNo;    
descriptionTextView.keyboardType = UIKeyboardTypeDefault;  
descriptionTextView.returnKeyType = UIReturnKeyDone;  
descriptionTextView.editable = NO;
descriptionTextView.tag = 1;
[self.view addSubview:descriptionTextView];

The problem is that I the whole url I write appears, http://www.google.com

Is there a way I can use just a single word to contain the link? So the user can only see 'Goggle' written in blue and when they click that work it opens safari.

Many Thanks, -Code

Upvotes: 1

Views: 752

Answers (2)

Delorean
Delorean

Reputation: 364

I know this is super old thread. UITextView supports rich text/HTML. You should create NSAttributedString and assign it to attributedText property to manipulate with non plain text content. Like so:

        NSString *htmlString = @"<your HTML code>";
        textView.attributedText = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding]
                                                           options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                     NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                                documentAttributes:nil
                                                             error:nil];

Upvotes: 0

adali
adali

Reputation: 5977

you can do it use a UIWebview

use

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;

and code the whole message then wrap the link to

[webView.loadHTMLString:@"<a href="http://www.google.com/">Google</a>" baseURL:nil];

Upvotes: 1

Related Questions