在UIWebView中媒体文件的展示和HTML字符串的展示

来源:互联网 发布:java页面自动跳转代码 编辑:程序博客网 时间:2024/06/05 13:18

UIWebView中媒体文件的展示和HTML字符串的展示


序言:

       UIWebView中不仅可以显示网页,还可以显示或者播放图片、视频、音频,甚至、PDF和Word文档。以上东西可以保存在Web上还可以压缩保存到本地。。。。



第一步:

我们今天来在Web上显示图片。

    NSString*path;    path=[[NSBundle mainBundle]  pathForResource:@"one1" ofType:@"png"];    NSData*data=[NSData dataWithContentsOfFile:path];    [_ZSJwebView  loadData:data MIMEType:@"image/png" textEncodingName:nil baseURL:nil];    

效果展示:






第二步:  我们用Web来显示文档。


     NSString*path;    path=[[NSBundle mainBundle]  pathForResource:@"df" ofType:@"doc"];        NSURL*url=[NSURL  fileURLWithPath:path];        NSURLRequest*request=[NSURLRequest   requestWithURL:url];    [_ZSJwebView  loadRequest:request];        

效果展示:

完整代码:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIWebViewDelegate>{        UIWebView*_ZSJwebView;        }@end

#import "ViewController.h"#import "Mylabel.h"@implementation ViewController-(void)viewDidLoad{            _ZSJwebView=[[UIWebView alloc]initWithFrame:self.view.frame];    [self.view addSubview:_ZSJwebView];                        //    NSString*path;//    path=[[NSBundle mainBundle]  pathForResource:@"one1" ofType:@"png"];//    NSData*data=[NSData dataWithContentsOfFile:path];//    [_ZSJwebView  loadData:data MIMEType:@"image/png" textEncodingName:nil baseURL:nil];//                    NSString*path;    path=[[NSBundle mainBundle]  pathForResource:@"df" ofType:@"doc"];        NSURL*url=[NSURL  fileURLWithPath:path];        NSURLRequest*request=[NSURLRequest   requestWithURL:url];    [_ZSJwebView  loadRequest:request];                                        } @end





UIWebView中HTML字符串的显示

正文:

UIWebView不近可以用URL来导入界面,也可以用HTML来导入界面。


代码:

      [super viewDidAppear:animated];            NSString*html=@"<b>[小明同学]</b><br />"@"<b>20154678<hr />"@"<b>[逐月]</b><br />" "http://blog.csdn.net/zhoushuangjian511/";    [_ZSJwebView loadHTMLString:html baseURL:nil];    

效果展示:


注释:

UIwebView的datadetectorTypes的属性设置成UIDataDetectorTypeAll.这样HTML中的电话和URL就会变成链接。



完整代码:


#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIWebViewDelegate>{        UIWebView*_ZSJwebView;        }@end

#import "ViewController.h"#import "Mylabel.h"@implementation ViewController-(void)viewDidLoad{            _ZSJwebView=[[UIWebView alloc]initWithFrame:self.view.frame];    _ZSJwebView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;    _ZSJwebView.dataDetectorTypes=UIDataDetectorTypeAll;    [self.view addSubview:_ZSJwebView];            }-(void)viewDidAppear:(BOOL)animated{            [super viewDidAppear:animated];            NSString*html=@"<b>[小明同学]</b><br />"@"<b>20154678<hr />"@"<b>[逐月]</b><br />" "http://blog.csdn.net/zhoushuangjian511/";    [_ZSJwebView loadHTMLString:html baseURL:nil];            } @end








友情快递:

UITextView中的字符串里含有URL或者电话时,将自动显示链接


正文:

该功能默认情况下是关闭的。为OFF;将UItextView的后,就开启该功能。下面是该属性的其他几个:dataDetectorTypeALL的属性UIDataDetectorTypes效果UIDataDetectorTypePhoneNumber只创建电话号码的链接UIDataDetectorTypeLink只创建URL的链接UIDataDetectorTypeNone不创建链接(默认)UIDataDetectorTypeAll及创建URL也创建电话链接




0 0