TextView:如何监听或处理空格键事件 [英] TextView: How to listen or handle space bar key event

查看:181
本文介绍了TextView:如何监听或处理空格键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以'return'是'insertNewline',我喜欢,但在上帝的绿色地球是插入空间?



所以我试图在用户完成该单词后(即当他们点击空格键或返回时)调用拼写检查(对于关键字)。但爸爸需要检测空格。正在执行...

  / *监听命令* / 
- (BOOL )textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector
{
BOOL result = NO;
if(@selector(insertNewline :) == aSelector){
//做某事
result = YES;
return result;
}
return result;


解决方案

(UITextView *)textView shouldChangeTextInRange:(NSRange)text()()()()范围
replacementText:(NSString *)text
{
//添加的任何新字符作为text参数传递
if([text isEqualToString:@\ n]){
//当按下返回键时的代码。
}
else if([text isEqualToString:@])
{
//空格键按下时的代码。
}
//对于任何其他字符返回TRUE,以便文本被添加到视图
return TRUE;
}


Okay so 'return' is 'insertNewline', I love that, but where in god's green earth is the insertSpace?

So i'm trying to invoke a spellcheck (for a keyword) after the user completes the word (aka, when they hit space bar or return ). But daddy needs to detect space-bar. Heres the textView delegate method i'm implementing...

/* listen to commands */ 
-(BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector
{
    BOOL result = NO;
    if (@selector(insertNewline:) == aSelector) {
    // does something 
    result = YES; 
    return result; 
}
return result; 

解决方案

use the delegate method of UITextView as follows to achieve this...

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
 replacementText:(NSString *)text
{
    // Any new character added is passed in as the "text" parameter
    if ([text isEqualToString:@"\n"]) {
        //code when return key pressed.
    }
    else if([text isEqualToString:@" "])
    {
        //code when space key pressed.
    }
    // For any other character return TRUE so that the text gets added to the view
    return TRUE;
}

这篇关于TextView:如何监听或处理空格键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆