我的NSString如下如何在NSString中製作html鏈接?
NSString *textOutStations = [NSString stringWithFormat:@"Hello Everyone. Please check out website:<br> http://www.google.com/</br>"];
我想在URL顯示google.com下面,我將加載這個在UIWebView的。
所以,當用戶點擊它,它必須在iPhone打開Safari。
我的NSString如下如何在NSString中製作html鏈接?
NSString *textOutStations = [NSString stringWithFormat:@"Hello Everyone. Please check out website:<br> http://www.google.com/</br>"];
我想在URL顯示google.com下面,我將加載這個在UIWebView的。
所以,當用戶點擊它,它必須在iPhone打開Safari。
NSString *textOutStations = [NSString stringWithFormat:@"Hello Everyone. Please check out website:<br> <a href=\"google.com\">http://www.google.com/</a>"];
[self.webView loadHTMLString:textOutStations baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
然後在UIWebView的委託:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// Opening safari
[[UIApplication sharedApplication] openURL:request.URL];
....
}
試試這個
NSString *textOutStations = @"<html><head><title></title></head><body><div> Hello Everyone. Please check out website:<br/> <a href=\"http://www.google.com/\"> http://www.google.com/ </a> </div></body></html>";
[self.webView loadHTMLString:textOutStations baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
這將幫助你......
謝謝,但請參閱注入註釋,另一個問題是 – Duaan
你不能指定一個URL在一個字符串中並且它是可點擊的,這種類型的功能取決於正在使用它的對象,也就是UITextView
UITextField
UILabel
UIWebView
等,
一個UIWebview
會顯示您的網址,在web視圖不會在Safari中打開的鏈接。如果您希望將其加載到用戶界面的Web視圖,它上面已經ansered,如果你想在Safari瀏覽器中打開它,你必須做
[[UIAplication sharedApplication] openUrl:urlObject];
,如果你想在一個UITextView
打開它是文本我建議這個其他堆棧溢出鏈接here
我檢查了UIWebView中的屬性檢查器中的鏈接選項,它檢測到的鏈接。
方法在Safari中打開...
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if (inType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
mm..put Hello...「 – Injectios
你能做出的,我試過了,但發現了一些錯誤? – Duaan
您是如何顯示的?的UILabel,的UITextField,一個UIWebView? –