Images Being Substituted in UIText .attributedText
Images Being Substituted in UIText .attributedText
- Subject: Images Being Substituted in UIText .attributedText
- From: "Michal L. Wright" <email@hidden>
- Date: Fri, 10 Apr 2015 19:18:22 -0400
Hi,
I have a pair of almost-matching flat-file database apps, iData Pro for the Mac and iData Pro-Mobile for mobile devices. Datafiles for both apps are written to disk as a plist (xml) file and are almost identical in structure. Up till now, iData Pro-Mobile could display only plain text. Now I’m modifying it to display formatted text, like the Mac version.
Datafiles are organized into records.
Each record includes a block of RTFD text, which is an NSAttributedString object that is displayed in an NSTextView in iData Pro and in a UITextView in iData Pro-Mobile. A typical NSAttributedString is encoded as NSData like this in the xml:
<key>TextRTFD</key>
<data>
cnRmZAAAAAADAAAAAgAAAAcAAABUWFQucnRmAQAAAC51AQAAKwAA
AAEAAABtAQAAe1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcY29jb2Fy
dGYxMjY1XGNvY29hc3VicnRmMjEwCntcZm9udHRibFxmMFxmbmls
XGZjaGFyc2V0MCBHZW9yZ2lhO30Ke1xjb2xvcnRibDtccmVkMjU1
XGdyZWVuMjU1XGJsdWUyNTU7fQpccGFyZFx0eDU2MFx0eDExMjBc
dHgxNjgwXHR4MjI0MFx0eDI4MDBcdHgzMzYwXHR4MzkyMFx0eDQ0
ODBcdHg1MDQwXHR4NTYwMFx0eDYxNjBcdHg2NzIwCgpcZjBcZnMy
NCBcY2YwIE1yLiBGcmVkIEZhcmtsZVwKMTIzNDU2NyBOb3J0aHdl
c3QgQnJvYWR3YXkgQXZlLlwKQXVzdGluLCBUWCAgNzc3NzdcClwK
V29yazogNTE1LTU1NS0wOTg3XApGQVg6IDUxNS01NTUtNjc4OVwK
SG9tZTogNTE1LTU1NS00NTY3XApcCn0BAAAAIwAAAAEAAAAHAAAA
VFhULnJ0ZhAAAADBUAtVtgEAAAAAAAAAAAAA
</data>
The UIText and NSAttributedString calls for iOS are a bit different from the ones I use on the Mac, but I now have the mobile app showing formatted text with embedded images. I’ve added “Insert Image” and “Set Font” menu items to the editing menu, and they are working fine. “Insert Image” gives access to saved photos, and “Set Font” includes a font picker and a color picker.
The results are great, except for one rather bizarre glitch — when stepping throught records (and making no changes), images in the currently displayed record are occasionally replaced by images from other previously displayed records.
I’ve hacked around a bit, but don’t really have any idea what could be causing this behavior. It’s almost as though the system is caching the images and getting them mixed up at display time. This occurs in what seems to be an entirely random way. I haven’t been able to find any repeatable pattern of replacements.
A particularly odd thing is that if I select and copy an incorrect image, then paste it into a different datafile, the correct image is pasted in. This seems to indicate some kind of disconnect between the display of images and the underlying attributed text.
Here’s what I do iData Pro when parsing the NSData TextRTFD for the display of a new record:
IBOutlet NSTextView *freeformTextView; // From my RecordViewController.h file. RecordViewController is a UIViewController
NSUInteger length = [freeformTextViewString length];
// Clear attributes displayed for the previous record
NSRange fullllRange = NSMakeRange(0, length);
[freeformTextView setTypingAttributes:nil];
[[freeformTextView textStorage] removeAttribute:NSLinkAttributeName fullllRange];
// Replace the text; use the RTFD version if it is available
NSData *textRTFD = [record textRTFD]; // from the xml datafile
if (textRTFD])
[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 the preceding code has been in use for over a decade with no problems.)
Here’s what I do in iData Pro-Mobile:
// Replace the attributedText; use the RTFD version if it is available
NSData *textRTFD = [record textRTFD];
if (textRTFD)
{
NSError *error;
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: NSRTFDTextDocumentType, NSDocumentTypeDocumentAttribute, nil];
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData:textRTFD options:dict documentAttributes:NULL error:&error];
[freeformTextView setAttributedText:attributedText];
}
Only images are affected. Text formatting — text content, fonts, point sizes, text colors — remains stable. When a datafile is transferred to the Mac and opened in the Mac app, it displays as expected.
Is it obvious that I’m doing something wrong, or could this be an iOS bug?
Thanks,
Mike Wright
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden