iOS 使用markdown 实现编辑和预览文本

注意要点: 

1.在iOS 可以依赖UIWebview 来实现

2.丰富的UI样式依赖 html 的样式, js 调用插入markdown内容呈现出来

3.实现markdown编辑快捷键:参考github 编辑内容的快捷键即可

⚠️ 实现过程中遇到一个问题,在github上 编辑好的文本,客户端在线预览的时候是空白的。。。调试了好久终于找到问题:一样的文本在pc端可能会生成\r,但是在iOS 客户端回车键只会是\n. 所以markdown文本在注入js前执行的过滤方案中我多加了一条过滤\r 即问题解决了

- (NSString *)getMarkdownContentWithMarkdowString:(NSString *)markdown {
    markdown = [markdown stringByReplacingOccurrencesOfString:@"\r"withString:@""];//⚠️防止不识别\r
    markdown = [markdown stringByReplacingOccurrencesOfString:@"\n"withString:@"\\n"];
    markdown = [markdown stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    markdown = [markdown stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"];
    return markdown;
}

测试复现这种情况 可以使用下面的测试文案:

(1)driving off the spleen and regulating the circulation.\r\n   //无法显示
(2)driving off the spleen and regulating the circulation.\r     //无法显示
(3)driving off the spleen and regulating the circulation.\n     //正常
(4)driving off the spleen and regulating the circulation.       //正常

markdown 编辑效果如下:

 

markdown 预览效果如下:

 

参考项目:地址  https://github.com/Li-Qun/EditMarkdownDemo 

欢迎给小星星✨✨✨鼓励哦😘😘😘

posted on 2019-03-21 16:25  ACM_Someone like you  阅读(1266)  评论(0编辑  收藏  举报

导航