一个将文本转换为富文本字符串以及将富文本字符串转换为文本的引擎。默认支持 HTML 和 Markdown。它是 iOS 和 OSX 的跨平台引擎。
需要 CoreText。
NSString *string = @"#Dalton Cherry\n##Dalton Cherry\n*Lorem Ipsum* is simply _dummy_ text of the **printing** and typesetting industry. Lorem Ipsum has been the industry's standard *dummy* text ever since the 1500s. Here is a link to test with [Google](http://www.google.com/), should work fine. Here is a raw link: https://github.com. Hello at @austin, here is that email [email protected]. youtube video: http://www.youtube.com/watch?v=XibDfYd83Zg here is more text. Some more text";
DCTextEngine *engine = [DCTextEngine engineWithMarkdown];
__weak blockEngine = engine;
[engine addPattern:@"more text." found:^DCTextOptions*(NSString *regex, NSString *text){
DCTextOptions *opts = [DCTextOptions new];
NSTextAttachment *attach = [[NSTextAttachment alloc] init];
attach.image = [UIImage imageNamed:@"subways.png"];
attach.bounds = CGRectMake(0, 0, self.view.frame.size.width, 100);
opts.attachment = attach;
return opts;
}];
[engine addPattern:@"(^|\\s)@\\w+" found:^DCTextOptions*(NSString *regex, NSString *text){
DCTextOptions *opts = [DCTextOptions new];
opts.color = [UIColor redColor];
opts.font = [blockEngine boldFont];
return opts;
}];
正如示例所示,该引擎具有非常强大的正则表达式解析能力,但仍然非常简单易用。这个引擎适用于 iOS 6,但您可应用的样式会非常有限。我 强烈 建议使用 iOS 7 或以上版本,或者至少 10.7 或以上版本。