《省钱达人》与《猎豆优选》UI相同版。域名tbk

PlaceholderTextView.m 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // PlaceholderTextView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/2/5.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "PlaceholderTextView.h"
  9. @interface PlaceholderTextView ()
  10. @property (strong, nonatomic) NSString *currentText;
  11. @end
  12. @implementation PlaceholderTextView
  13. -(id)initWithFrame:(CGRect)frame{
  14. self = [super initWithFrame:frame];
  15. if (self) {
  16. [self addObserver];
  17. [self setView];
  18. }
  19. return self;
  20. }
  21. -(id)initWithCoder:(NSCoder *)aDecoder{
  22. self = [super initWithCoder:aDecoder];
  23. if (self) {
  24. [self addObserver];
  25. [self setView];
  26. }
  27. return self;
  28. }
  29. -(id)init{
  30. self = [super init];
  31. if (self) {
  32. [self addObserver];
  33. [self setView];
  34. }
  35. return self;
  36. }
  37. -(void)setView{
  38. if (!self.placeholderLabel) {
  39. self.placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 0, self.frame.size.width, self.frame.size.height)];
  40. self.placeholderLabel.textColor = [UIColor lightGrayColor];
  41. self.placeholderLabel.numberOfLines = 0;
  42. self.placeholderLabel.font = [self font];
  43. [self addSubview:self.placeholderLabel];
  44. super.delegate = self;
  45. }
  46. if (!self.wordNumLabel) {
  47. self.wordNumLabel = [[UILabel alloc]initWithFrame:CGRectZero];
  48. self.wordNumLabel.font = [UIFont systemFontOfSize:13];
  49. self.wordNumLabel.textColor = [UIColor lightGrayColor];
  50. self.wordNumLabel.textAlignment = NSTextAlignmentRight;
  51. [self addSubview:self.wordNumLabel];
  52. }
  53. }
  54. -(void)layoutSubviews{
  55. self.placeholderLabel.frame = CGRectMake(8, 6.5, self.frame.size.width-8, self.frame.size.height);
  56. [self.placeholderLabel sizeToFit];
  57. [self.wordNumLabel sizeToFit];
  58. [self refreshFram];
  59. }
  60. -(void)addObserver
  61. {
  62. [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:self];
  63. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(placeholderTextViewdidChange:) name:UITextViewTextDidChangeNotification object:self];
  64. }
  65. -(void)setPlaceholder:(NSString *)placeholder{
  66. _placeholder = placeholder;
  67. self.placeholderLabel.text = _placeholder;
  68. [self.placeholderLabel sizeToFit];
  69. [self endEditing:NO];
  70. }
  71. -(void)setMaxLength:(NSInteger)maxLength{
  72. _maxLength = maxLength;
  73. self.wordNumLabel.text = [NSString stringWithFormat:@"0/%ld",(long)_maxLength];
  74. }
  75. -(void)placeholderTextViewdidChange:(NSNotification *)notificat{
  76. PlaceholderTextView *textView = (PlaceholderTextView *)notificat.object;
  77. if ([self.text length]>0) {
  78. [self.placeholderLabel setHidden:YES];
  79. }else{
  80. [self.placeholderLabel setHidden:NO];
  81. }
  82. if ([textView.text length]>self.maxLength&&self.maxLength!=0&&textView.markedTextRange == nil) {
  83. textView.text = [textView.text substringToIndex:self.maxLength];
  84. }
  85. self.wordNumLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)[textView.text length],(long)_maxLength];
  86. if (self.didChangeText) {
  87. self.didChangeText(textView);
  88. }
  89. [self refreshFram];
  90. _currentText = textView.text;
  91. }
  92. - (void)didChangeText:(void (^)(PlaceholderTextView *))block{
  93. self.didChangeText = block;
  94. }
  95. - (void)setText:(NSString *)text{
  96. [super setText:text];
  97. if (text.length>0) {
  98. [self.placeholderLabel setHidden:YES];
  99. self.wordNumLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)[text length],(long)_maxLength];
  100. [self.wordNumLabel sizeToFit];
  101. [self refreshFram];
  102. }
  103. }
  104. -(void)placeholderTextViewEndEditing{
  105. if ([self.text length]>0) {
  106. [self.placeholderLabel setHidden:YES];
  107. }else{
  108. [self.placeholderLabel setHidden:NO];
  109. }
  110. }
  111. - (void)refreshFram{
  112. [self.wordNumLabel sizeToFit];
  113. if (self.contentSize.height>self.frame.size.height-self.wordNumLabel.frame.size.height-5) {
  114. self.wordNumLabel.frame = CGRectMake(self.frame.size.width - self.wordNumLabel.frame.size.width-5, self.contentSize.height+self.contentInset.bottom-self.wordNumLabel.frame.size.height-5, self.wordNumLabel.frame.size.width, self.wordNumLabel.frame.size.height);
  115. self.contentInset = UIEdgeInsetsMake(0, 0, self.wordNumLabel.frame.size.height, 0);
  116. }else{
  117. self.wordNumLabel.frame = CGRectMake(self.frame.size.width - self.wordNumLabel.frame.size.width-5, self.frame.size.height + self.contentInset.bottom-self.wordNumLabel.frame.size.height-5, self.wordNumLabel.frame.size.width, self.wordNumLabel.frame.size.height);
  118. self.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
  119. }
  120. }
  121. -(void)dealloc{
  122. [[NSNotificationCenter defaultCenter] removeObserver:self];
  123. }
  124. @end