2

I am declaring an NSMutable Attributed String and appending different attributed strings to it. These attributed string may contain images as NSTextattachments.

var historyText : NSMutableAttributedString = NSMutableAttributedString(string: "")
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named: "some_image")
imageAttachment.bounds = CGRect(x:0, y:-3.0, width:    (imageAttachment.image?.size.width)!, height:(imageAttachment.image?.size.height)!)
let imageString = NSAttributedString(attachment: imageAttachment)
historyText.append(imageString)

I then save this attributed text in core data. But when i retrieve this attributed text from core data, the imageAttachment.bounds are lost. Please suggest a way to preserve these bounds. Thanks in advance.

Kriti Sharma
  • 153
  • 1
  • 13
  • How are you saving it in Core Data? – sschale Mar 18 '17 at 22:03
  • @sschale I defined a column of type "Transformable" to save it – Kriti Sharma Mar 19 '17 at 06:44
  • Using `[NSKeyedArchiver archivedDataWithRootObject:historyText]` & `[NSKeyedUnarchiver unarchiveObjectWithData:data]` resets the bounds to {{0, 0}, {0, 0}}. Same with trying to convert them with `NSRTFDTextDocumentType` or `NSHTMLTextDocumentType`. Seems that your solution is to save yourself the bounds of each images, with `enumerateAttribute(_ attrName:, in enumerationRange:, options, using block:)` – Larme Mar 22 '17 at 13:54
  • Unfortunately this problem still persists. The bounds still get reset to `{{0, 0}, {0, 0}}` when unarchiving. – Wizard Jun 29 '18 at 14:26

0 Answers0