How do i hyperlink in my iPhone Application?
I want a hyperlink in my iPhone Application, for example like in HTML:
<a href="www.yahoo.com"> Click here </a>
How can i do this in Objective-C?
Th开发者_运维问答e most important thing is that I don't want to show the actual link, I just want to show some text like "click here" and then it loads a link.
I'm currently using the below code. But as i told you I don't want to show the link instead I want something like "click here".
textView.text = @"http://stackoverflow.com";
textView.dataDetectorTypes = UIDataDetectorTypeLink;
Apple has a writeup that should tell you everything you need to know to do this.
The summary is that you make a category on NSAttributedString
so that you can pass it a string and a URL, and it will create an attributed string that looks like a hyperlink (blue and underlined, for example) and has the URL as an attribute.
Try this way:
UIButton *btnFaceBook = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[btnFaceBook setFrame:CGRectMake(20, 290, 280, 30)];
[btnFaceBook setBackgroundColor:[UIColor clearColor]];
[btnFaceBook.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
[btnFaceBook addTarget:self action:@selector(followFacebook) forControlEvents:UIControlEventTouchUpInside];
[btnFaceBook setTitle:@"Click Here" forState:UIControlStateNormal];
[contentView addSubview:btnFaceBook];
below is the function to open the link:
-(void) followFacebook{
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://facebook.com/"]];
}
You can try this with a UIButton
. Just set the title of the button as "click here" or whatever you want and open the link when the user clicks on the button.
-(void)buttonClicked {
// code to open link
}
精彩评论