KeyboardViewController.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // KeyboardViewController.m
  3. // CustomKeyboard
  4. //
  5. // Created by Ternence on 2021/5/14.
  6. //
  7. #import "KeyboardViewController.h"
  8. #import "CCToolBar.h"
  9. #import "CCLeftTableView.h"
  10. #import "Masonry.h"
  11. #import "CCCenterView.h"
  12. #import "CCRightView.h"
  13. #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
  14. #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.width
  15. #define imageNamed(s) [UIImage imageNamed:s]
  16. static CGFloat KEYBOARDHEIGHT = 256;
  17. @interface KeyboardViewController ()
  18. <CCTopBarDelegate,
  19. CCLeftViewDelegate,
  20. CCCenterViewDelegate,
  21. CCRightViewDelegate>
  22. /// 用于设置键盘自定义高度
  23. @property (nonatomic, assign) NSLayoutConstraint *heightConstraint;
  24. @end
  25. @implementation KeyboardViewController
  26. - (void)prepareHeightConstraint {
  27. if (self.heightConstraint == nil) {
  28. UILabel *dummyView = [[UILabel alloc] initWithFrame:CGRectZero];
  29. dummyView.translatesAutoresizingMaskIntoConstraints = NO;
  30. [self.view addSubview:dummyView];
  31. self.heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:KEYBOARDHEIGHT];
  32. self.heightConstraint.priority = 750;
  33. [self.view addConstraint:self.heightConstraint];
  34. } else {
  35. self.heightConstraint.constant = KEYBOARDHEIGHT;
  36. }
  37. }
  38. - (void)viewWillAppear:(BOOL)animated {
  39. [super viewWillAppear:animated];
  40. [self prepareHeightConstraint];
  41. }
  42. /// 重写的父类方法,苹果建议在此处updateViewConstraints
  43. - (void)updateViewConstraints {
  44. [super updateViewConstraints];
  45. if (self.view.frame.size.width == 0 && self.view.frame.size.height == 0) {
  46. return;
  47. }
  48. [self prepareHeightConstraint];
  49. }
  50. - (void)viewDidLoad {
  51. [super viewDidLoad];
  52. [self setupUI];
  53. }
  54. - (void)setupUI {
  55. CGFloat toolBarHeight = 40;
  56. CGFloat bottom = 5;
  57. CGFloat buttonSpace = 8;
  58. CGFloat eachButtonHeight = (KEYBOARDHEIGHT - toolBarHeight - 10 - 8 * 3 - bottom) / 4;
  59. CCToolBar *toolBar = [[CCToolBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, toolBarHeight)];
  60. [self.view addSubview:toolBar];
  61. CCLeftTableView *leftTableView = [[CCLeftTableView alloc]initWithFrame:CGRectMake(5, toolBarHeight + 10, 60, eachButtonHeight * 3 + buttonSpace * 2)];
  62. [self.view addSubview:leftTableView];
  63. CCCenterView *centerView = [[CCCenterView alloc] initWithFrame:CGRectMake(leftTableView.frame.size.width + leftTableView.frame.origin.x, leftTableView.frame.origin.y, SCREEN_WIDTH - leftTableView.frame.origin.x - leftTableView.frame.size.width - 68, eachButtonHeight * 4 + buttonSpace * 3)];
  64. [self.view addSubview:centerView];
  65. CCRightView *rightView = [[CCRightView alloc] initWithFrame:CGRectMake(centerView.frame.size.width + centerView.frame.origin.x, leftTableView.frame.origin.y, 60, eachButtonHeight * 4 + buttonSpace * 3)];
  66. [self.view addSubview:rightView];
  67. UIButton *symbolButton = [UIButton buttonWithType:UIButtonTypeCustom];
  68. symbolButton.clipsToBounds = YES;
  69. symbolButton.layer.cornerRadius = 5;
  70. symbolButton.frame = CGRectMake(leftTableView.frame.origin.x, KEYBOARDHEIGHT - bottom - eachButtonHeight, 60, eachButtonHeight);
  71. symbolButton.backgroundColor = [UIColor colorWithRed:189/255.0 green:189/255.0 blue:196/255.0 alpha:1];
  72. [symbolButton setTitle:@"符" forState:UIControlStateNormal];
  73. [symbolButton addTarget:self action:@selector(handleInputModeListFromView:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  74. [self.view addSubview:symbolButton];
  75. toolBar.delegate = self;
  76. leftTableView.delegate = self;
  77. centerView.delegate = self;
  78. rightView.delegate = self;
  79. }
  80. #pragma mark - CCToolBarDelegate
  81. /**
  82. CCTopBarActionSetting = 0, //设置
  83. CCTopBarActionEmoji, //表情
  84. CCTopBarActionVoice, //语音
  85. CCTopBarActionSwitchBoard, //切换键盘
  86. CCTopBarActionRecommend, //推荐语句
  87. CCTopBarActionSearch, //搜索
  88. CCTopBarActionQuit //退出键盘
  89. */
  90. - (void)didClickToolBar:(CCTopBarAction)action {
  91. NSLog(@"===%ld",action);
  92. if (action == CCTopBarActionSetting) {
  93. }else if (action == CCTopBarActionEmoji) {
  94. }else if (action == CCTopBarActionVoice) {
  95. }else if (action == CCTopBarActionSwitchBoard) {
  96. }else if (action == CCTopBarActionRecommend) {
  97. }else if (action == CCTopBarActionSearch) {
  98. }else if (action == CCTopBarActionQuit) {
  99. [self dismissKeyboard];
  100. }
  101. }
  102. #pragma mark - CCLeftViewDelegate
  103. - (void)leftViewDidClick:(id)keyboardModel {
  104. [self didClickKeyboardModel:keyboardModel];
  105. }
  106. #pragma mark - CCCenterViewDelegate
  107. - (void)centerViewDidClick:(CCKeyboardModel *)keyboardModel {
  108. NSLog(@"===%@",keyboardModel.string);
  109. [self didClickKeyboardModel:keyboardModel];
  110. }
  111. #pragma mark - CCRightViewDelegate
  112. - (void)rightViewDidClick:(CCKeyboardModel *)keyboardModel {
  113. [self didClickKeyboardModel:keyboardModel];
  114. }
  115. - (void)didClickKeyboardModel:(CCKeyboardModel *)keyboardModel {
  116. if (keyboardModel.keyboardAction == CCKeyboardActionText) {
  117. [self.textDocumentProxy insertText:keyboardModel.string];
  118. } else if (keyboardModel.keyboardAction == CCKeyboardActionWrap) {
  119. [self.textDocumentProxy insertText:@"\n"];
  120. } else if (keyboardModel.keyboardAction == CCKeyboardActionSpace) {
  121. [self.textDocumentProxy insertText:@" "];
  122. } else if (keyboardModel.keyboardAction == CCKeyboardActionDelete) {
  123. [self.textDocumentProxy deleteBackward];
  124. } else if (keyboardModel.keyboardAction == CCKeyboardActionSymbol) {
  125. } else if (keyboardModel.keyboardAction == CCKeyboardActionSwitchBoard) {
  126. [self advanceToNextInputMode];
  127. }
  128. }
  129. - (void)textWillChange:(id<UITextInput>)textInput {
  130. // The app is about to change the document's contents. Perform any preparation here.
  131. }
  132. - (void)textDidChange:(id<UITextInput>)textInput {
  133. // The app has just changed the document's contents, the document context has been updated.
  134. UIColor *textColor = nil;
  135. if (self.textDocumentProxy.keyboardAppearance == UIKeyboardAppearanceDark) {
  136. textColor = [UIColor whiteColor];
  137. } else {
  138. textColor = [UIColor blackColor];
  139. }
  140. }
  141. @end