CustomTextField.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. if (_autoScrollUp) {
  215. if (CGRectEqualToRect(CGRectZero, gKeyboardRect)) {
  216. [self performSelector:@selector(scrollUp) withObject:nil afterDelay:0.1f];
  217. return;
  218. }
  219. if (!_targetSuperView) {
  220. [self setCustomTextFieldSuperview:_customTextFieldSuperview];
  221. }
  222. CGRect selfRect = self.frame;
  223. if (CGRectEqualToRect(_superRect, CGRectZero)) {//기존 위치를 저장함.
  224. _superRect = _targetSuperView.frame;
  225. }
  226. CGRect cr = [self.superview convertRect:selfRect toView:_targetSuperView];
  227. if (_customTextFieldSuperview == CustomTextFieldSuperviewIsPopupContentView || _customTextFieldSuperview == CustomTextFieldSuperviewIsChildViewController) {
  228. cr = [self.superview convertRect:selfRect toView:[[UIApplication sharedApplication].delegate window]];
  229. }
  230. CGFloat cy = CGRectGetMaxY(cr) + _adjustY;
  231. CGFloat ay = gKeyboardRect.size.height + 60;//(gKeyboardRect.size.height + KEYBOARD_ACCESSORY_HEIGHT + KEYBOARD_PREDICTS_HEIGHT);
  232. CGFloat ky = ([UIScreen mainScreen].bounds.size.height - _naviMargin) - ay; // 260
  233. NSLog(@"_superRect1 %@, cy=%f, ky:%f", NSStringFromCGRect(_superRect), cy, ky);
  234. if (cy >= ky) {
  235. __block CGRect scrollRect = _targetSuperView.frame;
  236. scrollRect.origin.y = ky - cy;
  237. [UIView animateWithDuration:kfAnimationDur animations:^{
  238. if ([_targetSuperView isKindOfClass:[UIScrollView class]]) {
  239. CGFloat offsetY = _contentOffset.y + (cy - ky);
  240. [(UIScrollView *)_targetSuperView setContentOffset:CGPointMake(0, offsetY) animated:NO];
  241. }
  242. else {
  243. NSLog(@"_customTextFieldSuperview : %d", _customTextFieldSuperview);
  244. if (_customTextFieldSuperview == CustomTextFieldSuperviewIsPopupContentView || _customTextFieldSuperview == CustomTextFieldSuperviewIsChildViewController) {
  245. scrollRect.origin.y += _superRect.origin.y;
  246. }
  247. NSLog(@"scrollRect y : %f", scrollRect.origin.y);
  248. _targetSuperView.frame = scrollRect;
  249. }
  250. } completion:^(BOOL finished) {
  251. _isScrollUp = YES;
  252. }];
  253. } else {
  254. if (!CGRectEqualToRect(_superRect, CGRectZero)) {
  255. [UIView animateWithDuration:kfAnimationDur animations:^{
  256. _targetSuperView.frame = _superRect;
  257. } completion:^(BOOL finished) {
  258. _isScrollUp = YES;
  259. }];
  260. }
  261. }
  262. }
  263. }
  264. - (BOOL)becomeFirstResponder {
  265. NSLog(@"test tf becomeFirstResponder");
  266. _bgPressImageName = _bgPressImageName && ![_bgPressImageName isEmptyString] ? _bgPressImageName : @"img_input_round_bg_active";
  267. UIImage *bgImage = [UIImage imageNamed:_bgPressImageName];
  268. if (bgImage) {
  269. bgImage = [ImageUtil resizableImageWithCapInsets:_bgInsets resizingMode:UIImageResizingModeStretch img:bgImage];
  270. [self setBackground:bgImage];
  271. }
  272. if ([self isKindOfClass:[CustomSearchTextField class]] || _isScrollUp) {
  273. return [super becomeFirstResponder];
  274. }
  275. //텍스트필드가 여러개일 경우, 다른 텍스트필드에서 resign을 처리하기 전에 호출되는 문제를 해결함.
  276. for (id textField in _targetSuperView.subviews) {
  277. if ([textField isKindOfClass:[UITextField class]] && [textField isFirstResponder]) {
  278. NSLog(@"regisgn t=%@, s=%@", textField, self);
  279. [textField resignFirstResponder];
  280. }
  281. }
  282. _isScrollUp = NO;
  283. [self scrollUp];
  284. return [super becomeFirstResponder];
  285. }
  286. - (BOOL)resignFirstResponder {
  287. NSLog(@"test tf resignFirstResponder");
  288. UIImage *bgImage = [UIImage imageNamed:_bgImageName];
  289. bgImage = [ImageUtil resizableImageWithCapInsets:_bgInsets resizingMode:UIImageResizingModeStretch img:bgImage];
  290. [self setBackground:bgImage];
  291. if ([self isKindOfClass:[CustomSearchTextField class]] || !_isScrollUp) {
  292. return [super resignFirstResponder];
  293. }
  294. if (_autoScrollUp) {
  295. // if (_customTextFieldSuperview == CustomTextFieldSuperviewIsScrollView) {
  296. // _targetSuperView = self.superview;
  297. // _superRect.origin.y = _superRect.origin.y < 0 ? 0.0f : _superRect.origin.y;
  298. // }
  299. [UIView animateWithDuration:kfAnimationDur animations:^{
  300. if ([_targetSuperView isKindOfClass:[UIScrollView class]]) {
  301. [(UIScrollView *)_targetSuperView setContentOffset:_contentOffset animated:NO];
  302. } else if (!CGRectIsEmpty(_superRect)) {
  303. _targetSuperView.frame = _superRect;
  304. NSLog(@"_superRect2 %@", NSStringFromCGRect(_superRect));
  305. }
  306. } completion:^(BOOL finished) {
  307. _isScrollUp = NO;
  308. }];
  309. }
  310. return [super resignFirstResponder];
  311. }
  312. - (void)goPrevField:(id)sender {
  313. [self resignFirstResponder];
  314. if ([self.delegate respondsToSelector:@selector(moveToPrevField:)]) {
  315. [self.delegate moveToPrevField:self];
  316. }
  317. }
  318. - (void)goNextField:(id)sender {
  319. [self resignFirstResponder];
  320. if ([self.delegate respondsToSelector:@selector(moveToNextField:)]) {
  321. [self.delegate moveToNextField:self];
  322. }
  323. }
  324. - (void)hideKeyboard:(id)sender {
  325. [self resignFirstResponder];
  326. if ([self.delegate respondsToSelector:@selector(willHideKeyboard:)]) {
  327. [self.delegate willHideKeyboard:self];
  328. }
  329. }
  330. // placeholder position
  331. - (CGRect)textRectForBounds:(CGRect)bounds {
  332. CGFloat margin = 16;
  333. CGRect inset = bounds;
  334. if (self.textAlignment == NSTextAlignmentLeft || self.textAlignment == NSTextAlignmentJustified || self.textAlignment == NSTextAlignmentNatural) {
  335. inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width, bounds.size.height);
  336. }
  337. return inset;
  338. }
  339. // text position
  340. - (CGRect)editingRectForBounds:(CGRect)bounds {
  341. CGFloat margin = 16;
  342. CGRect inset = bounds;
  343. if (self.textAlignment == NSTextAlignmentLeft || self.textAlignment == NSTextAlignmentJustified || self.textAlignment == NSTextAlignmentNatural) {
  344. inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width, bounds.size.height);
  345. }
  346. return inset;
  347. }
  348. - (void)deleteBackward {
  349. if ([self.delegate respondsToSelector:@selector(deleteBackward:)] && [self.text isEmptyString]) {
  350. [self.delegate deleteBackward:self];
  351. return;
  352. }
  353. [super deleteBackward];
  354. }
  355. - (void)setUserInteractionEnabled:(BOOL)userInteractionEnabled {
  356. [super setUserInteractionEnabled:userInteractionEnabled];
  357. UIImage *bgImage = [UIImage imageNamed:@"img_input_round_bg_active"];
  358. bgImage = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(kfBoxCap, kfBoxCap, kfBoxCap, kfBoxCap) resizingMode:UIImageResizingModeStretch img:bgImage];
  359. [self setBackground:bgImage];
  360. }
  361. @end
  362. @implementation CustomTextField2
  363. // placeholder position
  364. - (CGRect)textRectForBounds:(CGRect)bounds {
  365. // CGFloat y = IOS_VERSION < 7.0f ? 6 : 0;
  366. CGFloat margin = 16;
  367. CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width, bounds.size.height);
  368. return inset;
  369. }
  370. // text position
  371. - (CGRect)editingRectForBounds:(CGRect)bounds {
  372. // CGFloat y = IOS_VERSION < 7.0f ? 6 : 0;
  373. CGFloat margin = 16;
  374. CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width, bounds.size.height);
  375. return inset;
  376. }
  377. @end
  378. @implementation CustomSearchTextField
  379. - (id)initWithFrame:(CGRect)frame {
  380. if (self = [super initWithFrame:frame]) {
  381. self.borderStyle = UITextBorderStyleNone;
  382. UIImage *bgImage = [UIImage imageNamed:@"input_wh_search"];
  383. if (bgImage) {
  384. bgImage = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(1, 22, 1, 1) resizingMode:UIImageResizingModeStretch img:bgImage];
  385. [self setBackground:bgImage];
  386. }
  387. if (self.placeholder) {
  388. NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:kUIPlaceHolderColor}];
  389. self.attributedPlaceholder = attrString;
  390. }
  391. }
  392. return self;
  393. }
  394. - (BOOL)becomeFirstResponder {
  395. //텍스트필드가 여러개일 경우, 다른 텍스트필드에서 resign을 처리하기 전에 호출되는 문제를 해결함.
  396. for (id textField in _targetSuperView.subviews) {
  397. if ([textField isKindOfClass:[UITextField class]] && [textField isFirstResponder]) {
  398. [textField resignFirstResponder];
  399. NSLog(@"regisgn t=%@, s=%@", textField, self);
  400. }
  401. }
  402. UIImage *bgImage = [UIImage imageNamed:@"input_wh_search_press"];
  403. if (bgImage) {
  404. bgImage = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(1, 22, 1, 1) resizingMode:UIImageResizingModeStretch img:bgImage];
  405. [self setBackground:bgImage];
  406. }
  407. return [super becomeFirstResponder];
  408. }
  409. - (BOOL)resignFirstResponder {
  410. UIImage *bgImage = [UIImage imageNamed:@"input_wh_search"];
  411. bgImage = [ImageUtil resizableImageWithCapInsets:UIEdgeInsetsMake(1, 22, 1, 1) resizingMode:UIImageResizingModeStretch img:bgImage];
  412. [self setBackground:bgImage];
  413. if (self.autoScrollUp) {
  414. // if (_customTextFieldSuperview == CustomTextFieldSuperviewIsScrollView) {
  415. // _targetSuperView = self.superview;
  416. // _superRect.origin.y = _superRect.origin.y < 0 ? 0.0f : _superRect.origin.y;
  417. // }
  418. [UIView animateWithDuration:kfAnimationDur animations:^{
  419. if ([_targetSuperView isKindOfClass:[UIScrollView class]]) {
  420. [(UIScrollView *)_targetSuperView setContentOffset:_contentOffset animated:NO];
  421. } else if (!CGRectIsEmpty(_superRect)) {
  422. _targetSuperView.frame = _superRect;
  423. NSLog(@"_superRect2 %@", NSStringFromCGRect(_superRect));
  424. }
  425. }];
  426. }
  427. return [super resignFirstResponder];
  428. }
  429. // placeholder position
  430. - (CGRect)textRectForBounds:(CGRect)bounds {
  431. return CGRectInset(bounds, 30.0f, 0);
  432. }
  433. // text position
  434. - (CGRect)editingRectForBounds:(CGRect)bounds {
  435. return CGRectInset(bounds, 30.0f, 0);
  436. }
  437. @end
  438. /*
  439. @implementation CustomTextField3
  440. - (id)initWithFrame:(CGRect)frame {
  441. if (self = [super initWithFrame:frame]) {
  442. self.inputAccessoryView = [self generateAccessoryView];
  443. self.borderStyle = UITextBorderStyleNone;
  444. [self setBackground:[ACDUtil getStretchedImage:[UIImage imageNamed:@"bm_src_textfield_bg"] expectSize:self.bounds.size]];
  445. _cRect = self.frame;
  446. }
  447. return self;
  448. }
  449. - (void)awakeFromNib {
  450. self.inputAccessoryView = [self generateAccessoryView];
  451. self.borderStyle = UITextBorderStyleNone;
  452. [self setBackground:[ACDUtil getStretchedImage:[UIImage imageNamed:@"bm_src_textfield_bg"] expectSize:self.bounds.size]];
  453. _cRect = self.frame;
  454. }
  455. - (void)drawRect:(CGRect)rect {
  456. CGRect tr = _cRect;
  457. tr.size.height = 29.0f;
  458. self.frame = tr;
  459. [self setNeedsLayout];
  460. }
  461. // placeholder position
  462. - (CGRect)textRectForBounds:(CGRect)bounds {
  463. return CGRectInset(bounds, 26, 0);
  464. }
  465. // text position
  466. - (CGRect)editingRectForBounds:(CGRect)bounds {
  467. return CGRectInset(bounds, 26, 0);
  468. }
  469. @end
  470. @implementation CustomTextField4
  471. - (id)initWithFrame:(CGRect)frame {
  472. if (self = [super initWithFrame:frame]) {
  473. self.inputAccessoryView = [self generateAccessoryView];
  474. self.borderStyle = UITextBorderStyleNone;
  475. [self setBackground:[ACDUtil getStretchedImage:[UIImage imageNamed:@"bm_src_textfield_bg2"] expectSize:self.bounds.size]];
  476. _cRect = self.frame;
  477. }
  478. return self;
  479. }
  480. - (void)awakeFromNib {
  481. self.inputAccessoryView = [self generateAccessoryView];
  482. self.borderStyle = UITextBorderStyleNone;
  483. [self setBackground:[ACDUtil getStretchedImage:[UIImage imageNamed:@"bm_src_textfield_bg2"] expectSize:self.bounds.size]];
  484. _cRect = self.frame;
  485. }
  486. - (void)drawRect:(CGRect)rect {
  487. CGRect tr = _cRect;
  488. tr.size.height = 29.0f;
  489. self.frame = tr;
  490. [self setNeedsLayout];
  491. }
  492. // placeholder position
  493. - (CGRect)textRectForBounds:(CGRect)bounds {
  494. return CGRectInset(bounds, 26, 0);
  495. }
  496. // text position
  497. - (CGRect)editingRectForBounds:(CGRect)bounds {
  498. return CGRectInset(bounds, 26, 0);
  499. }
  500. @end
  501. @implementation CustomTextField5
  502. - (id)initWithFrame:(CGRect)frame {
  503. if (self = [super initWithFrame:frame]) {
  504. self.inputAccessoryView = [self generateAccessoryView];
  505. self.borderStyle = UITextBorderStyleNone;
  506. [self setBackground:[ACDUtil getStretchedImage:[UIImage imageNamed:@"st_src_textfield_bg"] expectSize:self.bounds.size]];
  507. _cRect = self.frame;
  508. }
  509. return self;
  510. }
  511. - (void)awakeFromNib {
  512. self.inputAccessoryView = [self generateAccessoryView];
  513. self.borderStyle = UITextBorderStyleNone;
  514. [self setBackground:[ACDUtil getStretchedImage:[UIImage imageNamed:@"st_src_textfield_bg"] expectSize:self.bounds.size]];
  515. _cRect = self.frame;
  516. }
  517. - (void)drawRect:(CGRect)rect {
  518. CGRect tr = _cRect;
  519. tr.size.height = 36.0f;
  520. self.frame = tr;
  521. [self setNeedsLayout];
  522. }
  523. // placeholder position
  524. - (CGRect)textRectForBounds:(CGRect)bounds {
  525. return CGRectInset(bounds, 5, 0);
  526. }
  527. // text position
  528. - (CGRect)editingRectForBounds:(CGRect)bounds {
  529. return CGRectInset(bounds, 5, 0);
  530. }
  531. @end
  532. */
  533. @implementation CustomMenuTextFieldMenu
  534. @end
  535. @implementation CustomMenuTextField
  536. //텍스트필드 컨텍스트 메뉴 커스터마이징
  537. - (void)setMenuItems:(NSArray *)menuItems {
  538. UIMenuController *menu = [UIMenuController sharedMenuController];
  539. menu.menuItems = menuItems;
  540. [menu update];
  541. }
  542. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  543. BOOL canPerform = NO;
  544. for (CustomMenuTextFieldMenu *menu in _menuItems) {
  545. canPerform = (action == menu.selector);
  546. if (canPerform)
  547. break;
  548. }
  549. return canPerform;
  550. }
  551. @end