On Nov 20, 2014, at 3:47 PM, Charles Jenkins <[email protected]> wrote:

> I have an NSTextTable in which each cell will contain the entire text of a 
> small RTF document. I want the user to be able to edit the document’s text in 
> the cell, including applying styles.  
> 
> In this case, a “style" means an NSDictionary containing entries for 
> NSFontAttributeName, NSParagraphStyleAttributeName, and some custom 
> attributes that I need, but I think the layout manager will ignore.
> 
> When I go to apply a style to a paragraph, I call this method in my subclass 
> of NSTextView:  
> 
> -(void)setParagraphStyle:(NSString*)styleName  
> {
>  Document* doc = [[self.window windowController] document];
>  NamedStyle* sty = [doc.styleSet styleByName:styleName];
> 
>  [self.textStorage addAttributes:sty.attributes  
>                            range:self.rangeForUserParagraphAttributeChange];
> 
>  [self didChangeText];  
> }
> 
> 
> Okay, I know that must be very wrong, because although doing it does alter 
> the text as I expect, it also blows up my text table and breaks or merges 
> cells.
> 
> What should I be doing instead, rather than blithely calling 
> addAttributes:range:?

The table-ness of text is stored in the paragraph style.  You are replacing the 
paragraph style.  -addAttributes:range: will preserve existing attributes 
_other than any you specify_.  It will replace the value for any attributes you 
specify.  You are presumably specifying a value for 
NSParagraphStyleAttributeName, so you are replacing the paragraph style.

You need to manually merge the existing paragraph style with the one in 
sty.attributes.  For the table-ness in particular, you need to figure out which 
elements of the existing textBlocks array are for the enclosing table and keep 
those, then append any textBlocks from the new style you want to apply (if 
any).  From the sound of it, you only expect a single enclosing table, so you 
would only keep the first element of the existing textBlocks array.

Regards,
Ken


_______________________________________________

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]

Reply via email to