Custom NSTextView and scrolling problem
Custom NSTextView and scrolling problem
- Subject: Custom NSTextView and scrolling problem
- From: Ricky Sharp <email@hidden>
- Date: Mon, 15 Nov 2004 12:15:31 -0600
In a previous thread, I had asked about shadows and custom NSTextField/Views. Ignore that post as I've decided to do a slight change in direction.
What I now have are simple subclasses of NSTextField and NSTextView.
For my NSTextField subclass, I have this for initWithFrame:
- (id)initWithFrame:(NSRect)frame
{
if (self = [super initWithFrame:frame])
{
[self setBordered:NO];
[self setBezeled:NO];
[self setDrawsBackground:NO];
[self setFont:[NSFont boldSystemFontOfSize:24]];
[[self cell] setScrollable:YES];
}
return self;
}
I also provide an implementation of drawRect that will render the text with the appropriate shadow effect I wanted.
Then, for my NSTextView subclass (which is designated as my field editor), I have this for initWithFrame:
- (id)initWithFrame:(NSRect)frame
{
if (self = [super initWithFrame:frame])
{
[self setDrawsBackground:YES];
[self setFieldEditor:YES];
[self setTextContainerInset:NSMakeSize (5,5)];
[self setInsertionPointColor:[NSColor redColor]];
}
return self;
}
Here, I do _not_ provide an implementation of drawRect. While the text is being edited, it will not have a shadow. But I can live with such a thing. Once editing ends (and the field editor is thus hidden), the edited custom NSTextField will re-render itself with the shadowed text.
However, another problem has cropped up. While the text container's inset is initially honored, as I type in more text than can fit (which thus starts to scroll the text to the left), the left-most edge of the text now falls on the left edge of the text view frame. This leads to the text drawing over my 5-pixel margin.
I cannot figure out a way to have the text container's bounds always be 5 pixels inset from the text view frame. Is this a bug with AppKit?
BTW, the reason for the 5-pixel margin is so that I can draw a custom focus ring.
I'm very close to scrapping the idea of subclassing all these text-handling classes and instead writing just a custom NSView or NSControl. In such a class, I'd manage drawing the string myself (e.g. NSAttributedString's drawInRect) and processing text input events to manipulate the text.
But another thing I could do is to get rid of all text-input in my app and put that responsibility to another app. What I have is a kiosk type of app where children can choose their name from a list to "log in". The original Carbon version of the app offers a UI to also add names to the list. I could just create Cocoa app that uses the standard Aqua UI to serve as an "Admin" tool to let users manage the names in the list (as well as other tasks).
Is it not possible (or practical) to have a custom NSTextField/View?
--
Rick Sharp
Instant Interactive(tm)
_______________________________________________
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