在ios中集成CKEditor实现富文本编辑

在ios上可以使用CKEditor和UIWebView实现富文本创建和编辑的功能.

首先下载CKEditor的包,  下载地址是http://download.cksource.com/CKEditor/CKEditor/CKEditor%204.2.2/ckeditor_4.2.2_standard.zip

然后将CKEditor解压后引入到工程中, 可以选中文件夹拖到XCode中, 注意上面要选中 Copy items into destination group's folder,  下面选择第二个, Create folder references for any added folders, 由于CKeditor中是js引用, 需要有目录结构, 因此拖到工程中时要保留文件夹目录

然后显示的时候需要使用一个UIWebView加载CKEditor中samples的一个html


- (void)viewDidLoad
{
	[super viewDidLoad];
	self.fileName = @"ckeditor/samples/api.html";
	
	webView.delegate = self;
    


        NSString *mainPath =[[[NSBundle mainBundle] bundlePath] stringByAppendingString:[@"/" stringByAppendingString:self.fileName]];
        NSLog(mainPath);
        NSString *basePath =[[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/ckeditor/samples/api.html"];
        NSLog(self.fileName);
        
        BOOL fileExistsBase = [[NSFileManager defaultManager]fileExistsAtPath:basePath];
        BOOL fileExistsMain = [[NSFileManager defaultManager]fileExistsAtPath:mainPath];

        
        
        NSString *basecontent = [NSString stringWithContentsOfFile:basePath
                                                          encoding:NSUTF8StringEncoding
                                                             error:NULL];
        
        NSString *javaScriptCodeToReplaceContent = @"";
        [[@""];
        
        NSString *mainContent = [basecontent stringByAppendingString:javaScriptCodeToReplaceContent];
        NSData *mainContentNSData = [mainContent dataUsingEncoding:NSUTF8StringEncoding];
        
        NSLog(mainContent);
        BOOL a = [mainContentNSData writeToFile:mainPath atomically:YES];
        
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:basePath]]];
        
}



使用了 ckeditor/samples/api.html(目录需要打全)文件, 然后将它显示在UIWebView中, 其中的javascript会自动链接到ckeditor包中的js文件, 就会显示出一个可编辑的富文本



你可能感兴趣的:(ios,ios,CKEditor,富文本)