YZInputView.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // YZInputView.m
  3. // YZInputView
  4. //
  5. // Created by yz on 16/8/1.
  6. // Copyright © 2016年 yz. All rights reserved.
  7. //
  8. #import "YZInputView.h"
  9. @interface YZInputView ()
  10. /**
  11. * 占位文字View: 为什么使用UITextView,这样直接让占位文字View = 当前textView,文字就会重叠显示
  12. */
  13. @property (nonatomic, weak) UITextView *placeholderView;
  14. /**
  15. * 文字高度
  16. */
  17. @property (nonatomic, assign) NSInteger textH;
  18. /**
  19. * 文字最大高度
  20. */
  21. @property (nonatomic, assign) NSInteger maxTextH;
  22. @end
  23. @implementation YZInputView
  24. - (UITextView *)placeholderView
  25. {
  26. if (_placeholderView == nil) {
  27. UITextView *placeholderView = [[UITextView alloc] init];
  28. _placeholderView = placeholderView;
  29. _placeholderView.scrollEnabled = NO;
  30. _placeholderView.showsHorizontalScrollIndicator = NO;
  31. _placeholderView.showsVerticalScrollIndicator = NO;
  32. _placeholderView.userInteractionEnabled = NO;
  33. _placeholderView.font = self.font;
  34. _placeholderView.textColor = [UIColor lightGrayColor];
  35. _placeholderView.backgroundColor = [UIColor clearColor];
  36. [self addSubview:placeholderView];
  37. }
  38. return _placeholderView;
  39. }
  40. - (void)awakeFromNib
  41. {
  42. [self setup];
  43. }
  44. - (instancetype)initWithFrame:(CGRect)frame
  45. {
  46. if (self = [super initWithFrame:frame]) {
  47. [self setup];
  48. }
  49. return self;
  50. }
  51. - (void)setup
  52. {
  53. self.scrollEnabled = NO;
  54. self.scrollsToTop = NO;
  55. self.showsHorizontalScrollIndicator = NO;
  56. self.enablesReturnKeyAutomatically = YES;
  57. self.layer.borderWidth = 1;
  58. self.layer.cornerRadius = 5;
  59. self.layer.borderColor = [UIColor lightGrayColor].CGColor;
  60. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
  61. }
  62. - (void)setMaxNumberOfLines:(NSUInteger)maxNumberOfLines
  63. {
  64. _maxNumberOfLines = maxNumberOfLines;
  65. // 计算最大高度 = (每行高度 * 总行数 + 文字上下间距)
  66. _maxTextH = ceil(self.font.lineHeight * maxNumberOfLines + self.textContainerInset.top + self.textContainerInset.bottom);
  67. }
  68. - (void)setCornerRadius:(NSUInteger)cornerRadius
  69. {
  70. _cornerRadius = cornerRadius;
  71. self.layer.cornerRadius = cornerRadius;
  72. }
  73. - (void)setYz_textHeightChangeBlock:(void (^)(NSString *, CGFloat))yz_textChangeBlock
  74. {
  75. _yz_textHeightChangeBlock = yz_textChangeBlock;
  76. [self textDidChange];
  77. }
  78. - (void)setPlaceholderColor:(UIColor *)placeholderColor
  79. {
  80. _placeholderColor = placeholderColor;
  81. self.placeholderView.textColor = placeholderColor;
  82. }
  83. - (void)setPlaceholder:(NSString *)placeholder
  84. {
  85. _placeholder = placeholder;
  86. self.placeholderView.text = placeholder;
  87. }
  88. - (void)textDidChange
  89. {
  90. // 占位文字是否显示
  91. self.placeholderView.hidden = self.text.length > 0;
  92. NSInteger height = ceilf([self sizeThatFits:CGSizeMake(self.bounds.size.width, MAXFLOAT)].height);
  93. if (_textH != height) { // 高度不一样,就改变了高度
  94. // 最大高度,可以滚动
  95. self.scrollEnabled = height > _maxTextH && _maxTextH > 0;
  96. _textH = height;
  97. // if (_yz_textHeightChangeBlock && self.scrollEnabled == NO) {
  98. _yz_textHeightChangeBlock(self.text,height);
  99. [self.superview layoutIfNeeded];
  100. self.placeholderView.frame = self.bounds;
  101. // }
  102. }
  103. }
  104. - (void)dealloc
  105. {
  106. [[NSNotificationCenter defaultCenter] removeObserver:self];
  107. }
  108. @end