cho*_*ise 1 macos cocoa objective-c nstextfield nstextview
我需要一个特殊的文本字段,应该做以下的事情:
我不知道该用什么.
NSTextView看起来不错,但是我无法在输入时设置操作并按Enter键导致换行
NSTextField没有tab键支持,shift-enter不起作用.
有任何想法吗?谢谢!
最好的办法是子类化NSTextView以获得所需的功能.这是一个简单的例子:
MyTextView.h
@interface MyTextView : NSTextView
{
id target;
SEL action;
}
@property (nonatomic, assign) id target;
@property (nonatomic, assign) SEL action;
@end
Run Code Online (Sandbox Code Playgroud)
MyTextView.m
@implementation MyTextView
@synthesize target;
@synthesize action;
- (void)keyDown:(NSEvent *)theEvent
{
if ([theEvent keyCode] == 36) // enter key
{
NSUInteger modifiers = [theEvent modifierFlags];
if ((modifiers & NSShiftKeyMask) || (modifiers & NSAlternateKeyMask))
{
// shift or option/alt held: new line
[super insertNewline:self];
}
else
{
// straight enter key: perform action
[target performSelector:action withObject:self];
}
}
else
{
// allow NSTextView to handle everything else
[super keyDown:theEvent];
}
}
@end
Run Code Online (Sandbox Code Playgroud)
设定目标和行动将按如下方式进行:
[myTextView setTarget:someController];
[mytextView setAction:@selector(omgTheUserPressedEnter:)];
Run Code Online (Sandbox Code Playgroud)
有关密钥代码和全套NSResponder消息的更多详细信息insertNewline:,请参阅我关于密钥代码的问题的优秀答案NSEvent:在哪里可以找到与Cocoa的NSEvent类一起使用的密钥代码列表?
| 归档时间: |
|
| 查看次数: |
557 次 |
| 最近记录: |