CustomTextField.m 24 KB

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