Re: Viewing a single NSTextStorage with two sets of attributes
Re: Viewing a single NSTextStorage with two sets of attributes
- Subject: Re: Viewing a single NSTextStorage with two sets of attributes
- From: Aki Inoue <email@hidden>
- Date: Mon, 13 Sep 2010 12:12:05 -0700
Andrew,
I usually recommend subclassing NSTextStorage returning different attributes based on a property/flag.
The tricky part is (as you might already encountered) to make sure setting up the property at the right moment.
In normal operations, the Text System queries the attributes roughly in two places: rendering and performing layout.
Capturing the rendering is relatively easy. You can override -[NSTextView drawRect:], or appropriate NSLayoutManager rendering methods: -drawGlyphsForGlyphRange:atPoint: and/or -drawBackgroundForGlyphRange:atPoint:.
The layout side is a bit involved since it can be invoked asynchronously from the background layout and/or non-contiguous layout settings.
You can override two points: -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] and -[NSATSTypesetter layoutCharactersInRange:forLayoutManager:maximumNumberOfLineFragments :].
From there, you can ensure the text storage property is right whenever accessing the layout manager methods.
Aki
On 2010/09/12, at 11:27, Andrew Hughes wrote:
> Hello all,
>
> I'm trying to figure out the cleanest method for viewing one NSTextStorage with multiple attributes (mostly font and paragraph attributes). Basically I am developing a text editor program and I would like to have two different views on the same text, one in a non-paginated mode and another in a paginated mode, and I would like to be able to automatically apply a different font and paragraph style for the paginated mode. I'm not worried about the user being able to edit the attributes in this mode, but they should be able to edit the text itself.
>
> I could simply apply and revert the attributes when the user switches between the modes, and in fact this is my current implementation. However, I would like to allow the user to have multiple windows open on the same document, and not necessarily have them in the same mode, and thus with the same formatting.
>
> My initial thought was to subclass NSLayoutManager's attributedString: method, returning a different attributed string for each view. However, this didn't seem to work. It may have been a problem with glyph caching. I tried to invalidate the glyphs and layout, but the text persisted in the original formatting. Should this have worked?
>
> The next thought was to subclass NSTextStorage and have two different text storage classes that passed on primitive calls to each other (such that they stayed in sync). However, this quickly became more complicated and problematic than I would like because I make extensive use of various delegate calls and the multiple text storages caused problems with this.
>
> So I'm wondering if anyone else has solved this problem. I found one reference to this on the list archive, but the solution was essentially to use another solution that doesn't work for me.
>
> Another possible solution would be to have the NSTextStorage send different attributed strings to the different layout managers. However, I'm unclear as to how the text storage object will know which layout manager is querying it, and thus which set of attributes to apply.
>
> It seems like the best place for me to implement this would be somewhere in the layout manager because the layout manager is associated with the particular view and thus knows which set of attributes should be applied. Unfortunately, the layout manager is complex and it's unclear to me what I should override or change to make this happen.
>
> Any help would be greatly appreciated.
>
> Andrew
> _______________________________________________
>
> 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
_______________________________________________
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