LZBTextView.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // LZBTextView.m
  3. // LZBKeyBoardView
  4. //
  5. // demo地址:https://github.com/lzbgithubcode/LZBKeyBoardView.git
  6. // Created by zibin on 16/11/29.
  7. // Copyright © 2016年 apple. All rights reserved.
  8. //
  9. #import "LZBTextView.h"
  10. @interface LZBTextView()
  11. @property (nonatomic, strong) UILabel *placeholderLabel;
  12. @end
  13. @implementation LZBTextView
  14. - (instancetype)initWithFrame:(CGRect)frame
  15. {
  16. if(self = [super initWithFrame:frame])
  17. {
  18. [self addSubview:self.placeholderLabel];
  19. self.alwaysBounceVertical = YES;
  20. self.font = [UIFont systemFontOfSize:14.0];
  21. self.placeholderColor = [UIColor grayColor];
  22. self.placeholderLabel.frame = CGRectMake(4.0f, 8.0f, 0, 0);
  23. // 监听文字改变
  24. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
  25. }
  26. return self;
  27. }
  28. #pragma mark - API
  29. - (void)setFont:(UIFont *)font
  30. {
  31. [super setFont:font];
  32. self.placeholderLabel.font = font;
  33. [self computePlaceholderLabelSize];
  34. }
  35. - (void)setPlaceholderColor:(UIColor *)placeholderColor
  36. {
  37. _placeholderColor = placeholderColor;
  38. self.placeholderLabel.textColor = placeholderColor;
  39. }
  40. - (void)setPlaceholder:(NSString *)placeholder
  41. {
  42. _placeholder = [placeholder copy];
  43. self.placeholderLabel.text = placeholder;
  44. [self computePlaceholderLabelSize];
  45. }
  46. - (void)setPlaceHolderOffsetX:(CGFloat)placeHolderOffsetX
  47. {
  48. _placeHolderOffsetX = placeHolderOffsetX;
  49. [self computePlaceholderLabelSize];
  50. }
  51. - (void)setPlaceHolderOffsetY:(CGFloat)placeHolderOffsetY
  52. {
  53. _placeHolderOffsetY = placeHolderOffsetY;
  54. [self computePlaceholderLabelSize];
  55. }
  56. - (void)setCursorOffset:(UIOffset)cursorOffset
  57. {
  58. _cursorOffset = cursorOffset;
  59. self.textContainerInset = UIEdgeInsetsMake(cursorOffset.vertical, cursorOffset.horizontal, cursorOffset.vertical,cursorOffset.horizontal);
  60. }
  61. - (void)setPlaceHolderHidden:(BOOL)placeHolderHidden
  62. {
  63. _placeHolderHidden = placeHolderHidden;
  64. self.placeholderLabel.hidden = placeHolderHidden;
  65. }
  66. #pragma mark - hanlde
  67. -(void)computePlaceholderLabelSize
  68. {
  69. CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - 2 * (self.placeholderLabel.frame.origin.x - self.placeHolderOffsetX);
  70. CGSize maxSize = CGSizeMake(maxWidth, MAXFLOAT);
  71. CGSize computeSize = [self.placeholder boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : self.font} context:nil].size;
  72. CGRect frame = self.placeholderLabel.frame;
  73. frame.size = computeSize;
  74. frame.origin.x = self.placeHolderOffsetX + frame.origin.x;
  75. frame.origin.y = self.placeHolderOffsetY + frame.origin.y;
  76. self.placeholderLabel.frame = frame;
  77. }
  78. - (void)textDidChange
  79. {
  80. self.placeHolderHidden = self.hasText;
  81. }
  82. #pragma mark - lazy
  83. - (UILabel *)placeholderLabel
  84. {
  85. if (!_placeholderLabel) {
  86. // 添加一个用来显示占位文字的label
  87. _placeholderLabel = [[UILabel alloc] init];
  88. _placeholderLabel.numberOfLines = 0;
  89. }
  90. return _placeholderLabel;
  91. }
  92. - (void)dealloc
  93. {
  94. [[NSNotificationCenter defaultCenter] removeObserver:self];
  95. }
  96. @end