CCUIMultilineTextField.mm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2012 James Chen
  4. Copyright (c) 2015 Mazyad Alabduljaleel
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #import "ui/UIEditBox/iOS/CCUIMultilineTextField.h"
  23. #include "base/CCDirector.h"
  24. /**
  25. * http://stackoverflow.com/questions/1328638/placeholder-in-uitextview
  26. */
  27. CGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25;
  28. @implementation CCUIMultilineTextField
  29. #pragma mark - Init & Dealloc
  30. - (instancetype)initWithFrame:(CGRect)frame
  31. {
  32. self = [super initWithFrame:frame];
  33. if (self) {
  34. [[NSNotificationCenter defaultCenter] addObserver:self
  35. selector:@selector(textChanged:)
  36. name:UITextViewTextDidChangeNotification
  37. object:nil];
  38. }
  39. return self;
  40. }
  41. - (void)dealloc
  42. {
  43. [[NSNotificationCenter defaultCenter] removeObserver:self];
  44. [_placeHolderLabel release];
  45. [super dealloc];
  46. }
  47. #pragma mark - Properties
  48. - (NSString *)placeholder
  49. {
  50. return self.placeHolderLabel.text;
  51. }
  52. - (void)setPlaceholder:(NSString *)placeholder
  53. {
  54. self.placeHolderLabel.text = placeholder;
  55. [self.placeHolderLabel sizeToFit];
  56. }
  57. - (void)setText:(NSString *)text
  58. {
  59. [super setText:text];
  60. [self textChanged:nil];
  61. }
  62. - (UILabel *)placeHolderLabel
  63. {
  64. if (_placeHolderLabel == nil) {
  65. auto glview = cocos2d::Director::getInstance()->getOpenGLView();
  66. float padding = CC_EDIT_BOX_PADDING * glview->getScaleX() / glview->getContentScaleFactor();
  67. _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(padding,
  68. padding,
  69. self.bounds.size.width - padding * 2,
  70. 0)];
  71. _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
  72. _placeHolderLabel.numberOfLines = 0;
  73. _placeHolderLabel.font = self.font;
  74. _placeHolderLabel.backgroundColor = [UIColor clearColor];
  75. _placeHolderLabel.textColor = [UIColor lightGrayColor];
  76. _placeHolderLabel.alpha = 0;
  77. [self addSubview:_placeHolderLabel];
  78. }
  79. return _placeHolderLabel;
  80. }
  81. #pragma mark - Public methods
  82. - (CGRect)textRectForBounds:(CGRect)bounds
  83. {
  84. auto glview = cocos2d::Director::getInstance()->getOpenGLView();
  85. float padding = CC_EDIT_BOX_PADDING * glview->getScaleX() / glview->getContentScaleFactor();
  86. return CGRectInset(bounds, padding, padding);
  87. }
  88. - (CGRect)editingRectForBounds:(CGRect)bounds
  89. {
  90. return [self textRectForBounds:bounds];
  91. }
  92. - (void)layoutSubviews
  93. {
  94. [super layoutSubviews];
  95. if (self.placeholder.length > 0) {
  96. [self sendSubviewToBack:self.placeHolderLabel];
  97. }
  98. }
  99. - (void)drawRect:(CGRect)rect
  100. {
  101. if (self.text.length == 0 && self.placeholder.length > 0) {
  102. self.placeHolderLabel.alpha = 1;
  103. }
  104. [super drawRect:rect];
  105. }
  106. #pragma mark - NSNotification Observers
  107. - (void)textChanged:(NSNotification *)notification
  108. {
  109. if (self.placeholder.length == 0) {
  110. return;
  111. }
  112. [UIView animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION animations:^{
  113. CGFloat alpha = (self.text.length == 0 ? 1 : 0);
  114. self.placeHolderLabel.alpha = alpha;
  115. }];
  116. }
  117. @end