Re: using the text system to implement styles
Re: using the text system to implement styles
- Subject: Re: using the text system to implement styles
- From: Douglas Davidson <email@hidden>
- Date: Wed, 6 Jul 2005 12:39:21 -0700
On Jul 6, 2005, at 12:29 PM, Todd Ransom wrote:
I am working on an application that consists primarily of a text
view where a user would enter text and assign styles to portions of
the text. Although the text view would contain attributed text,
users would not be able to assign arbitrary attributes to arbitrary
text -- most formatting would be handled by assigning a style to
the text.
A style consists of anything that can be put in an attributed
string's attributes dictionary plus some other stuff (all caps for
instance). A style can be reconfigured by the user and all text of
that style should adjust to match the new configuration. This leads
me to believe I should store only my custom attributes, adding
style attributes somewhere in the layout process. But I can't seem
to figure out exactly where to do this.
There are many ways that this could be implemented. For example, you
could use a standard text storage to store your styles as custom
attributes in addition to the corresponding standard attributes; then
when a style changed, you would search through the text for
occurrences of your custom attribute, and modify the standard
attributes to match. Alternatively, you could use a custom text
storage subclass, that would store only your custom attributes, but
would synthesize the corresponding standard attributes in response to
any inquiry. You would need to make sure to invalidate layout in the
layout manager for the appropriate ranges, however.
I have looked into subclassing NSLayoutManager and overriding
drawGlyphsForGlyphRange: atPoint: but it is not clear to me how I
should be implementing this method. Do I just use NSString's
drawAtPoint: withAttributes: to draw the string with the attributes
I want?
I have also looked into subclassing NSTextView's drawRect: but I'm
not sure if that's the right thing to do, either. I have seen
several messages that indicate you should avoid overriding drawRect
in a text view if at all possible.
I think you need to study the text system architecture a bit more.
The layout manager stores glyph and layout information about your
text, as calculated by the glyph generator and typesetter. All of
this recognizes only the standard attributes. It is possible to
override this to some extent with a custom glyph generator or
typesetter, but in this case you are interested in applying the
standard attributes, so it would be much easier simply to supply them
to the typesetter in the form it expects.
You also would want to consider what you would want to happen when
the user copies and pastes from your view. If you make sure that the
standard attributes are in fact available, as would be the case in
either of the alternatives I presented above, then the default
copying behavior would at least preserve the standard attributes when
copying to other applications.
Douglas Davidson
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden