UIEditBoxImpl-ios.mm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2012 James Chen
  4. Copyright (c) 2013-2015 zilongshanren
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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. #include "ui/UIEditBox/UIEditBoxImpl-ios.h"
  24. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  25. #define kLabelZOrder 9999
  26. #include "ui/UIEditBox/UIEditBox.h"
  27. #include "base/CCDirector.h"
  28. #include "2d/CCLabel.h"
  29. #import "platform/ios/CCEAGLView-ios.h"
  30. #import <Foundation/Foundation.h>
  31. #import <UIKit/UIKit.h>
  32. #import "ui/UIEditBox/iOS/CCUIEditBoxIOS.h"
  33. #define getEditBoxImplIOS() ((cocos2d::ui::EditBoxImplIOS *)_editBox)
  34. NS_CC_BEGIN
  35. namespace ui {
  36. EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
  37. {
  38. return new EditBoxImplIOS(pEditBox);
  39. }
  40. EditBoxImplIOS::EditBoxImplIOS(EditBox* pEditText)
  41. : EditBoxImplCommon(pEditText)
  42. , _systemControl(nullptr)
  43. {
  44. }
  45. EditBoxImplIOS::~EditBoxImplIOS()
  46. {
  47. [_systemControl release];
  48. _systemControl = nil;
  49. }
  50. void EditBoxImplIOS::createNativeControl(const Rect& frame)
  51. {
  52. auto glview = cocos2d::Director::getInstance()->getOpenGLView();
  53. Rect rect(0, 0, frame.size.width * glview->getScaleX(), frame.size.height * glview->getScaleY());
  54. float factor = cocos2d::Director::getInstance()->getContentScaleFactor();
  55. rect.size.width /= factor;
  56. rect.size.height /= factor;
  57. _systemControl = [[UIEditBoxImplIOS_objc alloc] initWithFrame:CGRectMake(rect.origin.x,
  58. rect.origin.y,
  59. rect.size.width,
  60. rect.size.height)
  61. editBox:this];
  62. }
  63. bool EditBoxImplIOS::isEditing()
  64. {
  65. return [_systemControl isEditState] ? true : false;
  66. }
  67. void EditBoxImplIOS::doAnimationWhenKeyboardMove(float duration, float distance)
  68. {
  69. if ([_systemControl isEditState] || distance < 0.0f) {
  70. [_systemControl doAnimationWhenKeyboardMoveWithDuration:duration distance:distance];
  71. }
  72. }
  73. void EditBoxImplIOS::setNativeFont(const char* pFontName, int fontSize)
  74. {
  75. UIFont* textFont = constructFont(pFontName, fontSize);
  76. if (textFont != nil) {
  77. [_systemControl setFont:textFont];
  78. }
  79. }
  80. void EditBoxImplIOS::setNativeFontColor(const Color4B& color)
  81. {
  82. _systemControl.textColor = [UIColor colorWithRed:color.r / 255.0f
  83. green:color.g / 255.0f
  84. blue:color.b / 255.0f
  85. alpha:color.a / 255.f];
  86. }
  87. void EditBoxImplIOS::setNativePlaceholderFont(const char* pFontName, int fontSize)
  88. {
  89. UIFont* textFont = constructFont(pFontName, fontSize);
  90. if (textFont != nil) {
  91. [_systemControl setPlaceholderFont:textFont];
  92. }
  93. }
  94. void EditBoxImplIOS::setNativePlaceholderFontColor(const Color4B& color)
  95. {
  96. [_systemControl setPlaceholderTextColor:[UIColor colorWithRed:color.r / 255.0f
  97. green:color.g / 255.0f
  98. blue:color.b / 255.0f
  99. alpha:color.a / 255.f]];
  100. }
  101. void EditBoxImplIOS::setNativeInputMode(EditBox::InputMode inputMode)
  102. {
  103. [_systemControl setInputMode:inputMode];
  104. auto oldPos = _editBox->getPosition();
  105. _editBox->setPosition(oldPos + Vec2(10,10));
  106. _editBox->setPosition(oldPos);
  107. }
  108. void EditBoxImplIOS::setNativeInputFlag(EditBox::InputFlag inputFlag)
  109. {
  110. [_systemControl setInputFlag:inputFlag];
  111. }
  112. NSString* removeSiriString(NSString* str)
  113. {
  114. NSString* siriString = @"\xef\xbf\xbc";
  115. return [str stringByReplacingOccurrencesOfString:siriString withString:@""];
  116. }
  117. const char* EditBoxImplIOS::getText(void)
  118. {
  119. return [removeSiriString(_systemControl.text) UTF8String];
  120. }
  121. void EditBoxImplIOS::setNativeReturnType(EditBox::KeyboardReturnType returnType)
  122. {
  123. [_systemControl setReturnType:returnType];
  124. }
  125. void EditBoxImplIOS::setNativeTextHorizontalAlignment(cocos2d::TextHAlignment alignment)
  126. {
  127. [_systemControl setTextHorizontalAlignment:alignment];
  128. }
  129. void EditBoxImplIOS::setNativeText(const char* pText)
  130. {
  131. NSString* nsText =[NSString stringWithUTF8String:pText];
  132. if ([nsText compare:_systemControl.text] != NSOrderedSame) {
  133. _systemControl.text = nsText;
  134. }
  135. }
  136. void EditBoxImplIOS::setNativePlaceHolder(const char* pText)
  137. {
  138. [_systemControl setPlaceHolder:[NSString stringWithUTF8String:pText]];
  139. }
  140. void EditBoxImplIOS::setNativeVisible(bool visible)
  141. {
  142. [_systemControl setVisible:visible];
  143. }
  144. void EditBoxImplIOS::updateNativeFrame(const Rect& rect)
  145. {
  146. auto glview = cocos2d::Director::getInstance()->getOpenGLView();
  147. CCEAGLView *eaglview = (CCEAGLView *) glview->getEAGLView();
  148. float factor = eaglview.contentScaleFactor;
  149. [_systemControl updateFrame:CGRectMake(rect.origin.x / factor,
  150. rect.origin.y / factor,
  151. rect.size.width / factor,
  152. rect.size.height / factor)];
  153. }
  154. const char* EditBoxImplIOS::getNativeDefaultFontName()
  155. {
  156. const char* pDefaultFontName = [[_systemControl getDefaultFontName] UTF8String];
  157. return pDefaultFontName;
  158. }
  159. void EditBoxImplIOS::nativeOpenKeyboard()
  160. {
  161. [_systemControl setVisible:YES];
  162. [_systemControl openKeyboard];
  163. }
  164. void EditBoxImplIOS::nativeCloseKeyboard()
  165. {
  166. [_systemControl closeKeyboard];
  167. }
  168. UIFont* EditBoxImplIOS::constructFont(const char *fontName, int fontSize)
  169. {
  170. CCASSERT(fontName != nullptr, "fontName can't be nullptr");
  171. CCEAGLView *eaglview = static_cast<CCEAGLView *>(cocos2d::Director::getInstance()->getOpenGLView()->getEAGLView());
  172. float retinaFactor = eaglview.contentScaleFactor;
  173. NSString * fntName = [NSString stringWithUTF8String:fontName];
  174. fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
  175. auto glview = cocos2d::Director::getInstance()->getOpenGLView();
  176. float scaleFactor = glview->getScaleX();
  177. if (fontSize == -1)
  178. {
  179. fontSize = _systemControl.frameRect.size.height*2/3;
  180. }
  181. else
  182. {
  183. fontSize = fontSize * scaleFactor / retinaFactor;
  184. }
  185. UIFont *textFont = nil;
  186. if (strlen(fontName) > 0)
  187. {
  188. textFont = [UIFont fontWithName:fntName size:fontSize];
  189. if (textFont == nil) {
  190. textFont = [UIFont systemFontOfSize:fontSize];
  191. }
  192. }
  193. else
  194. {
  195. textFont = [UIFont systemFontOfSize:fontSize];
  196. }
  197. return textFont;
  198. }
  199. }
  200. NS_CC_END
  201. #endif /* #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) */