カスタムキーボード画面1
顔ボタンを押すとキーボードが変化します。
カスタムキーボード画面2
変化後のカスタムキーボード。ABCボタンで元に戻ります。
このUITextFieldから派生したクラスを画面(*.xib)のカスタムキーボードで入力したいテキストフィールドに設定します。
// //-------------------------------------------------- // CustomTextField.h //-------------------------------------------------- #import#import "CustomKeybord.h" @interface CustomTextField : UITextField { @private UIView *inputAccessoryView_; BOOL useCustomKeybord_; NSString *customBtnTxt_; } @property (nonatomic, retain) IBOutlet CustomKeybord *keybord; @property (nonatomic, retain) UIButton *compButton; - (IBAction)faceBtn1:(id)sender; - (IBAction)faceBtn2:(id)sender; - (IBAction)faceBtn3:(id)sender; @end //--------------------------------------------------------- // CustomTextField.m //--------------------------------------------------------- #import "CustomTextField.h" @implementation CustomTextField @synthesize keybord=keybord_; @synthesize compButton; - (void)changeKeybord:(id)sender { self->useCustomKeybord_ = !self->useCustomKeybord_; self->customBtnTxt_ = (self->useCustomKeybord_) ? @"ABC" : @"(^-^)"; [self reloadInputViews]; } - (UIView *)inputAccessoryView { if (self->inputAccessoryView_ == nil) { CGFloat width = [[UIScreen mainScreen] applicationFrame].size.width; CGRect accessFrame = CGRectMake(0.0, 0.0, width, 50.0); self->inputAccessoryView_ = [[UIView alloc] initWithFrame:accessFrame]; self->inputAccessoryView_.backgroundColor = [UIColor blueColor]; self.compButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; self.compButton.frame = CGRectMake(20.0, 5.0, 80.0, 40.0); [self.compButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.compButton addTarget:self action:@selector(changeKeybord:) forControlEvents:UIControlEventTouchUpInside]; [self->inputAccessoryView_ addSubview:compButton]; self->customBtnTxt_ = @"(^-^)"; } [self.compButton setTitle: self->customBtnTxt_ forState:UIControlStateNormal]; return self->inputAccessoryView_; } - (UIView *)inputView { if (self->useCustomKeybord_) { if (self->keybord_ == nil) { UINib *customKeybord = [UINib nibWithNibName:@"CustomKeybord" bundle:nil]; [customKeybord instantiateWithOwner:self options:nil]; } return self->keybord_; } else { return [super inputView]; } } - (IBAction)faceBtn1:(id)sender { self.text = [NSString stringWithFormat:@"%@(^-^)", self.text]; } - (IBAction)faceBtn2:(id)sender { self.text = [NSString stringWithFormat:@"%@(^^;)", self.text]; } - (IBAction)faceBtn3:(id)sender { self.text = [NSString stringWithFormat:@"%@(-.-;)", self.text]; } @end
カスタムキーボードとしてこのようなNibを作ります。
上のNibに対応するUIViewの派生クラスを作ります。
// //------------------------ // CustomKeybord.h //------------------------ #import@interface CustomKeybord : UIView { } @end //------------------------------- // CustomKeybord.m //------------------------------- #import "CustomKeybord.h" @implementation CustomKeybord - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } - (void)dealloc { [super dealloc]; } @end