CCUIEditBoxIOS.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2012 James Chen
  4. Copyright (c) 2013-2015 zilongshanren
  5. Copyright (c) 2015 Mazyad Alabduljaleel
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #import "ui/UIEditBox/iOS/CCUIEditBoxIOS.h"
  24. #import "ui/UIEditBox/iOS/CCUISingleLineTextField.h"
  25. #import "ui/UIEditBox/iOS/CCUIMultilineTextField.h"
  26. #import "platform/ios/CCEAGLView-ios.h"
  27. #include "base/CCDirector.h"
  28. #define getEditBoxImplIOS() ((cocos2d::ui::EditBoxImplIOS *)_editBox)
  29. @implementation UIEditBoxImplIOS_objc
  30. #pragma mark - Static methods
  31. + (void)initialize
  32. {
  33. [super initialize];
  34. LoadUITextViewCCUITextInputCategory();
  35. LoadUITextFieldCCUITextInputCategory();
  36. }
  37. #pragma mark - Init & Dealloc
  38. - (instancetype)initWithFrame:(CGRect)frameRect editBox:(void *)editBox
  39. {
  40. self = [super init];
  41. if (self) {
  42. _editState = NO;
  43. self.frameRect = frameRect;
  44. self.editBox = editBox;
  45. self.dataInputMode = cocos2d::ui::EditBox::InputFlag::LOWERCASE_ALL_CHARACTERS;
  46. self.keyboardReturnType = cocos2d::ui::EditBox::KeyboardReturnType::DEFAULT;
  47. [self createMultiLineTextField];
  48. }
  49. return self;
  50. }
  51. - (void)dealloc
  52. {
  53. // custom setter cleanup
  54. self.textInput = nil;
  55. [super dealloc];
  56. }
  57. #pragma mark - Properties
  58. - (void)setTextInput:(UIView<UITextInput,CCUITextInput> *)textInput
  59. {
  60. if (_textInput == textInput) {
  61. return;
  62. }
  63. // common init
  64. textInput.backgroundColor = [UIColor clearColor];
  65. textInput.hidden = true;
  66. textInput.returnKeyType = UIReturnKeyDefault;
  67. [textInput ccui_setDelegate:self];
  68. // Migrate properties
  69. textInput.ccui_textColor = _textInput.ccui_textColor ?: [UIColor whiteColor];
  70. textInput.ccui_text = _textInput.ccui_text ?: @"";
  71. textInput.ccui_placeholder = _textInput.ccui_placeholder ?: @"";
  72. textInput.ccui_font = _textInput.ccui_font ?: [UIFont systemFontOfSize:self.frameRect.size.height*2/3];
  73. textInput.ccui_placeholderFont = _textInput.ccui_placeholderFont ?: textInput.ccui_font;
  74. textInput.ccui_placeholderTextColor = _textInput.ccui_placeholderTextColor ?: [UIColor lightGrayColor];
  75. [_textInput resignFirstResponder];
  76. [_textInput removeFromSuperview];
  77. [_textInput release];
  78. _textInput = [textInput retain];
  79. [self setInputFlag:self.dataInputMode];
  80. [self setReturnType:self.keyboardReturnType];
  81. }
  82. #pragma mark - Public methods
  83. - (void)createSingleLineTextField
  84. {
  85. CCUISingleLineTextField *textField = [[[CCUISingleLineTextField alloc] initWithFrame:self.frameRect] autorelease];
  86. textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  87. textField.borderStyle = UITextBorderStyleNone;
  88. [textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
  89. self.textInput = textField;
  90. }
  91. - (void)createMultiLineTextField
  92. {
  93. CCUIMultilineTextField *textView = [[[CCUIMultilineTextField alloc] initWithFrame:self.frameRect] autorelease];
  94. self.textInput = textView;
  95. }
  96. #pragma mark - Public methods
  97. - (void)setFont:(UIFont *)font
  98. {
  99. self.textInput.ccui_font = font;
  100. }
  101. - (void)setTextColor:(UIColor*)color
  102. {
  103. self.textInput.ccui_textColor = color;
  104. }
  105. - (void)setPlaceholderFont:(UIFont *)font
  106. {
  107. self.textInput.ccui_placeholderFont = font;
  108. }
  109. - (void)setPlaceholderTextColor:(UIColor *)color
  110. {
  111. self.textInput.ccui_placeholderTextColor = color;
  112. }
  113. - (void)setInputMode:(cocos2d::ui::EditBox::InputMode)inputMode
  114. {
  115. //multiline input
  116. if (inputMode == cocos2d::ui::EditBox::InputMode::ANY) {
  117. if (![self.textInput isKindOfClass:[UITextView class]]) {
  118. [self createMultiLineTextField];
  119. }
  120. }
  121. else {
  122. if (![self.textInput isKindOfClass:[UITextField class]]) {
  123. [self createSingleLineTextField];
  124. }
  125. }
  126. switch (inputMode)
  127. {
  128. case cocos2d::ui::EditBox::InputMode::EMAIL_ADDRESS:
  129. self.keyboardType = UIKeyboardTypeEmailAddress;
  130. break;
  131. case cocos2d::ui::EditBox::InputMode::NUMERIC:
  132. self.keyboardType = UIKeyboardTypeDecimalPad;
  133. break;
  134. case cocos2d::ui::EditBox::InputMode::PHONE_NUMBER:
  135. self.keyboardType = UIKeyboardTypePhonePad;
  136. break;
  137. case cocos2d::ui::EditBox::InputMode::URL:
  138. self.keyboardType = UIKeyboardTypeURL;
  139. break;
  140. case cocos2d::ui::EditBox::InputMode::DECIMAL:
  141. self.keyboardType = UIKeyboardTypeDecimalPad;
  142. break;
  143. case cocos2d::ui::EditBox::InputMode::SINGLE_LINE:
  144. self.keyboardType = UIKeyboardTypeDefault;
  145. break;
  146. default:
  147. self.keyboardType = UIKeyboardTypeDefault;
  148. break;
  149. }
  150. }
  151. - (void)setKeyboardType:(UIKeyboardType)type
  152. {
  153. self.textInput.keyboardType = type;
  154. }
  155. - (void)setInputFlag:(cocos2d::ui::EditBox::InputFlag)flag
  156. {
  157. self.dataInputMode = flag;
  158. switch (flag)
  159. {
  160. case cocos2d::ui::EditBox::InputFlag::PASSWORD:
  161. //textView can't be used for input password
  162. self.textInput.ccui_secureTextEntry = YES;
  163. break;
  164. case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_WORD:
  165. self.textInput.autocapitalizationType = UITextAutocapitalizationTypeWords;
  166. break;
  167. case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_SENTENCE:
  168. self.textInput.autocapitalizationType = UITextAutocapitalizationTypeSentences;
  169. break;
  170. case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS:
  171. self.textInput.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
  172. break;
  173. case cocos2d::ui::EditBox::InputFlag::SENSITIVE:
  174. self.textInput.autocorrectionType = UITextAutocorrectionTypeNo;
  175. break;
  176. case cocos2d::ui::EditBox::InputFlag::LOWERCASE_ALL_CHARACTERS:
  177. self.textInput.autocapitalizationType = UITextAutocapitalizationTypeNone;
  178. break;
  179. default:
  180. break;
  181. }
  182. }
  183. - (void)setReturnType:(cocos2d::ui::EditBox::KeyboardReturnType)returnType
  184. {
  185. self.keyboardReturnType = returnType;
  186. switch (returnType) {
  187. case cocos2d::ui::EditBox::KeyboardReturnType::DEFAULT:
  188. self.textInput.returnKeyType = UIReturnKeyDefault;
  189. break;
  190. case cocos2d::ui::EditBox::KeyboardReturnType::DONE:
  191. self.textInput.returnKeyType = UIReturnKeyDone;
  192. break;
  193. case cocos2d::ui::EditBox::KeyboardReturnType::SEND:
  194. self.textInput.returnKeyType = UIReturnKeySend;
  195. break;
  196. case cocos2d::ui::EditBox::KeyboardReturnType::SEARCH:
  197. self.textInput.returnKeyType = UIReturnKeySearch;
  198. break;
  199. case cocos2d::ui::EditBox::KeyboardReturnType::GO:
  200. self.textInput.returnKeyType = UIReturnKeyGo;
  201. break;
  202. case cocos2d::ui::EditBox::KeyboardReturnType::NEXT:
  203. self.textInput.returnKeyType = UIReturnKeyNext;
  204. break;
  205. default:
  206. self.textInput.returnKeyType = UIReturnKeyDefault;
  207. break;
  208. }
  209. }
  210. - (void)setTextHorizontalAlignment:(cocos2d::TextHAlignment)alignment
  211. {
  212. self.textInput.ccui_alignment = static_cast<NSTextAlignment>(alignment);
  213. }
  214. - (void)setText:(NSString *)text
  215. {
  216. self.textInput.ccui_text = text;
  217. }
  218. - (NSString *)text
  219. {
  220. return self.textInput.ccui_text ?: @"";
  221. }
  222. - (void)setVisible:(BOOL)visible
  223. {
  224. self.textInput.hidden = !visible;
  225. }
  226. - (NSString *)getDefaultFontName
  227. {
  228. return self.textInput.ccui_font.fontName ?: @"";
  229. }
  230. - (cocos2d::ui::EditBoxDelegate::EditBoxEndAction)getEndAction
  231. {
  232. cocos2d::ui::EditBoxDelegate::EditBoxEndAction action = cocos2d::ui::EditBoxDelegate::EditBoxEndAction::UNKNOWN;
  233. if (self.returnPressed) {
  234. if (self.keyboardReturnType == cocos2d::ui::EditBox::KeyboardReturnType::NEXT) {
  235. action = cocos2d::ui::EditBoxDelegate::EditBoxEndAction::TAB_TO_NEXT;
  236. } else if (self.keyboardReturnType == cocos2d::ui::EditBox::KeyboardReturnType::GO ||
  237. self.keyboardReturnType == cocos2d::ui::EditBox::KeyboardReturnType::SEND) {
  238. action = cocos2d::ui::EditBoxDelegate::EditBoxEndAction::RETURN;
  239. }
  240. }
  241. return action;
  242. }
  243. - (void)setPlaceHolder:(NSString *)text
  244. {
  245. self.textInput.ccui_placeholder = text;
  246. }
  247. - (void)doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance
  248. {
  249. auto view = cocos2d::Director::getInstance()->getOpenGLView();
  250. CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();
  251. [eaglview doAnimationWhenKeyboardMoveWithDuration:duration distance:distance];
  252. }
  253. - (void)updateFrame:(CGRect)rect
  254. {
  255. CGRect frame = self.textInput.frame;
  256. frame.origin = rect.origin;
  257. frame.size = rect.size;
  258. self.textInput.frame = frame;
  259. }
  260. - (void)openKeyboard
  261. {
  262. auto view = cocos2d::Director::getInstance()->getOpenGLView();
  263. CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();
  264. [eaglview addSubview:self.textInput];
  265. [self.textInput becomeFirstResponder];
  266. }
  267. - (void)closeKeyboard
  268. {
  269. [self.textInput resignFirstResponder];
  270. [self.textInput removeFromSuperview];
  271. }
  272. - (BOOL)textFieldShouldReturn:(UITextField *)sender
  273. {
  274. if (sender == self.textInput) {
  275. self.returnPressed = YES;
  276. [sender resignFirstResponder];
  277. }
  278. return NO;
  279. }
  280. - (void)animationSelector
  281. {
  282. auto view = cocos2d::Director::getInstance()->getOpenGLView();
  283. CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();
  284. [eaglview doAnimationWhenAnotherEditBeClicked];
  285. }
  286. #pragma mark - UITextView delegate methods
  287. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
  288. {
  289. CCLOG("textFieldShouldBeginEditing...");
  290. _editState = YES;
  291. _returnPressed = NO;
  292. auto view = cocos2d::Director::getInstance()->getOpenGLView();
  293. CCEAGLView *eaglview = (CCEAGLView *) view->getEAGLView();
  294. if ([eaglview isKeyboardShown]) {
  295. [self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f];
  296. }
  297. getEditBoxImplIOS()->editBoxEditingDidBegin();
  298. return YES;
  299. }
  300. - (BOOL)textViewShouldEndEditing:(UITextView *)textView
  301. {
  302. CCLOG("textFieldShouldEndEditing...");
  303. _editState = NO;
  304. getEditBoxImplIOS()->refreshInactiveText();
  305. const char* inputText = [textView.text UTF8String];
  306. getEditBoxImplIOS()->editBoxEditingDidEnd(inputText, [self getEndAction]);
  307. return YES;
  308. }
  309. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  310. {
  311. int maxLength = getEditBoxImplIOS()->getMaxLength();
  312. if (maxLength < 0)
  313. {
  314. return YES;
  315. }
  316. // Prevent crashing undo bug http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield
  317. if (range.length + range.location > textView.text.length) {
  318. return NO;
  319. }
  320. NSUInteger oldLength = textView.text.length;
  321. NSUInteger replacementLength = text.length;
  322. NSUInteger rangeLength = range.length;
  323. NSUInteger newLength = oldLength - rangeLength + replacementLength;
  324. return newLength <= maxLength;
  325. }
  326. - (void)textViewDidChange:(UITextView *)textView
  327. {
  328. int maxLength = getEditBoxImplIOS()->getMaxLength();
  329. if (textView.markedTextRange == nil) {
  330. if (textView.text.length > maxLength) {
  331. textView.text = [textView.text substringToIndex:maxLength];
  332. }
  333. const char* inputText = [textView.text UTF8String];
  334. getEditBoxImplIOS()->editBoxEditingChanged(inputText);
  335. }
  336. }
  337. #pragma mark - UITextField delegate methods
  338. /**
  339. * Called each time when the text field's text has changed.
  340. */
  341. - (void)textChanged:(UITextField *)textField
  342. {
  343. int maxLength = getEditBoxImplIOS()->getMaxLength();
  344. if (textField.markedTextRange == nil) {
  345. if (textField.text.length > maxLength) {
  346. textField.text = [textField.text substringToIndex:maxLength];
  347. }
  348. const char* inputText = [textField.text UTF8String];
  349. getEditBoxImplIOS()->editBoxEditingChanged(inputText);
  350. }
  351. }
  352. - (BOOL)textFieldShouldBeginEditing:(UITextField *)sender // return NO to disallow editing.
  353. {
  354. CCLOG("textFieldShouldBeginEditing...");
  355. _editState = YES;
  356. auto view = cocos2d::Director::getInstance()->getOpenGLView();
  357. CCEAGLView *eaglview = (CCEAGLView *)view->getEAGLView();
  358. if ([eaglview isKeyboardShown]) {
  359. [self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f];
  360. }
  361. getEditBoxImplIOS()->editBoxEditingDidBegin();
  362. return YES;
  363. }
  364. - (BOOL)textFieldShouldEndEditing:(UITextField *)sender
  365. {
  366. CCLOG("textFieldShouldEndEditing...");
  367. _editState = NO;
  368. const char* inputText = [sender.text UTF8String];
  369. getEditBoxImplIOS()->editBoxEditingDidEnd(inputText, [self getEndAction]);
  370. return YES;
  371. }
  372. /**
  373. * Delegate method called before the text has been changed.
  374. * @param textField The text field containing the text.
  375. * @param range The range of characters to be replaced.
  376. * @param string The replacement string.
  377. * @return YES if the specified text range should be replaced; otherwise, NO to keep the old text.
  378. */
  379. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  380. {
  381. int maxLength = getEditBoxImplIOS()->getMaxLength();
  382. if (maxLength < 0) {
  383. return YES;
  384. }
  385. // Prevent crashing undo bug http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield
  386. if (range.length + range.location > textField.text.length) {
  387. return NO;
  388. }
  389. NSUInteger oldLength = textField.text.length;
  390. NSUInteger replacementLength = string.length;
  391. NSUInteger rangeLength = range.length;
  392. NSUInteger newLength = oldLength - rangeLength + replacementLength;
  393. return newLength <= maxLength;
  394. }
  395. @end