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

技术分享

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也创建电话链接




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。