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 

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

转载于:https://www.cnblogs.com/someonelikeyou/p/10572448.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值