CustomTextField.m 24 KB

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