• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Adding carriage-return behavior to NSTextView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Adding carriage-return behavior to NSTextView


  • Subject: Re: Adding carriage-return behavior to NSTextView
  • From: Esteban <email@hidden>
  • Date: Tue, 25 Dec 2001 20:57:50 -0800

Andrew,

The way I've done this is by subclassing NSTextView. What I do as far as Interface Builder is put in a Custom View, and with the inspector change its class to my KeyDownTextView class, then I enclose that in an NSScrollView (Layout menu -> Make subviews of -> Scroll View). I also make sure I got an outlet from my nib controller to both the KeyDownTextView subclass of NSTextView, and the NSScrollView.

In code I do the following ( you may need more or less stuff to set depending on the functionality you want for the NSTextView subclass to have)

I'm calling my instance of my KeyDownTextView , sendTextView, and my outlet to the NSScrollView containing it, sendTextScrollView
---
sendTextView = [[KeyDownTextView alloc] initWithFrame:[[sendTextScrollView contentView] frame]];
[sendTextView setHorizontallyResizable:YES];
[sendTextView setVerticallyResizable:YES];
[sendTextView setAutoresizingMask:NSViewWidthSizable];
[sendTextView setSelectable:YES];
[sendTextView setEditable:YES];
[sendTextView setRichText:YES];
[sendTextView setImportsGraphics:NO];
[sendTextView setUsesFontPanel:YES];
[sendTextView setUsesRuler:YES];
[sendTextView setDelegate:self];
// [sendTextView setContinuousSpellCheckingEnabled:[[NSUserDefaults standardUserDefaults] boolForKey:SPELL_CHECKING_ON]];

[sendTextScrollView setBorderType:NSBezelBorder];
[sendTextScrollView setHasVerticalScroller:YES];
[sendTextScrollView setHasHorizontalScroller:NO];
[sendTextScrollView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
[[sendTextScrollView contentView] setAutoresizesSubviews:YES];
[sendTextScrollView setDocumentView:sendTextView];
---

setting the sendTextView to being the sendTextScrollView's documentView allows similar functionality to the default NSTextView and NSScrollView set up you get when dropping an NSTextView from the IB palette.

For an idea of what the KeyDownTextView subclass of NSTextView I wrote looks like, i've pasted it below (sorry for the long message)

KeyDownTextView.h
------------------------------------------------------------------------------------------------------
#import <Cocoa/Cocoa.h>

@interface KeyDownTextView : NSTextView
{
//i really don't need nothing here myself but you may.
}
@end
------------------------------------------------------------------------------------------------------


KeyDownTextView.m
------------------------------------------------------------------------------------------------------
#import "KeyDownTextView.h"

@implementation KeyDownTextView

- (BOOL)enterHit { return NO; }
- (BOOL)returnHit { return NO; }
- (BOOL)optionreturnHit { return NO; }
- (BOOL)optionenterHit { return NO; }

- (void)keyDown:(NSEvent *)theEvent
{
if ([[theEvent charactersIgnoringModifiers] characterAtIndex:0] == 13)
{
if(NSEventMaskFromType([theEvent type]) == NSAlternateKeyMask)
{
if([[self delegate] respondsToSelector:@selector(optionreturnHit)])
{
if([[self delegate] optionreturnHit]) return;
}
}
else
{
if ([[self delegate] respondsToSelector:@selector(returnHit)])
{
if ([[self delegate] returnHit]) return;
}
}
}
else if ([[theEvent charactersIgnoringModifiers] characterAtIndex:0] == 3)
{
if(NSEventMaskFromType([theEvent type]) == NSAlternateKeyMask)
{
if([[self delegate] respondsToSelector:@selector(optionenterHit)])
{
if([[self delegate] optionenterHit]) return;
}
}
else
{
if ([[self delegate] respondsToSelector:@selector(enterHit)])
{
if ([[self delegate] enterHit]) return;
}
}
}
[super keyDown:theEvent];

return;
}

- (void)reset { [self setString:@""]; }

- (BOOL)acceptsFirstResponder { return YES; }

@end
------------------------------------------------------------------------------------------------------

More or less that's what I'm using for an application I'm writing. Anyways, maybe someone has a much neater and perhaps better example :)

If you notice in the KeyDownTextView.m's keyDown: method, the KeyDownTextView is its own delegate, you can in code set the delegate to something else and write your own optionreturnHit, optionenterHit, returnHit, enterHit.

I basically used looked at the source code for Eric Peyton's Fire.app (http://epicware.com/fire.html) and figured out how to do this subclassing of NSTextView from there.

Hope this helps.

-Esteban

On Tuesday, December 25, 2001, at 05:19 PM, Andrew Begel wrote:

How can I add a carriage return behavior to an NSTextView? It seems that the only way is to override the insertNewLine() method by subclassing NSTextView. However, when I create an NSTextView via IB, I get an NSScrollView wrapping the NSTextView, and it subsequently won't let me select a custom class for the enclosed NSTextView. Does NSTextView send its key events to a delegate I could override? Or should I be creating the NSTextView via code instead of by IB?

Thanks,

Andrew
_______________________________________________
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.


  • Follow-Ups:
    • Re: Adding carriage-return behavior to NSTextView
      • From: Max Horn <email@hidden>
References: 
 >Adding carriage-return behavior to NSTextView (From: Andrew Begel <email@hidden>)

  • Prev by Date: Adding carriage-return behavior to NSTextView
  • Next by Date: Lists as newsgroups (or: give chuq more work!)
  • Previous by thread: Adding carriage-return behavior to NSTextView
  • Next by thread: Re: Adding carriage-return behavior to NSTextView
  • Index(es):
    • Date
    • Thread