標籤:網頁顯示媒體檔案 網頁載入html檔案 網頁 web uiwebview
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*[email protected]"<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*[email protected]"<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也建立電話連結 |
在UIWebView中媒體檔案的展示和HTML字串的展示