2023年7月14日发(作者:)
iphone keyboard 自定义键盘 和 表情
前段时间在做自定义键盘上的一些东西,网上找了一些资料,这里了一下。
这里我先声明了3个view:
UIView* pSysKeyboardView; 键盘最底层的view。
UIView* pSysKeyView; 常用键盘的view。
UIScrollView* pManualKeyView; 自定义键盘的view,这里我用了
UIScrollView, 是因为我要在上面放很多的表情。这些表情也是iphone自带的。
UITextView* pTextView; keyboard所对应的文本框,要把通过自定义贾磐输入的内容表情加到对应的文本
同,方法也做了一些改变。
eg:if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:)
name:UIKeyboardDidHideNotification object:nil];
}
else
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShown:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:)
name:UIKeyboardWillHideNotification object:nil];
}
我们可以在keyboardWillShown方法中找到我们想要的键盘的pSysKeyboardView和pSysKeyView。
代码如下:
- (void)keyboardWillShown:(NSNotification *)note
{
isKeyboardVisible = YES;
isManualKeyboard = NO;
pTextView = nil;
if (pSysKeyView==nil || pSysKeyboardView==nil)
1:首先我要找到键盘所在的view, 通过 UIKeyboardDidShowNotification 或者 UIKeyboardWillShowNotification, 因为 {
// locate keyboard view
UIWindow* tempWindow = nil;
UIView* keyboard = nil;
tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
//for (UIWindow *testWindow in [[UIApplication sharedApplication] windows])
//{
// if (![[testWindow class] isEqual:[UIWindow class]])
// {
// tempWindow = testWindow;
// break;
// }
//}
if (tempWindow == nil)
{
return;
}
int viewCount = [ws count];
////find key board view.
for(int i=0; i { keyboard = [ws objectAtIndex:i]; //keyboard view found; add the custom button to it if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { if(([[keyboard description] hasPrefix:@" { break; } } else { if([[keyboard description] hasPrefix:@" break; } } } /////find fist responder. [self findFirstResponder]; if (keyboard!=nil && pTextView!=nil) { viewCount = [ws count]; if (viewCount > 0) { pSysKeyView = [ws objectAtIndex:0]; } } pSysKeyboardView = keyboard; //[self findKeyViewOfSystem:pSysKeyView]; } } 2:找第一响应者,也就是输入的内容对应的文本框,可能是UITextField 和 UITextView. 在keyboardWillShown方法中,调用了这么一个方法: [self findFirstResponder],这个方法就是找出键盘响应的是哪一当一个窗口有多个输入框时,不会出现混乱,因为有时候键盘没有隐藏,用户就有可能直接把焦点设到另外的文本框区分键盘对应的是哪个文本框。 代码 如下: - (void)findFirstResponder { // locate keyboard view UIWindow* tempWindow = nil; int viewCount = 0; pTextView = nil; int count = [[[UIApplication sharedApplication] windows] count]; for (int k=0; k { tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:k]; viewCount = [ws count]; UIView* tempView = nil; for (int index=0; index { tempView = [ws objectAtIndex:index]; if ([self findAndResignFirstResponder:tempView]) { break; } } if (pTextView) { break; } } } - (BOOL)findAndResignFirstResponder:(UIView*)pView { if (tResponder) { if ([pView isKindOfClass:[UITextView class]]) { pTextView = (UITextView*)pView; } else if ([pView isKindOfClass:[UITextField class]]) { pTextView = (UITextView*)pView; } else { pTextView = nil; } return YES; } for (UIView *subView in ws) { if ([self findAndResignFirstResponder:subView]) { return YES; } } return NO; } 3:当然什么时候调用findFirstResponder呢,就是在焦点切换的时候调用就可以了。要在init中加入 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewChanged:) name:UITextFieldTextDidBegfication object: nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewChanged:) name:UITextViewTextDidBotification object: nil]; 在方法textViewChanged中调用findFirstResponder: - (void)textViewChanged:(NSNotification *)note { [self findFirstResponder]; } 4:最重要的一步,创建自定义的键盘,这里是创建表情键盘。- (void)initManualKeyBoardView - (void)initManualKeyBoardView { int frameWidth = 0; int frameHeight = 0; BOOL isNeedCreateKeyboard = NO; if (pSysKeyboardView != nil) { frameWidth = ; frameHeight = ; } else { frameWidth = 320; frameHeight = 216; } if (pManualKeyView!=nil) { int tempWidth = ; int tempHeight = ; if (frameWidth!=tempWidth || frameHeight!=tempHeight) { isNeedCreateKeyboard = YES; } } else { isNeedCreateKeyboard = YES; } if (isNeedCreateKeyboard) { float width = (([faceArray count]>>2)+1)*40.0; pManualKeyView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, frameWidth, frameHeight)]; oundColor = [UIColor colorWithRed:0.4 green:0.6 blue:0.6 alpha:1.0]; //oundColor = [UIColor magentaColor]; [pManualKeyView setContentSize:CGSizeMake(width, )]; Enabled = YES; //UIButton* btnBack= [UIButton buttonWithType:UIButtonTypeCustom]; // [btnBack setFrame:CGRectMake(2.0, 2.0, 36.0, 45.0)]; // oundColor = [UIColor greenColor]; // [btnBack setTitle:[NSString stringWithString:@"up"] forState:UIControlStateNormal]; // [btnBack addTarget:self action:@selector(changeKeyBoard) forControlEvents:UIControlEventTouchUpInside]; // [pManualKeyView addSubview:btnBack]; UIButton* btn = nil; for (int i=0; i<[faceArray count]; i++) { btn = [UIButton buttonWithType:UIButtonTypeCustom]; //int row = (i+1)>>2; //int line = (i+1)&3; int row = (i)>>2; int line = (i)&3; [btn setFrame:CGRectMake(40.0*row+2, 45.0*line+8, 36.0, 41.0)]; //oundColor = [UIColor grayColor]; [btn setBackgroundImage:[UIImage imageNamed:@"speech_bubble_"] forState:UIControlStateNormal]; UIFont *font = [UIFont systemFontOfSize:30.0]; = font; [btn setTitle:[NSString stringWithFormat:@"%@",[faceArray objectAtIndex:i]] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(editText:) forControlEvents:UIControlEventTouchUpInside]; [pManualKeyView addSubview:btn]; } } } 钮加到pSysKeyView 和 pManualKeyView上,进行切换。 - (IBAction)changeKeyBoard { if (pSysKeyboardView!=nil && isKeyboardVisible) { CATransition *animation = [CATransition animation]; on = 0.3f; Function = UIViewAnimationCurveEaseInOut; de = kCAFillModeForwards; = kCATransitionMoveIn; e = kCATransitionFromTop; [ addAnimation:animation forKey:@"animation"]; if (!isManualKeyboard) { [self initManualKeyBoardView]; [pSysKeyboardView addSubview:pManualKeyView]; = YES; = NO; 5: 自定义键盘创建好以后,就是怎么样用了,这里有一个方法;可以用一个键盘外的按钮来控制切换,也可以自己 isManualKeyboard = YES; } else { = NO; = YES; isManualKeyboard = NO; } } } 6:就是怎么样使用,我是定义了一个方法来获取自定义键盘,在整个应用程序中只有一个实例, + (manualKeyboard*)shareInstance { static manualKeyboard* pManualKeyboard = nil; if (pManualKeyboard == nil) { pManualKeyboard = [[manualKeyboard alloc] init]; } return pManualKeyboard; } 7: init方法就是上面个的1和2的代码。 - (id)init { self = [super init]; pSysKeyboardView = nil; pSysKeyView = nil; pManualKeyView = nil; pTextView = nil; isKeyboardVisible = NO; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:) name:UIKeyboardDidHideNotification object:nil]; } else { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShown:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:) name:UIKeyboardWillHideNotification object:nil]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewChanged:) name:UITextFieldTextDidBotification object: nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewChanged:) name:UITextViewTextDidBotification object: nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewTextDidChanged:) name:UITextFieldTeNotification object: nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewTextDidChanged:) name:UITextViewTitingNotification object: nil]; faceArray = [[NSMutableArray alloc] init]; ////// 0 [faceArray addObject:@"uE001"]; [faceArray addObject:@"uE002"]; [faceArray addObject:@"uE003"]; [faceArray addObject:@"uE004"]; [faceArray addObject:@"uE005"]; return self; } faceArray找你个放的就是各种表情,iphone中一共有470+个,这里简单的列出来几个。 似乎有点乱,希望对大家有帮助。最后说几句:在应用程序启动的时候调用[[manualKeyboard shareInstance] initManuew];一次就可以了,应用程序中的文本框都可以使用,也可以根据自己的需要,灵活使用- (IBAction)changeKeyBoard否使用自定义键盘,如果把按钮加到自定义键盘中,所有的文本框都将可以使用,如果是外面,将根据自己需要。
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689265151a226454.html
评论列表(0条)