説明なし

IQTextView.m 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // IQTextView.m
  3. // https://github.com/hackiftekhar/IQKeyboardManager
  4. // Copyright (c) 2013-15 Iftekhar Qurashi.
  5. //
  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. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #import "IQTextView.h"
  24. #import <UIKit/UILabel.h>
  25. #import <UIKit/UINibLoading.h>
  26. @interface IQTextView ()
  27. -(void)refreshPlaceholder;
  28. @end
  29. @implementation IQTextView
  30. {
  31. UILabel *placeHolderLabel;
  32. }
  33. @synthesize placeholder = _placeholder;
  34. -(void)initialize
  35. {
  36. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshPlaceholder) name:UITextViewTextDidChangeNotification object:self];
  37. }
  38. -(void)dealloc
  39. {
  40. [[NSNotificationCenter defaultCenter] removeObserver:self];
  41. }
  42. - (instancetype)init
  43. {
  44. self = [super init];
  45. if (self) {
  46. [self initialize];
  47. }
  48. return self;
  49. }
  50. -(void)awakeFromNib
  51. {
  52. [super awakeFromNib];
  53. [self initialize];
  54. }
  55. -(void)refreshPlaceholder
  56. {
  57. if([[self text] length])
  58. {
  59. [placeHolderLabel setAlpha:0];
  60. }
  61. else
  62. {
  63. [placeHolderLabel setAlpha:1];
  64. }
  65. [self setNeedsLayout];
  66. [self layoutIfNeeded];
  67. }
  68. - (void)setText:(NSString *)text
  69. {
  70. [super setText:text];
  71. [self refreshPlaceholder];
  72. }
  73. -(void)setFont:(UIFont *)font
  74. {
  75. [super setFont:font];
  76. placeHolderLabel.font = self.font;
  77. [self setNeedsLayout];
  78. [self layoutIfNeeded];
  79. }
  80. -(void)layoutSubviews
  81. {
  82. [super layoutSubviews];
  83. [placeHolderLabel sizeToFit];
  84. placeHolderLabel.frame = CGRectMake(8, 8, CGRectGetWidth(self.frame)-16, CGRectGetHeight(placeHolderLabel.frame));
  85. }
  86. -(void)setPlaceholder:(NSString *)placeholder
  87. {
  88. _placeholder = placeholder;
  89. if ( placeHolderLabel == nil )
  90. {
  91. placeHolderLabel = [[UILabel alloc] init];
  92. placeHolderLabel.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
  93. placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
  94. placeHolderLabel.numberOfLines = 0;
  95. placeHolderLabel.font = self.font;
  96. placeHolderLabel.backgroundColor = [UIColor clearColor];
  97. placeHolderLabel.textColor = [UIColor colorWithWhite:0.7 alpha:1.0];
  98. placeHolderLabel.alpha = 0;
  99. [self addSubview:placeHolderLabel];
  100. }
  101. placeHolderLabel.text = self.placeholder;
  102. [self refreshPlaceholder];
  103. }
  104. //When any text changes on textField, the delegate getter is called. At this time we refresh the textView's placeholder
  105. -(id<UITextViewDelegate>)delegate
  106. {
  107. [self refreshPlaceholder];
  108. return [super delegate];
  109. }
  110. @end