I have RTF text which I am showing to the user on Mac. Now I need to replace some text. The text has some images inline. When I execute the following code, the images are getting lost. I am using c#, Mono and Monobjc to run this on mac.
NSText _questionView;
// some initialisation code which I have skipped
//
NSRange range = NSRange.NSMakeRange(0, _questi开发者_高级运维onView.TextStorage.Length);
NSData oldString = _questionView.RTFFromRange(range);
if (oldString != null)
{
string s = oldString.ConvertRTFToString();
_questionView.ReplaceCharactersInRangeWithRTF(range, s.ConvertToNSData());
_questionView.SelectedRange = NSRange.NSMakeRange(0,0);
// After this line the inline images are lost.
}
You are losing the images because you are converting the RTF content to NSString
. NSString
can only carry text, not attributes. You should consider using NSAttributedString instead for the text manipulation, in order to keep the RTF attributes (style, images, etc.).
精彩评论