iphone - UIWebView 和 Richtext 文件

标签 iphone objective-c ios uiwebview rtf

据我了解,标准的 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)并返回YESUIWebViewDelegate 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/

相关文章:

iphone - 如何通过项目模板定义向 AppDelegate.m 添加方法

android - 为什么移动特定的 css 似乎只针对屏幕尺寸?

objective-c - 向 View 添加许多工具提示时出现问题

ios - 使用 SwiftUI 的 TextField 描边边框文本

ios - Cocoapods 私有(private) Pod 内的 Swift 框架

iphone - 如何将简单的字符串写入 iPhone 上的文件?

iphone - 如何在分组的 UITableView 单元格上使用自定义背景?

objective-c - 使用UIImagePickerController获取图片——如何判断保存的是PNG还是JPEG?

ios - 暂停和恢复加载屏幕的 CABasicAnimation?

ios - Textfield 的文本必须看起来有点正确