XLFormTextView.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // XLFormTextView.m
  3. // XLForm ( https://github.com/xmartlabs/XLForm )
  4. //
  5. // Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. #import "XLFormTextView.h"
  26. @implementation XLFormTextView
  27. - (void)dealloc
  28. {
  29. [[NSNotificationCenter defaultCenter] removeObserver:self];
  30. }
  31. - (instancetype)initWithFrame:(CGRect)frame
  32. {
  33. if((self = [super initWithFrame:frame])){
  34. self.scrollsToTop = NO;
  35. self.contentInset = UIEdgeInsetsMake(0, -4, 0, 0);
  36. [self setPlaceholder:@""];
  37. [self setPlaceholderColor:[UIColor colorWithRed:.78 green:.78 blue:.80 alpha:1.0]];
  38. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
  39. }
  40. return self;
  41. }
  42. - (void)textChanged:(NSNotification *)notification
  43. {
  44. if([[self placeholder] length] == 0){
  45. return;
  46. }
  47. if([[self text] length] == 0){
  48. [[self viewWithTag:999] setAlpha:1];
  49. }
  50. else{
  51. [[self viewWithTag:999] setAlpha:0];
  52. }
  53. }
  54. - (void)setText:(NSString *)text {
  55. [super setText:text];
  56. [self textChanged:nil];
  57. }
  58. - (void)drawRect:(CGRect)rect
  59. {
  60. if([[self placeholder] length] > 0){
  61. if (_placeHolderLabel == nil ){
  62. _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(4,8,self.bounds.size.width - 16,0)];
  63. _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
  64. _placeHolderLabel.numberOfLines = 0;
  65. _placeHolderLabel.backgroundColor = [UIColor clearColor];
  66. _placeHolderLabel.textColor = self.placeholderColor;
  67. _placeHolderLabel.alpha = 0;
  68. _placeHolderLabel.tag = 999;
  69. [self addSubview:_placeHolderLabel];
  70. }
  71. _placeHolderLabel.text = self.placeholder;
  72. _placeHolderLabel.font = self.font;
  73. [_placeHolderLabel sizeToFit];
  74. [self sendSubviewToBack:_placeHolderLabel];
  75. }
  76. if( [[self text] length] == 0 && [[self placeholder] length] > 0 ){
  77. [[self viewWithTag:999] setAlpha:1];
  78. }
  79. [super drawRect:rect];
  80. }
  81. @end