iOS 直接显示HTML

来源:互联网 发布:防晒霜推荐 知乎 编辑:程序博客网 时间:2024/05/22 19:06

大中午的好困,还在坚持写博文有没有,对于HTML之前可用UITextView 的方法setContentToHtml:方法 现在不让用了,应该是为了鼓励大家用UIWebView展示HTML吧。

不过现在还有小朋友UITextView和UIWebView加载HTML字符串。

 if (DeviceVersion >= 7.0) {//iOS7.0以上才可用这一方法

         UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0, 320, ScreenHeight)];

        [textView setEditable:NO];

        attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

        textView.attributedText = attributedString;

         textView.delegate  = self;

            [self.view addSubview:textView];

//            [textView setContentToHTMLString:htmlString]; //这老方法 不可用了 

   }else//ios7.0以下 还用webview

    {

        UIWebView *webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, ScreenHeight-44)];

        [webview loadHTMLString:htmlString baseURL:nil];

//        webview.scalesPageToFit = YES;

        webview.delegate = self;

        //        webview.autoresizesSubviews = YES;

        //        [(UIScrollView *)[[webview subviews]objectAtIndex:0]setBounces:NO ];

        webview.scrollView.scrollEnabled = YES;

        

        [self.view addSubview:webview];

      

    }




0 0