Hi,
I have a pair of almost-matching flat-file database apps, iData Pro for the Mac
and iData Mobile for mobile devices. Datafiles for both apps are written to
disk as xml.
Records in iData Mobile datafiles each include a block of plain text, which is
an NSString object that is displayed in a UITextView. The NSString is encoded
like this:
<key>TextString</key>
<string>
Mr. Fred Farkle
1234567 Northwest Broadway Ave.
Austin, TX 77777
Work: 515-555-0987
FAX: 515-555-6789
Home: 515-555-4567
</string>
Records in iData Pro datafiles also include a block of RTFD text, which is an
NSAttributedString object that is displayed in an NSTextView. The
NSAttributedString is encoded as NSData like this:
<key>TextRTFD</key>
<data>
cnRmZAAAAAADAAAAAgAAAAcAAABUWFQucnRmAQAAAC51AQAAKwAA
AAEAAABtAQAAe1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcY29jb2Fy
dGYxMjY1XGNvY29hc3VicnRmMjEwCntcZm9udHRibFxmMFxmbmls
XGZjaGFyc2V0MCBHZW9yZ2lhO30Ke1xjb2xvcnRibDtccmVkMjU1
XGdyZWVuMjU1XGJsdWUyNTU7fQpccGFyZFx0eDU2MFx0eDExMjBc
dHgxNjgwXHR4MjI0MFx0eDI4MDBcdHgzMzYwXHR4MzkyMFx0eDQ0
ODBcdHg1MDQwXHR4NTYwMFx0eDYxNjBcdHg2NzIwCgpcZjBcZnMy
NCBcY2YwIE1yLiBGcmVkIEZhcmtsZVwKMTIzNDU2NyBOb3J0aHdl
c3QgQnJvYWR3YXkgQXZlLlwKQXVzdGluLCBUWCAgNzc3NzdcClwK
V29yazogNTE1LTU1NS0wOTg3XApGQVg6IDUxNS01NTUtNjc4OVwK
SG9tZTogNTE1LTU1NS00NTY3XApcCn0BAAAAIwAAAAEAAAAHAAAA
VFhULnJ0ZhAAAADBUAtVtgEAAAAAAAAAAAAA
</data>
After looking at the Notes app on the iPhone, and fooling around with styled
text and embedded images, I decided to modify iData Mobile to handle RTFD.
What I was expecting was that the RTFD text blocks created on the Mac would
look the same on the iPhone or iPad (assuming the availability of the same
fonts on the mobile device). Of course, the routines used are not very similar
(and I’m not sure why).
Here’s what I do in iData Pro when parsing the NSData TextRTFD for the display
of a new record:
IBOutlet NSTextView *freeformTextView; // the text view in the datafile
window controller
NSData *textRTFD = [record textRTFD]; // extracted from the TextRTFD block
in the xml datafile
NSRange rtfdRange = NSMakeRange(0,[freeformTextView.string length]);
/* clear attributes from the old record */
[freeformTextView setTypingAttributes:nil];
[[freeformTextView textStorage] removeAttribute:NSLinkAttributeName range:
rtfdRange];
/* replace the old record’s RTFD with that of the new record */
[freeformTextView replaceCharactersInRange:allRange withRTFD:textRTFD];
This results in the display of the RTFD styled text with any attachments,
including links, images, and/or sound files. (And this code has been in use for
over a decade with no problems.)
Here’s what I thought would be the equivalent under iOS (obviously I was wrong):
notesContentField = [mw_fieldContentTableCells objectAtIndex:fieldCount];
// the UITextview
NSError *error;
NSDictionary *attrs =
@{NSDocumentTypeDocumentAttribute:NSRTFDTextDocumentType};
NSData * textRTFD = [record textRTFD]; // extracted from the TextRTFD block
in the xml datafile
notesContentField.attributedText = [[NSAttributedString alloc]
initWithData:textRTFD options:attrs documentAttributes:NULL error:&error];
When a record that has complex formatted text, including different fonts; a
variety of font sizes; regular, bold, and italic styles; some underlining; and
an embedded image is displayed in iData Mobile, the font and font attributes of
the first item of the record text are applied to the entire block of text, and
the embedded image disappears. When entering text in a record, it’s possible to
set various parts of the text to bold, italic, and/or underlined in a variety
of combinations, so it’s not that only one style can be displayed. Then there’s
Notes, which seems to be able to save text with mixed formatting and embedded
images.
So, I’d appreciate any ideas about what I’m doing wrong. (I have a different
problem going the other way — encoding the text view’s attributedString as
NSData, replacing the TextRTFD value, and saving it to disk. However, I figure
I’d better work on one problem at a time.)
Thanks,
Mike Wright
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]