【问题标题】:Hyperlink in uitableview footeruitableview 页脚中的超链接
【发布时间】:2013-06-16 01:45:52
【问题描述】:

我正在使用 InAppSettingsKit 进行设置,并希望在表格视图页脚中添加公司超链接。我已经成功地在部分标题中添加了一个 uiwebview(它位于表格视图的底部并显示为页脚)。 webview 显示超链接,但不响应触摸。

在 settingsViewController:tableView:viewForHeaderForSection: 中创建 web 视图:

    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor clearColor];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(30, 0, self.view.frame.size.width-60, 35)];
    webView.delegate = self;
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:[NSDate date]];
    NSUInteger fontSize = 13;

    NSString *link = @"<a href=\"http://www.google.com\">My Hyperlink</a>";
    [webView loadHTMLString:[NSString stringWithFormat:@"<html><head></head><body style=\"font-family: sans-serif;font-size:%dpx;\"> <center>Copyright © %d %@</center></body></html>", fontSize, dateComponents.year,link]  baseURL:nil];
    webView.backgroundColor = [UIColor clearColor];
    webView.opaque = NO;
    [containerView addSubview:webView];
    return containerView;

和 uiwebview 委托方法:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
DDLogVerbose(@"Web view should start load with request %@. InType: %d", inRequest.URL.absoluteString, inType);
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
    [[UIApplication sharedApplication] openURL:[inRequest URL]];
    return NO;
}

return YES;

}

谁能告诉我为什么触摸不起作用? 顺便说一句... webView:shoudStartLoadWithRequest: 在表格视图被加载/显示时立即被调用

谢谢!

【问题讨论】:

    标签: ios uitableview uiwebview hyperlink footer


    【解决方案1】:

    在您的代码中,我发现容器视图有两个问题,没有框架,并且您没有为 web 视图设置正确的框架,一旦尝试此示例,您就会发现出错的地方。

    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(30, 0, 100, 35)];
    

    【讨论】:

    • 感谢您的回复,但这没有帮助。带有 formattet 超链接的 webview 已经显示得很好。它只是没有按应有的方式响应触摸。
    【解决方案2】:

    原因是 IASKSettingsDelegate 方法 settingsViewController:tableView:heightForHeaderForSection: 为该特定部分返回 0。返回与 uiwebview 的高度相同的高度解决了这个问题。

    【讨论】:

      【解决方案3】:

      作为替代方案:从 iOS 7 开始,UIKit/NSAttributedString 支持 NSLinkAttributeName,甚至可以进行基本的 HTML 导入。 因此,您可以使用 UITextView 而不是 UIWebView 作为表格视图的页眉/页脚。

      要导入 HTML,请使用 -[NSAttributedString data:options:documentAttributes:]NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType 选项。

      NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
      NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
      NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:NULL error:NULL];
      
      UITextView *textView = [[UILabel alloc] initWithFrame: ...];
      textView.editable = NO;
      textView.scrollEnabled = NO;
      textView.textContainerInset = UIEdgeInsetsZero; // optional
      textView.attributedText = attributedText;
      

      [UILabel 显示超链接但不可点击,见UILabel and NSLinkAttributeName: Link is not clickable

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-07
        • 1970-01-01
        • 2018-02-18
        • 1970-01-01
        • 2014-03-10
        • 1970-01-01
        • 1970-01-01
        • 2016-07-13
        相关资源
        最近更新 更多