首先,使用NSAttributedString
orNSMutableAttributedString
在子视图中显示您的 RichText(例如 UITextView/UILabel)
然后,用于NSTextAttachment
替换文本中的图像脚本。
NSString *egText = @"hello [this_is_an_img_script]";
NSMutableAttributedString * destStr = [[NSMutableAttributedString alloc] initWithString:egText];
NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
attachment.image = [UIImage imageNamed:[this_is_an_img_script]];
NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; //make your image to an attributedString
[destStr replaceCharactersInRange:range withAttributedString:textAttachmentString];
at last: [YourLabel(or YourTextView) setAttributedString:destStr];
顺便说一句:如果您将YYkit
用于 RichText,则不能使用YYAttachMentString
替换NSAttachMentString
,这些是不同的东西,UITextView(UILabel)
无法加载YYAttachMentString
. 我正在寻找某种方式来显示我的 gif UITextView
(因为YYKit
无法加载和预览带有 url 的网络图像,所以YYKit
总是显示空的应该是 a netImage
,cripes!)