string of rich text (rtf) tags to rtfData

  • How do I convert a string of rtf tags to an NSData object?
    My string looks like this:

    {\rtf1\mac\ansicpg10000\cocoartf824\cocoasubrtf410
    {\fonttbl\f0\fswiss\fcharset77 Helvetica;}
    {\colortbl;\red255\green255\blue255;\red255\green0\blue12;\red51
    \green204\blue255;\red7\green255\blue0;
    }
    \vieww12000\viewh15840\viewkind0
    \deftab720
    \pard\pardeftab720\qc

    \f0\fs120 \cf2 This\cf1  \cf3 is a\cf1  \cf4 Test}

    -Rich
  • Richard Salvatierra skrev:
    > How do I convert a string of rtf tags to an NSData object?
    > My string looks like this:
    >
    > {\rtf1\mac\ansicpg10000\cocoartf824\cocoasubrtf410
    > {\fonttbl\f0\fswiss\fcharset77 Helvetica;}
    > {\colortbl;\red255\green255\blue255;\red255\green0\blue12;\red51\green204\blue255;\red7\green255\blue0;
    >
    > }
    > \vieww12000\viewh15840\viewkind0
    > \deftab720
    > \pard\pardeftab720\qc
    >
    > \f0\fs120 \cf2 This\cf1  \cf3 is a\cf1  \cf4 Test}
    >
    I assume you have your RTF-string in NSString *rtfString.

    NSData *rtfData = [rtfString
    dataUsingEncoding:NSWindowsCP1252StringEncoding];

    And then maybe?:

    NSAttributedString *attributedString = [[NSAttributedString alloc]
    initWithRTF:rtfData documentAttributes:nil];

    // Fredrik Olsson

    > -Rich
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<fredrik.olsson...>.
    se

    >
    >
    > This email sent to <fredrik.olsson...>
  • This is working fine.  Does anyone see and issues (All string data
    will have come from a mac);
    NSData *data = [aString dataUsingEncoding: NSMacOSRomanStringEncoding]

    One other question:  To test for the presence of an rtf commented
    string I do the following.  What is a better way?

    NSRange rtfRange = [aString rangeOfString: @"rtf1"];
    if(NSNotFound != rtfRange.location){
    // rich text
    }

    On Oct 25, 2006, at 3:09 AM, Fredrik Olsson wrote:

    > Richard Salvatierra skrev:
    >> How do I convert a string of rtf tags to an NSData object?
    >> My string looks like this:
    >>
    >> {\rtf1\mac\ansicpg10000\cocoartf824\cocoasubrtf410
    >> {\fonttbl\f0\fswiss\fcharset77 Helvetica;}
    >> {\colortbl;\red255\green255\blue255;\red255\green0\blue12;\red51
    >> \green204\blue255;\red7\green255\blue0;
    >> }
    >> \vieww12000\viewh15840\viewkind0
    >> \deftab720
    >> \pard\pardeftab720\qc
    >>
    >> \f0\fs120 \cf2 This\cf1  \cf3 is a\cf1  \cf4 Test}
    >>
    > I assume you have your RTF-string in NSString *rtfString.
    >
    > NSData *rtfData = [rtfString
    > dataUsingEncoding:NSWindowsCP1252StringEncoding];
    >
    > And then maybe?:
    >
    > NSAttributedString *attributedString = [[NSAttributedString alloc]
    > initWithRTF:rtfData documentAttributes:nil];
    >
    >
    > // Fredrik Olsson
    >
    >
    >> -Rich
    >> _______________________________________________
    >> Do not post admin requests to the list. They will be ignored.
    >> Cocoa-dev mailing list      (<Cocoa-dev...>)
    >> Help/Unsubscribe/Update your Subscription:
    >> http://lists.apple.com/mailman/options/cocoa-dev/fredrik.olsson%
    >> 40kmstudio.se
    >>
    >> This email sent to <fredrik.olsson...>
    >
  • On Oct 25, 2006, at 9:09 AM, Richard Salvatierra wrote:

    > One other question:  To test for the presence of an rtf commented
    > string I do the following.  What is a better way?

    Here's the file header as described in /usr/share/file/magic.

    0      string          {\\rtf          Rich Text Format data,
       > 5      byte            x              version %c,
       > 6      string          \\ansi          ANSI
       > 6      string          \\mac          Apple Macintosh
       > 6      string          \\pc            IBM PC, code page 437
       > 6      string          \\pca          IBM PS/2, code page 850

    That will give you the version of the RTF format used in the file,
    and the encoding it uses. Not sure if ANSI uses ASCII, an ISO-8859-*
    or UTF-* encoding, or something else.

    sherm--

    Web Hosting by West Virginians, for West Virginians: http://wv-www.net
    Cocoa programming in Perl: http://camelbones.sourceforge.net
  • On Oct 25, 2006, at 6:09 AM, Richard Salvatierra wrote:

    > This is working fine.  Does anyone see and issues (All string data
    > will have come from a mac);
    > NSData *data = [aString dataUsingEncoding: NSMacOSRomanStringEncoding]
    >

    The basic problem with this sort of thing is that RTF contents are
    not really a string, because they do not have a well-defined
    encoding.  It is better never to put the RTF data into an NSString in
    the first place, rather than to try to figure out how to get them in
    and out.  Picking an encoding will more or less work, but not
    necessarily for all text.  We try to handle RTF contents as NSData
    everywhere, for just this reason.  Look for the point at which your
    RTF gets into an NSString, and try changing that to NSData.

    > One other question:  To test for the presence of an rtf commented
    > string I do the following.  What is a better way?
    >
    > NSRange rtfRange = [aString rangeOfString: @"rtf1"];
    > if(NSNotFound != rtfRange.location){
    > // rich text
    > }

    We usually look for the 5 bytes "{\\rtf" to detect RTF data.  If you
    use NSAttributedString methods and do not specify the type of the
    document you are importing, NSAttributedString will automatically
    detect RTF along with many other formats.

    Douglas Davidson
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today