据我了解,标准的 UIWebView
能够显示许多扩展的本地文件。由于我需要在我的应用程序中显示格式化文本,我认为这种方法是可行的。
因此,我创建了一个名为 test.rtf 的富文本文件,目前我正在尝试在我的 WebView 中显示它。但是,我没有成功。我只看到一个空白的白色屏幕,而不是我在 rtf 文件中键入的文本。
有人可以查看我的代码并指出我在实现中哪里出了问题吗?
头文件:
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController <UIWebViewDelegate> {
UIWebView * webView;
}
@end
主文件:
#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)init
{
self = [super init];
if (self) {
// Customization at initialization
self.view.backgroundColor = [UIColor clearColor];
//Adding a webview to display richtext documents
int webHeight = self.view.bounds.size.height;
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 150.0, 320.0, webHeight-150.0)];
webView.dataDetectorTypes = NO;
webView.scrollView.bounces = NO;
webView.delegate = self;
[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque: NO];
[self.view addSubview:webView];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Accessing richtext file
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"rtf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
if (request) {
// Additional configuration
}
//Adding the content of the webiew
[webView loadRequest:request];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSLog(@"委托(delegate)被调用"); //在这里做任何你想做的事 返回是; } @结束
最佳答案
您应该设置UIWebView
的委托(delegate)并返回YES
。 UIWebViewDelegate Protocol Reference说:
webView:shouldStartLoadWithRequest:navigationType:
Sent before a web view begins loading a frame.
因此您应该在第一次加载您的 test.rtf 文件时返回 YES
。
当您更新问题时。你需要设置
webView.delegate = self;
//webView.delegate = appDelegate; // not this line
关于iphone - UIWebView 和 Richtext 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13440808/