Re: Subclassing NSLayoutManager
Re: Subclassing NSLayoutManager
- Subject: Re: Subclassing NSLayoutManager
- From: "Kenneth C. Dyke" <email@hidden>
- Date: Sun, 7 Jul 2002 00:11:22 -0400
As one of the (many) folks who've probably worked on an IRC client for
NeXTSTEP/OpenStep/Rhapsody/MacOSX....(and then never finished it or
released it).....
I managed to do this without using a custom layout manager. Here's the
(probably ugly) code from my message view to do what you're talking
about... it basically uses a custom paragraph format per message to do
the kind of rendering alignment you want.
- (void)formatMessageForNickLocation:(float)nickLocation
andTrenchLocation:(float)trenchLocation
{
NSMutableParagraphStyle *style;
NSTextTab *tab1, *tab2;
float nickLength;
NSFont *messageFont;
// Free old string (if it exists)
[messageString release];
// Calculate layout information
messageFont = [messageAttributes messageFont];
nickLength = [messageFont widthOfString:messageNick];
tab1 = [[NSTextTab alloc] initWithType:NSLeftTabStopType
location:nickLocation - nickLength];
tab2 = [[NSTextTab alloc] initWithType:NSLeftTabStopType
location:trenchLocation];
// Set up paragraph attributes.
style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setTabStops:[NSArray arrayWithObjects:tab1, tab2, nil]];
[style setHeadIndent:trenchLocation];
// Build attributed string.
messageString = [[NSMutableAttributedString alloc] initWithString:
[NSString
stringWithFormat:@"\t%@\t%@\n",messageNick,messageText]
attributes:
[messageAttributes messageAttributes]];
[messageString addAttribute:NSParagraphStyleAttributeName
value:style
range:NSMakeRange(0,[messageString length])];
[messageString addAttribute:NSFontAttributeName
value:messageFont
range:NSMakeRange(0,[messageString length])];
// We don't need to hang on to these...
[tab1 release];
[tab2 release];
[style release];
}
The neat part is that you don't need multiple text containers. I just
used an NSTextView subclass that added the rendering for the line
splitting the messages and the nicknames. The other cool part is that
when you copy/paste into another app, the formatting comes out nicely.
-Ken
On Saturday, July 6, 2002, at 11:50 PM, Adrian Rutledge wrote:
Well, now, people are likely going to ask me why I want to go and do a
stupid thing like that. See, though, I'm developing an IRC client.
I'm going for something akin to X-Chat's text output, if anyone has
seen it. It has the nickname of the poster, enclosed in <>s and right
aligned to a line. On the other side of the line is has the message,
left aligned to the same line. In my server connectivity class I've
already got the message cut into pieces, with the prefix (who its
from), the target, and the type of message set as attributes.
According to the NSLayoutManager documentation that is what I subclass
to handle custom attributes. First off, I want to display the name in
one NSTextContainer/NSTextView, the message in another, similar to
X-Chat. Second, I'd like to be able to color messages depending on
what type of message they are, so, use the type of message attribute
as a color hint. According to what little I could find in the
documentation I would use a custom subclass on NSLayoutManager to do
that but I don't know how to go about subclassing it. Specifically,
some pointers at which methods I override to handle custom attributes
and to do the pushing of the nick into one NSTextContainer/NSTextView.
Sorry if this post is rambling or otherwise not very clear, I can make
another run at explaining it if anyone would like.
-Atma
PS: I've got the server connectivity class nearly complete, if anyone
wants a peek at it. Its not commented well and the only thing that's
not done is that it doesn't pass messages up to channels and queries
yet, because I've yet to _CODE_ the channel and query model classes.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.