Displaying Richtext

NeilTurpNeilTurp GBMember ✭✭

I'm writing an app that displays rich text. I have some encrypted rtf files that I decrypt and then extract a section from. Then I want to display this section onscreen. I was hoping to take the string and put it straight into a RTF property on a NSTextView but I can't see how to do it. This is what I am doing:

    string str = DecryptandExtractFromFile(sFileName, nSect);

This produces, e.g:

{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
{\colortbl ;\red0\green0\blue0;\red0\green0\blue255;}
\viewkind4\uc1\pard\cf1\lang2057\f0\fs20 \cf0\fs20\par
\par
Chapter:\cf2\b TEST PAPER No. 1\cf0\b0\par
\par
These questions should be answered only when the student feels confident that the lesson has been mastered.\par
\par
There should be no reference to books as this would defeat the object.\par
\par
\cf2\fs22 \line\line}

This is a very basic example but illustrates the problem.

Now I can't see how to put this into a NSTextView directly so I thought, save to an RTF file and open like this:

    string sPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + System.IO.Path.DirectorySeparatorChar + "Temp.rtf";
    System.IO.File.WriteAllText(sPath, str);
    tv.FromRtfdFile(sPath);

where tv is a predefined and loaded NSTextView.

However the the temp file created is not a valid RTF file. It doesn't open here, or in a native Mac RTF reader or even in Windows. Have I missed something or is there an easier way to get this into a Mono Mac app as formatted text?

Neil

Best Answer

Answers

  • TimothyRisiXMTimothyRisiXM USXamarin Team Xamurai

    According to http://pardoe.io/blog/2006/08/27/rich-text-file-rtf-into-nstextview-cocoa-obj-c it sounds like it should just require you to grab the file path and load with FromRtfdFile. What does the DecryptAndExtractFromFile method you pasted in the first code block do?

  • NeilTurpNeilTurp GBMember ✭✭

    I saw that page and that is what led me to trying this way. The DecryptandExtractFromFile routine loads a file from disk and decrypts it. It then extracts a piece of that file and returns it as a string. Example of the outputted string shown above. It looks like a piece of standard rtf to me - it begins with {\rtf1\ansi\ansicpg1252\deff0 and ends with two blank lines: \line\line}. I have a Windows version of this and it just works.

  • NeilTurpNeilTurp GBMember ✭✭

    Does anyone know a way to avoid saving and reloading the rtf file?

  • NeilTurpNeilTurp GBMember ✭✭

    Thanks for trying. Your idea of using NSString gets the string into the textview but it opens it as a plain text file showing the rtf codings. I couldn't make any headway with the NSAttributedString idea. I've managed to get it to work by checking the string for missing } characters and adding in any missing ones before saving to file.

Sign In or Register to comment.