PlaceHolderTextView.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // PlaceHolderTextView.m
  3. // 乐销
  4. //
  5. // Created by 隋林栋 on 2016/12/24.
  6. // Copyright © 2016年 ping. All rights reserved.
  7. //
  8. #import "PlaceHolderTextView.h"
  9. @interface PlaceHolderTextView()
  10. @end
  11. @implementation PlaceHolderTextView
  12. @synthesize textColor = _textColor;
  13. @synthesize font = _font;
  14. #pragma mark 懒加载
  15. - (void)setTextColor:(UIColor *)textColor{
  16. if (!CGColorEqualToColor(_textColor.CGColor, textColor.CGColor)) {
  17. _textColor = textColor;
  18. self.attributes = nil;
  19. }
  20. }
  21. - (void)setFont:(UIFont *)font{
  22. _font = font;
  23. self.attributes = nil;
  24. }
  25. - (void)setLineSpace:(CGFloat)lineSpace{
  26. _lineSpace = lineSpace;
  27. self.attributes = nil;
  28. }
  29. - (NSDictionary *)attributes{
  30. if (!_attributes) {
  31. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  32. paragraphStyle.lineSpacing = self.lineSpace; // 字体的行间距
  33. paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
  34. _attributes = @{NSForegroundColorAttributeName : self.textColor,
  35. NSFontAttributeName : self.font,NSParagraphStyleAttributeName:paragraphStyle
  36. };
  37. }
  38. return _attributes;
  39. }
  40. - (UILabel *)placeHolder{
  41. if (_placeHolder == nil) {
  42. _placeHolder = [UILabel new];
  43. _placeHolder.hidden = false;
  44. [GlobalMethod setLabel:_placeHolder widthLimit:0 numLines:0 fontNum:F(15) textColor:[UIColor lightGrayColor] text:@""];
  45. _placeHolder.leftTop = XY(W(6),W(8));
  46. }
  47. return _placeHolder;
  48. }
  49. - (void)setText:(NSString *)text{
  50. [super setText:text];
  51. self.attributedText = [[NSAttributedString alloc]initWithString:UnPackStr(text) attributes:self.attributes];
  52. }
  53. - (void)setAttributedText:(NSAttributedString *)attributedText{
  54. [super setAttributedText:attributedText];
  55. if (self.isFirstResponder) {
  56. self.placeHolder.hidden = true;
  57. }else{
  58. self.placeHolder.hidden = attributedText&&attributedText.length>0;
  59. }
  60. }
  61. #pragma mark 初始化
  62. - (instancetype)initWithCoder:(NSCoder *)aDecoder{
  63. self = [super initWithCoder:aDecoder];
  64. if (self) {
  65. [self setUp];
  66. }
  67. return self;
  68. }
  69. - (instancetype)initWithFrame:(CGRect)frame{
  70. self = [super initWithFrame:frame];
  71. if (self) {
  72. [self setUp];
  73. self.clipsToBounds = true;
  74. }
  75. return self;
  76. }
  77. - (void)setUp{
  78. //generate origin
  79. self.lineSpace = W(5);
  80. self.textColor = COLOR_TEXT;
  81. self.font = [UIFont systemFontOfSize:F(15)];
  82. [self addSubview:self.placeHolder];
  83. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditing:) name:UITextViewTextDidBeginEditingNotification object:self];
  84. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEdit:) name:UITextViewTextDidEndEditingNotification object:self];
  85. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChange) name:UITextViewTextDidChangeNotification object:self];
  86. }
  87. #pragma mark dealloc
  88. - (void)dealloc{
  89. [[NSNotificationCenter defaultCenter]removeObserver:self];
  90. }
  91. #pragma mark 监听方法
  92. - (void)beginEditing:(NSNotification *)notice{
  93. self.placeHolder.hidden = true;
  94. }
  95. - (void)endEdit:(NSNotification *)notice{
  96. self.placeHolder.hidden = self.text.length > 0;
  97. }
  98. - (void)didChange{
  99. //保存光标的位置
  100. if (self.markedTextRange == nil) {
  101. NSRange rangeSelect = self.selectedRange;
  102. if (rangeSelect.location == NSNotFound) {
  103. rangeSelect.location = self.text.length;
  104. }
  105. self.attributedText = [[NSAttributedString alloc]initWithString:UnPackStr(self.text) attributes:self.attributes];
  106. self.selectedRange = rangeSelect;
  107. if (self.blockTextChange) {
  108. self.blockTextChange(self);
  109. }
  110. [self changeLinesCallBlock:true];
  111. }else{
  112. if (self.blockTextChange) {
  113. self.blockTextChange(self);
  114. }
  115. [self changeLinesCallBlock:true];
  116. }
  117. }
  118. - (void)changeLinesCallBlock:(BOOL)isCall{
  119. CGFloat heightOrigin = self.numTextHeight;
  120. self.numTextHeight = [self sizeThatFits:CGSizeMake(self.width, CGFLOAT_MAX)].height;
  121. if (self.numTextHeight != heightOrigin && self.blockHeightChange && isCall) {
  122. self.blockHeightChange(self);
  123. }
  124. }
  125. @end