CustomTextField.m 23 KB

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