CQTextView.m 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #import "CQTextView.h"
  2. @interface CQTextView ()<UITextViewDelegate>{
  3. /** 记录初始化时的height,textview */
  4. CGFloat _initHeight;
  5. /*记录当前高度,textview*/
  6. CGFloat _oldHeight;
  7. }
  8. /** placeholder的label */
  9. @property (nonatomic,strong) UILabel *placeholderLabel;
  10. @end
  11. @implementation CQTextView
  12. /** 重写初始化方法 */
  13. - (instancetype)initWithFrame:(CGRect)frame{
  14. if (self = [super initWithFrame:frame]) {
  15. // 记录初始高度
  16. _initHeight = frame.size.height;
  17. _oldHeight = frame.size.height;
  18. self.clipsToBounds = NO;
  19. // 添加textView
  20. self.textView = [[UITextView alloc]initWithFrame:self.bounds];
  21. [self addSubview:self.textView];
  22. self.textView.delegate = (id)self;
  23. self.textView.backgroundColor = [UIColor clearColor];
  24. // 添加placeholderLabel
  25. self.placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(3, 0, frame.size.width - 3, frame.size.height)];
  26. [self addSubview:self.placeholderLabel];
  27. self.placeholderLabel.backgroundColor = [UIColor clearColor];
  28. self.placeholderLabel.textColor = [UIColor lightGrayColor];
  29. }
  30. return self;
  31. }
  32. // 赋值placeholder
  33. - (void)setPlaceholder:(NSString *)placeholder{
  34. _placeholder = placeholder;
  35. self.placeholderLabel.text = placeholder;
  36. [self.placeholderLabel sizeToFit];
  37. self.placeholderLabel.center = self.textView.center;
  38. }
  39. // 赋值font
  40. - (void)setFont:(UIFont *)font{
  41. self.textView.font = self.placeholderLabel.font = font;
  42. // 重新调整placeholderLabel的大小
  43. [self.placeholderLabel sizeToFit];
  44. self.placeholderLabel.center = self.textView.center;
  45. }
  46. /** textView文本内容改变时回调 */
  47. - (void)textViewDidChange:(UITextView *)textView{
  48. // 计算高度
  49. CGSize size = CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX);
  50. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.textView.font,NSFontAttributeName, nil];
  51. CGFloat curheight = [textView.text boundingRectWithSize:size
  52. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  53. attributes:dic
  54. context:nil].size.height;
  55. // 如果高度小于初始化时的高度,则不赋值(仍采用最初的高度)
  56. if (curheight < _initHeight) {
  57. self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, _initHeight);
  58. self.textView.frame = CGRectMake(self.textView.frame.origin.x, self.textView.frame.origin.y, self.textView.frame.size.width, _initHeight);
  59. }else{
  60. // 重新给frame赋值(改变高度)
  61. self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, curheight+20);
  62. self.textView.frame = CGRectMake(self.textView.frame.origin.x, self.textView.frame.origin.y, self.textView.frame.size.width, curheight+20);
  63. }
  64. // 如果文本为空,显示placeholder
  65. if (textView.text.length == 0) {
  66. self.placeholderLabel.hidden = NO;
  67. self.placeholderLabel.center = self.textView.center;
  68. }else{
  69. self.placeholderLabel.hidden = YES;
  70. }
  71. }
  72. @end