Re: IRC-like NSTextView.... ?
Re: IRC-like NSTextView.... ?
- Subject: Re: IRC-like NSTextView.... ?
- From: "Kenneth C. Dyke" <email@hidden>
- Date: Sun, 9 Sep 2001 11:07:02 -0700
On Sunday, September 9, 2001, at 05:47 AM, Luc Heinrich wrote:
On dimanche, septembre 9, 2001, at 12:08 , Andreas Monitzer wrote:
What about using a fixed font (Monaco, Courier etc) and inserting
spaces for alignment?
This could be a good (and very simple) workaround, yes, but it implies
that users would not be able to customize display fonts (which is
usually considered as a pretty important feature in an IRC client).
By the way, I only checked the documentation of my 5G27 build, if
tab-stops support is more complete in later builds (5G48 which I did
not install, or later) I would be happy to hear about it.
Heh, once upon a time when I was writing an IRC client I wanted to solve
the same problem. Eventually I came up with the following solution in
my message class. Hopefully just the method implementation will be
enough to get you going. 'nickLocation' was the x coordinate
of the rightmost pixel of the nickname part of the text view, and
trenchlocation was the x location of the message text.
One neat aspect of doing it this way is that cut/paste into other
documents works really nice as well. My app would calculation the nick
and trench location based on the worst-case width of the nickname field
(like how long would the nickname MMMMMMMMMMM be, etc).
- (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];
}
Kenneth Dyke, email@hidden (personal), email@hidden (work)
Sr. Mad Scientist, MacOS X OpenGL Group, Apple Computer, Inc.
C++: The power, elegance and simplicity of a hand grenade.