CustomTextField.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. //
  2. // CustomInsetTextField.m
  3. // JasonDevelop
  4. //
  5. // Created by Jason Lee on 2013. 12. 6..
  6. // Copyright (c) jasondevelop. All rights reserved.
  7. //
  8. #import "NSString-Addtions.h"
  9. #import "CommonUtil.h"
  10. #import "CustomTextField.h"
  11. #import "ImageUtil.h"
  12. #define kUIBtnTintColor [UIColor whiteColor]
  13. #define kfBoxCap 12.0f
  14. #define kUIPlaceHolderColor RGBCOLOR(136, 143, 168)
  15. @interface CustomTextField () <UIKeyInput> {
  16. @protected
  17. UIView *_targetSuperView;
  18. CGRect _cRect;
  19. CGRect _superRect;
  20. CGPoint _contentOffset;
  21. CGFloat _marginTop, _naviMargin, _adjustY;
  22. UIBarButtonItem *_btnPrev, *_btnNext, *_btnClose;
  23. UIToolbar *_toolBar;
  24. NSMutableArray *_barItems;
  25. UIBarButtonItem *_btnFlexibleSpace;
  26. UIEdgeInsets _bgInsets;
  27. BOOL _isSecureEntry;
  28. BOOL _isScrollUp;
  29. }
  30. @end
  31. CGRect gKeyboardRect;
  32. @implementation CustomTextField
  33. @dynamic delegate;
  34. - (id)initWithFrame:(CGRect)frame {
  35. if (self = [super initWithFrame:frame]) {
  36. _customTextFieldSuperview = _customTextFieldSuperview == CustomTextFieldSuperviewIsNone ? CustomTextFieldSuperviewIsViewController : _customTextFieldSuperview;
  37. NSLog(@"_customTextFieldSuperview : %d", _customTextFieldSuperview);
  38. self.inputAccessoryView = [self generateAccessoryView];
  39. self.borderStyle = UITextBorderStyleNone;
  40. _bgImageName = @"img_input_round_bg_default";
  41. UIImage *bgImage = [UIImage imageNamed:_bgImageName];
  42. if (bgImage) {
  43. _bgInsets = UIEdgeInsetsMake(kfBoxCap, kfBoxCap, kfBoxCap, kfBoxCap);
  44. bgImage = [ImageUtil resizableImageWithCapInsets:_bgInsets resizingMode:UIImageResizingModeStretch img:bgImage];
  45. [self setBackground:bgImage];
  46. }
  47. if (self.placeholder) {
  48. NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:kUIPlaceHolderColor}];
  49. self.attributedPlaceholder = attrString;
  50. }
  51. _cRect = self.frame;
  52. }
  53. return self;
  54. }
  55. - (id)initWithFrame:(CGRect)frame bgImageName:(NSString *)btnImageName bgPressImageName:(NSString *)bgPressImageName {
  56. if (self = [super initWithFrame:frame]) {
  57. _customTextFieldSuperview = _customTextFieldSuperview == CustomTextFieldSuperviewIsNone ? CustomTextFieldSuperviewIsViewController : _customTextFieldSuperview;
  58. self.inputAccessoryView = [self generateAccessoryView];
  59. self.borderStyle = UITextBorderStyleNone;
  60. _bgPressImageName = bgPressImageName;
  61. UIImage *bgImage = _bgImageName && ![_bgImageName isEmptyString] ? [UIImage imageNamed:_bgImageName] : [UIImage imageNamed:@"img_input_round_bg_default"];
  62. if (bgImage) {
  63. bgImage = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(kfBoxCap, kfBoxCap, kfBoxCap, kfBoxCap) resizingMode:UIImageResizingModeStretch img:bgImage];
  64. [self setBackground:bgImage];
  65. }
  66. if (self.placeholder) {
  67. NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:kUIPlaceHolderColor}];
  68. self.attributedPlaceholder = attrString;
  69. }
  70. _cRect = self.frame;
  71. }
  72. return self;
  73. }
  74. - (id)initWithFrame:(CGRect)frame hasMoveButton:(BOOL)hasMoveButton disablePrev:(BOOL)disablePrev disableNext:(BOOL)disableNext {
  75. if (self = [super initWithFrame:frame]) {
  76. _customTextFieldSuperview = _customTextFieldSuperview == CustomTextFieldSuperviewIsNone ? CustomTextFieldSuperviewIsViewController : _customTextFieldSuperview;
  77. _hasMoveButton = hasMoveButton;
  78. _disablePrevButton = disablePrev;
  79. _disableNextButton = disableNext;
  80. self.inputAccessoryView = [self generateAccessoryView];
  81. self.borderStyle = UITextBorderStyleNone;
  82. _bgImageName = @"img_input_round_bg_default";
  83. UIImage *bgImage = [UIImage imageNamed:_bgImageName];
  84. if (bgImage) {
  85. _bgInsets = UIEdgeInsetsMake(kfBoxCap, kfBoxCap, kfBoxCap, kfBoxCap);
  86. bgImage = [ImageUtil resizableImageWithCapInsets:_bgInsets resizingMode:UIImageResizingModeStretch img:bgImage];
  87. [self setBackground:bgImage];
  88. }
  89. if (self.placeholder) {
  90. NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:kUIPlaceHolderColor}];
  91. self.attributedPlaceholder = attrString;
  92. }
  93. _cRect = self.frame;
  94. }
  95. return self;
  96. }
  97. - (void)awakeFromNib {
  98. [super awakeFromNib];
  99. _customTextFieldSuperview = _customTextFieldSuperview == CustomTextFieldSuperviewIsNone ? CustomTextFieldSuperviewIsViewController : _customTextFieldSuperview;
  100. self.inputAccessoryView = [self generateAccessoryView];
  101. // self.borderStyle = UITextBorderStyleNone;
  102. _bgImageName = _bgImageName && ![_bgImageName isEmptyString] ? _bgImageName : @"img_input_round_bg_default";
  103. _bgDisableImageName = _bgDisableImageName && ![_bgDisableImageName isEmptyString] ? _bgDisableImageName : @"img_input_round_bg_disable";
  104. UIImage *bgImage = [UIImage imageNamed:_bgImageName];
  105. if (bgImage) {
  106. if (!CGRectEqualToRect(_rectForCapBackground, CGRectZero)) {//rectForCap이 설정된 경우, 이미지를 리사이즈함.
  107. CGFloat top, left, bottom, right;
  108. top = CGRectGetMinY(_rectForCapBackground);
  109. left = CGRectGetMinX(_rectForCapBackground);
  110. bottom = CGRectGetHeight(_rectForCapBackground);
  111. right = CGRectGetWidth(_rectForCapBackground);
  112. _bgInsets = UIEdgeInsetsMake(top, left, bottom, right);
  113. } else {
  114. _bgInsets = UIEdgeInsetsMake(kfBoxCap, kfBoxCap, kfBoxCap, kfBoxCap);
  115. }
  116. bgImage = [ImageUtil resizableImageWithCapInsets:_bgInsets resizingMode:UIImageResizingModeStretch img:bgImage];
  117. [self setBackground:bgImage];
  118. }
  119. if (self.placeholder) {
  120. NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:kUIPlaceHolderColor}];
  121. self.attributedPlaceholder = attrString;
  122. }
  123. _cRect = self.frame;
  124. if (self.secureTextEntry) {
  125. _isSecureEntry = self.secureTextEntry;
  126. }
  127. #ifdef DEBUG
  128. NSString *identifier = [NSString stringWithFormat:@"%@", self.placeholder];
  129. [self setValue:identifier forKey:@"layoutDebuggingIdentifier"];
  130. #endif
  131. }
  132. - (void)refreshAccessoryView {
  133. self.inputAccessoryView = [self generateAccessoryView];
  134. }
  135. - (UIView *)generateAccessoryView {
  136. self.keyboardAppearance = UIKeyboardAppearanceDark;
  137. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  138. _toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 44.0)];
  139. _toolBar.barStyle = UIBarStyleBlack;
  140. _toolBar.translucent = YES;
  141. _barItems = [NSMutableArray new];
  142. _btnFlexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  143. _btnPrev = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"이전", @"이전")
  144. style:UIBarButtonItemStylePlain
  145. target:self
  146. action:@selector(goPrevField:)];
  147. _btnNext = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"다음", @"다음")
  148. style:UIBarButtonItemStylePlain
  149. target:self
  150. action:@selector(goNextField:)];
  151. _btnClose = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"닫기", @"닫기")
  152. style:UIBarButtonItemStylePlain
  153. target:self
  154. action:@selector(hideKeyboard:)];
  155. if (IOS_VERSION >= 7.0f) {
  156. _btnClose.tintColor = kUIBtnTintColor;
  157. _btnPrev.tintColor = kUIBtnTintColor;
  158. _btnNext.tintColor = kUIBtnTintColor;
  159. }
  160. if (_hasMoveButton) {
  161. [_barItems addObject:_btnPrev];
  162. [_barItems addObject:_btnNext];
  163. _btnPrev.enabled = !_disablePrevButton;
  164. _btnNext.enabled = !_disableNextButton;
  165. }
  166. [_barItems addObject:_btnFlexibleSpace];
  167. [_barItems addObject:_btnClose];
  168. [_toolBar setItems:_barItems animated:YES];
  169. return _toolBar;
  170. }
  171. - (void)setCustomTextFieldSuperview:(CustomTextFieldSuperview)customTextFieldSuperview {
  172. if (!_autoScrollUp) {
  173. return;
  174. }
  175. _customTextFieldSuperview = customTextFieldSuperview;
  176. if (_customTextFieldSuperview == CustomTextFieldSuperviewIsViewController) {
  177. UIViewController *vc = [CommonUtil currentViewController];
  178. vc = vc.presentedViewController ? vc.presentedViewController : vc;
  179. _targetSuperView = vc.view;
  180. } else if (_customTextFieldSuperview == CustomTextFieldSuperviewIsPopup) {
  181. _targetSuperView = self.superview.superview;
  182. _adjustY = 130.0f;
  183. } else if (_customTextFieldSuperview == CustomTextFieldSuperviewIsPopupContentView) {
  184. _targetSuperView = self.superview.superview.superview; //팝업에서의 viewInRect로 계산됨.
  185. } else if (_customTextFieldSuperview == CustomTextFieldSuperviewIsScrollView) {
  186. _targetSuperView = self.superview;
  187. if ([_targetSuperView isKindOfClass:[UIScrollView class]]) {
  188. UIScrollView *sv = (UIScrollView *)_targetSuperView;
  189. _contentOffset = sv.contentOffset;
  190. _naviMargin = 150.0f;
  191. _adjustY = 0.0f; //?
  192. }
  193. } else if (_customTextFieldSuperview == CustomTextFieldSuperviewIsContentView) {
  194. _targetSuperView = self.superview.superview.superview.superview;
  195. // _targetSuperView = self.superview;
  196. if ([_targetSuperView isKindOfClass:[UIScrollView class]]) {
  197. UIScrollView *sv = (UIScrollView *)_targetSuperView;
  198. _contentOffset = sv.contentOffset;
  199. _naviMargin = 80.0f;
  200. _adjustY = 0.0f; //?
  201. }
  202. } else if (_customTextFieldSuperview == CustomTextFieldSuperviewIsChildViewController) {
  203. UIViewController *cvc = [CommonUtil currentViewController];
  204. _targetSuperView = cvc.view;
  205. // if ([_targetSuperView isKindOfClass:[UIScrollView class]]) {
  206. // UIScrollView *sv = (UIScrollView *)_targetSuperView;
  207. // _contentOffset = sv.contentOffset;
  208. // _naviMargin = 0.0f;
  209. // _adjustY = 0.0f; //?
  210. // }
  211. }
  212. }
  213. - (void)scrollUp {
  214. NSLog(@"test tf scrollUp autoScrollUp : %d", _autoScrollUp);
  215. if (_autoScrollUp) {
  216. if (CGRectEqualToRect(CGRectZero, gKeyboardRect)) {
  217. [self performSelector:@selector(scrollUp) withObject:nil afterDelay:0.1f];
  218. return;
  219. }
  220. if (!_targetSuperView) {
  221. [self setCustomTextFieldSuperview:_customTextFieldSuperview];
  222. }
  223. CGRect selfRect = self.frame;
  224. if (CGRectEqualToRect(_superRect, CGRectZero)) {//기존 위치를 저장함.
  225. _superRect = _targetSuperView.frame;
  226. }
  227. CGRect cr = [self.superview convertRect:selfRect toView:_targetSuperView];
  228. if (_customTextFieldSuperview == CustomTextFieldSuperviewIsPopupContentView || _customTextFieldSuperview == CustomTextFieldSuperviewIsChildViewController) {
  229. cr = [self.superview convertRect:selfRect toView:[[UIApplication sharedApplication].delegate window]];
  230. }
  231. CGFloat cy = CGRectGetMaxY(cr) + _adjustY;
  232. CGFloat ay = gKeyboardRect.size.height + 60;//(gKeyboardRect.size.height + KEYBOARD_ACCESSORY_HEIGHT + KEYBOARD_PREDICTS_HEIGHT);
  233. CGFloat ky = ([UIScreen mainScreen].bounds.size.height - _naviMargin) - ay; // 260
  234. NSLog(@"_superRect1 %@, cy=%f, ky:%f", NSStringFromCGRect(_superRect), cy, ky);
  235. if (cy >= ky) {
  236. __block CGRect scrollRect = _targetSuperView.frame;
  237. scrollRect.origin.y = ky - cy;
  238. [UIView animateWithDuration:kfAnimationDur animations:^{
  239. if ([_targetSuperView isKindOfClass:[UIScrollView class]]) {
  240. CGFloat offsetY = _contentOffset.y + (cy - ky);
  241. [(UIScrollView *)_targetSuperView setContentOffset:CGPointMake(0, offsetY) animated:NO];
  242. }
  243. else {
  244. NSLog(@"_customTextFieldSuperview : %d", _customTextFieldSuperview);
  245. if (_customTextFieldSuperview == CustomTextFieldSuperviewIsPopupContentView || _customTextFieldSuperview == CustomTextFieldSuperviewIsChildViewController) {
  246. scrollRect.origin.y += _superRect.origin.y;
  247. }
  248. NSLog(@"scrollRect y : %f", scrollRect.origin.y);
  249. _targetSuperView.frame = scrollRect;
  250. }
  251. } completion:^(BOOL finished) {
  252. _isScrollUp = YES;
  253. }];
  254. } else {
  255. if (!CGRectEqualToRect(_superRect, CGRectZero)) {
  256. [UIView animateWithDuration:kfAnimationDur animations:^{
  257. _targetSuperView.frame = _superRect;
  258. } completion:^(BOOL finished) {
  259. _isScrollUp = YES;
  260. }];
  261. }
  262. }
  263. }
  264. }
  265. - (BOOL)becomeFirstResponder {
  266. NSLog(@"test tf becomeFirstResponder");
  267. _bgPressImageName = _bgPressImageName && ![_bgPressImageName isEmptyString] ? _bgPressImageName : @"img_input_round_bg_active";
  268. UIImage *bgImage = [UIImage imageNamed:_bgPressImageName];
  269. if (bgImage) {
  270. bgImage = [ImageUtil resizableImageWithCapInsets:_bgInsets resizingMode:UIImageResizingModeStretch img:bgImage];
  271. [self setBackground:bgImage];
  272. }
  273. if ([self isKindOfClass:[CustomSearchTextField class]] || _isScrollUp) {
  274. return [super becomeFirstResponder];
  275. }
  276. //텍스트필드가 여러개일 경우, 다른 텍스트필드에서 resign을 처리하기 전에 호출되는 문제를 해결함.
  277. for (id textField in _targetSuperView.subviews) {
  278. if ([textField isKindOfClass:[UITextField class]] && [textField isFirstResponder]) {
  279. NSLog(@"regisgn t=%@, s=%@", textField, self);
  280. [textField resignFirstResponder];
  281. }
  282. }
  283. _isScrollUp = NO;
  284. [self scrollUp];
  285. return [super becomeFirstResponder];
  286. }
  287. - (BOOL)resignFirstResponder {
  288. NSLog(@"test tf resignFirstResponder");
  289. UIImage *bgImage = [UIImage imageNamed:_bgImageName];
  290. bgImage = [ImageUtil resizableImageWithCapInsets:_bgInsets resizingMode:UIImageResizingModeStretch img:bgImage];
  291. [self setBackground:bgImage];
  292. if ([self isKindOfClass:[CustomSearchTextField class]] || !_isScrollUp) {
  293. return [super resignFirstResponder];
  294. }
  295. if (_autoScrollUp) {
  296. // if (_customTextFieldSuperview == CustomTextFieldSuperviewIsScrollView) {
  297. // _targetSuperView = self.superview;
  298. // _superRect.origin.y = _superRect.origin.y < 0 ? 0.0f : _superRect.origin.y;
  299. // }
  300. [UIView animateWithDuration:kfAnimationDur animations:^{
  301. if ([_targetSuperView isKindOfClass:[UIScrollView class]]) {
  302. [(UIScrollView *)_targetSuperView setContentOffset:_contentOffset animated:NO];
  303. } else if (!CGRectIsEmpty(_superRect)) {
  304. _targetSuperView.frame = _superRect;
  305. NSLog(@"_superRect2 %@", NSStringFromCGRect(_superRect));
  306. }
  307. } completion:^(BOOL finished) {
  308. _isScrollUp = NO;
  309. }];
  310. }
  311. return [super resignFirstResponder];
  312. }
  313. - (void)goPrevField:(id)sender {
  314. [self resignFirstResponder];
  315. if ([self.delegate respondsToSelector:@selector(moveToPrevField:)]) {
  316. [self.delegate moveToPrevField:self];
  317. }
  318. }
  319. - (void)goNextField:(id)sender {
  320. [self resignFirstResponder];
  321. if ([self.delegate respondsToSelector:@selector(moveToNextField:)]) {
  322. [self.delegate moveToNextField:self];
  323. }
  324. }
  325. - (void)hideKeyboard:(id)sender {
  326. [self resignFirstResponder];
  327. if ([self.delegate respondsToSelector:@selector(willHideKeyboard:)]) {
  328. [self.delegate willHideKeyboard:self];
  329. }
  330. }
  331. // placeholder position
  332. - (CGRect)textRectForBounds:(CGRect)bounds {
  333. CGFloat margin = 16;
  334. CGRect inset = bounds;
  335. if (self.textAlignment == NSTextAlignmentLeft || self.textAlignment == NSTextAlignmentJustified || self.textAlignment == NSTextAlignmentNatural) {
  336. inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width, bounds.size.height);
  337. }
  338. return inset;
  339. }
  340. // text position
  341. - (CGRect)editingRectForBounds:(CGRect)bounds {
  342. CGFloat margin = 16;
  343. CGRect inset = bounds;
  344. if (self.textAlignment == NSTextAlignmentLeft || self.textAlignment == NSTextAlignmentJustified || self.textAlignment == NSTextAlignmentNatural) {
  345. inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width, bounds.size.height);
  346. }
  347. return inset;
  348. }
  349. - (void)deleteBackward {
  350. if ([self.delegate respondsToSelector:@selector(deleteBackward:)] && [self.text isEmptyString]) {
  351. [self.delegate deleteBackward:self];
  352. return;
  353. }
  354. [super deleteBackward];
  355. }
  356. - (void)setUserInteractionEnabled:(BOOL)userInteractionEnabled {
  357. [super setUserInteractionEnabled:userInteractionEnabled];
  358. UIImage *bgImage = [UIImage imageNamed:@"img_input_round_bg_active"];
  359. bgImage = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(kfBoxCap, kfBoxCap, kfBoxCap, kfBoxCap) resizingMode:UIImageResizingModeStretch img:bgImage];
  360. [self setBackground:bgImage];
  361. }
  362. @end
  363. @implementation CustomTextField2
  364. // placeholder position
  365. - (CGRect)textRectForBounds:(CGRect)bounds {
  366. // CGFloat y = IOS_VERSION < 7.0f ? 6 : 0;
  367. CGFloat margin = 16;
  368. CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width, bounds.size.height);
  369. return inset;
  370. }
  371. // text position
  372. - (CGRect)editingRectForBounds:(CGRect)bounds {
  373. // CGFloat y = IOS_VERSION < 7.0f ? 6 : 0;
  374. CGFloat margin = 16;
  375. CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width, bounds.size.height);
  376. return inset;
  377. }
  378. @end
  379. @implementation CustomSearchTextField
  380. - (id)initWithFrame:(CGRect)frame {
  381. if (self = [super initWithFrame:frame]) {
  382. self.borderStyle = UITextBorderStyleNone;
  383. UIImage *bgImage = [UIImage imageNamed:@"input_wh_search"];
  384. if (bgImage) {
  385. bgImage = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(1, 22, 1, 1) resizingMode:UIImageResizingModeStretch img:bgImage];
  386. [self setBackground:bgImage];
  387. }
  388. if (self.placeholder) {
  389. NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:kUIPlaceHolderColor}];
  390. self.attributedPlaceholder = attrString;
  391. }
  392. }
  393. return self;
  394. }
  395. - (BOOL)becomeFirstResponder {
  396. //텍스트필드가 여러개일 경우, 다른 텍스트필드에서 resign을 처리하기 전에 호출되는 문제를 해결함.
  397. for (id textField in _targetSuperView.subviews) {
  398. if ([textField isKindOfClass:[UITextField class]] && [textField isFirstResponder]) {
  399. [textField resignFirstResponder];
  400. NSLog(@"regisgn t=%@, s=%@", textField, self);
  401. }
  402. }
  403. UIImage *bgImage = [UIImage imageNamed:@"input_wh_search_press"];
  404. if (bgImage) {
  405. bgImage = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(1, 22, 1, 1) resizingMode:UIImageResizingModeStretch img:bgImage];
  406. [self setBackground:bgImage];
  407. }
  408. return [super becomeFirstResponder];
  409. }
  410. - (BOOL)resignFirstResponder {
  411. UIImage *bgImage = [UIImage imageNamed:@"input_wh_search"];
  412. bgImage = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(1, 22, 1, 1) resizingMode:UIImageResizingModeStretch img:bgImage];
  413. [self setBackground:bgImage];
  414. if (self.autoScrollUp) {
  415. // if (_customTextFieldSuperview == CustomTextFieldSuperviewIsScrollView) {
  416. // _targetSuperView = self.superview;
  417. // _superRect.origin.y = _superRect.origin.y < 0 ? 0.0f : _superRect.origin.y;
  418. // }
  419. [UIView animateWithDuration:kfAnimationDur animations:^{
  420. if ([_targetSuperView isKindOfClass:[UIScrollView class]]) {
  421. [(UIScrollView *)_targetSuperView setContentOffset:_contentOffset animated:NO];
  422. } else if (!CGRectIsEmpty(_superRect)) {
  423. _targetSuperView.frame = _superRect;
  424. NSLog(@"_superRect2 %@", NSStringFromCGRect(_superRect));
  425. }
  426. }];
  427. }
  428. return [super resignFirstResponder];
  429. }
  430. // placeholder position
  431. - (CGRect)textRectForBounds:(CGRect)bounds {
  432. return CGRectInset(bounds, 30.0f, 0);
  433. }
  434. // text position
  435. - (CGRect)editingRectForBounds:(CGRect)bounds {
  436. return CGRectInset(bounds, 30.0f, 0);
  437. }
  438. @end
  439. /*
  440. @implementation CustomTextField3
  441. - (id)initWithFrame:(CGRect)frame {
  442. if (self = [super initWithFrame:frame]) {
  443. self.inputAccessoryView = [self generateAccessoryView];
  444. self.borderStyle = UITextBorderStyleNone;
  445. [self setBackground:[ACDUtil getStretchedImage:[UIImage imageNamed:@"bm_src_textfield_bg"] expectSize:self.bounds.size]];
  446. _cRect = self.frame;
  447. }
  448. return self;
  449. }
  450. - (void)awakeFromNib {
  451. self.inputAccessoryView = [self generateAccessoryView];
  452. self.borderStyle = UITextBorderStyleNone;
  453. [self setBackground:[ACDUtil getStretchedImage:[UIImage imageNamed:@"bm_src_textfield_bg"] expectSize:self.bounds.size]];
  454. _cRect = self.frame;
  455. }
  456. - (void)drawRect:(CGRect)rect {
  457. CGRect tr = _cRect;
  458. tr.size.height = 29.0f;
  459. self.frame = tr;
  460. [self setNeedsLayout];
  461. }
  462. // placeholder position
  463. - (CGRect)textRectForBounds:(CGRect)bounds {
  464. return CGRectInset(bounds, 26, 0);
  465. }
  466. // text position
  467. - (CGRect)editingRectForBounds:(CGRect)bounds {
  468. return CGRectInset(bounds, 26, 0);
  469. }
  470. @end
  471. @implementation CustomTextField4
  472. - (id)initWithFrame:(CGRect)frame {
  473. if (self = [super initWithFrame:frame]) {
  474. self.inputAccessoryView = [self generateAccessoryView];
  475. self.borderStyle = UITextBorderStyleNone;
  476. [self setBackground:[ACDUtil getStretchedImage:[UIImage imageNamed:@"bm_src_textfield_bg2"] expectSize:self.bounds.size]];
  477. _cRect = self.frame;
  478. }
  479. return self;
  480. }
  481. - (void)awakeFromNib {
  482. self.inputAccessoryView = [self generateAccessoryView];
  483. self.borderStyle = UITextBorderStyleNone;
  484. [self setBackground:[ACDUtil getStretchedImage:[UIImage imageNamed:@"bm_src_textfield_bg2"] expectSize:self.bounds.size]];
  485. _cRect = self.frame;
  486. }
  487. - (void)drawRect:(CGRect)rect {
  488. CGRect tr = _cRect;
  489. tr.size.height = 29.0f;
  490. self.frame = tr;
  491. [self setNeedsLayout];
  492. }
  493. // placeholder position
  494. - (CGRect)textRectForBounds:(CGRect)bounds {
  495. return CGRectInset(bounds, 26, 0);
  496. }
  497. // text position
  498. - (CGRect)editingRectForBounds:(CGRect)bounds {
  499. return CGRectInset(bounds, 26, 0);
  500. }
  501. @end
  502. @implementation CustomTextField5
  503. - (id)initWithFrame:(CGRect)frame {
  504. if (self = [super initWithFrame:frame]) {
  505. self.inputAccessoryView = [self generateAccessoryView];
  506. self.borderStyle = UITextBorderStyleNone;
  507. [self setBackground:[ACDUtil getStretchedImage:[UIImage imageNamed:@"st_src_textfield_bg"] expectSize:self.bounds.size]];
  508. _cRect = self.frame;
  509. }
  510. return self;
  511. }
  512. - (void)awakeFromNib {
  513. self.inputAccessoryView = [self generateAccessoryView];
  514. self.borderStyle = UITextBorderStyleNone;
  515. [self setBackground:[ACDUtil getStretchedImage:[UIImage imageNamed:@"st_src_textfield_bg"] expectSize:self.bounds.size]];
  516. _cRect = self.frame;
  517. }
  518. - (void)drawRect:(CGRect)rect {
  519. CGRect tr = _cRect;
  520. tr.size.height = 36.0f;
  521. self.frame = tr;
  522. [self setNeedsLayout];
  523. }
  524. // placeholder position
  525. - (CGRect)textRectForBounds:(CGRect)bounds {
  526. return CGRectInset(bounds, 5, 0);
  527. }
  528. // text position
  529. - (CGRect)editingRectForBounds:(CGRect)bounds {
  530. return CGRectInset(bounds, 5, 0);
  531. }
  532. @end
  533. */
  534. @implementation CustomMenuTextFieldMenu
  535. @end
  536. @implementation CustomMenuTextField
  537. //텍스트필드 컨텍스트 메뉴 커스터마이징
  538. - (void)setMenuItems:(NSArray *)menuItems {
  539. UIMenuController *menu = [UIMenuController sharedMenuController];
  540. menu.menuItems = menuItems;
  541. [menu update];
  542. }
  543. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  544. BOOL canPerform = NO;
  545. for (CustomMenuTextFieldMenu *menu in _menuItems) {
  546. canPerform = (action == menu.selector);
  547. if (canPerform)
  548. break;
  549. }
  550. return canPerform;
  551. }
  552. @end