CustomTextField.m 23 KB

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