Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Text System perplexity. Use Swing instead?



On Thursday, November 15, 2001, at 02:18 PM, Jonathan Hendry wrote:

I think I want to use custom attributes in the NSTextStorage.
A document will be read in and tags will be converted to
attributes. Those attributes should be rendered in a text
object as an uneditable label, telling the user what to
type at that location. When they select that position,
the label disappears.

Think Mad Libs, where you have a paragraph of text
with blanks containing a label like <adjective>
or <verb> mixed in with the text. Something like
that.

Offhand, the easiest way I can think of to do this is by using a text view delegate. You can put your labels into the text storage just as ordinary text, or as attachments, whatever gives the best visual appearance, but you need to keep track of where they are--for that a custom attribute would probably be best.

To make the labels uneditable, and to make them vanish at the appropriate point, you could use the text view delegate methods

- (NSRange)textView:(NSTextView *)textView willChangeSelectionFromCharacterRange:(NSRange)oldSelectedCharRange toCharacterRange:(NSRange)newSelectedCharRange;
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString;

The first of these will allow you to intercept the user's selections, and change them if you wish, and you could change the text at that point to make the labels disappear. You'll know where the labels are by looking for your custom attribute. I'm not sure exactly what you mean by "when they select that position, the label disappears"--what happens when they select a range containing that position?--but this method should let you implement whatever policy you had in mind. The second would let you prevent the user from editing the labels, if that wasn't already precluded by the first.

I think the general principle here is that in Cocoa, you should consider delegates first, categories second, and subclassing only if those don't suffice.

Douglas Davidson


References: 
 >Text System perplexity. Use Swing instead? (From: Jonathan Hendry <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.