在一款項目里添加閱讀各種文檔功能 那么對在線的文檔或者是下載后的文檔 進行閱讀,比如 doc/docx/xls/pdf等文件
有兩種方法總結如下:
1.
- (void)viewDidLoad
{
[super viewDidLoad];
webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, Phone_Weight, Phone_Height)];
[self loadDocument:@"1.docx" inView:webView];
webView.scalesPageToFit=YES;//點擊伸縮效果的
webView.delegate=self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView1
{
[self.view addSubview:webView1];
}
2.
- (void)viewDidLoad
{
[super viewDidLoad];
webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, Phone_Weight, Phone_Height)];
webView.scalesPageToFit=YES;//點擊伸縮效果的
NSString *documentLocation=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"docx"];
NSURL *myDocument=[NSURL fileURLWithPath:documentLocation];
NSURLRequest *request=[NSURLRequest requestWithURL:myDocument];
[webView loadRequest:request];
webView.delegate=self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView1
{
[self.view addSubview:webView1];
}
webView.scalesPageToFit=YES;這句很重要,不然 讀出的文檔不能很好的適應屏幕
還有一點,在工程中添加測試文檔時候要在Add to targets第一項打對勾 不然路徑無效,如圖:

效果圖如下:(doc文件)

