MOMentionTextView.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // MOMentionTextView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2024/6/18.
  6. //
  7. #import "MOMentionTextView.h"
  8. @interface MOMentionTextView ()<UITextViewDelegate>
  9. @end
  10. @implementation MOMentionTextView
  11. - (instancetype)initWithFrame:(CGRect)frame {
  12. self = [super initWithFrame:frame];
  13. if (self) {
  14. self.delegate = self;
  15. }
  16. return self;
  17. }
  18. - (instancetype)initWithCoder:(NSCoder *)coder{
  19. self = [super initWithCoder:coder];
  20. if (self) {
  21. self.delegate = self;
  22. }
  23. return self;
  24. }
  25. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  26. if ([text isEqualToString:@"\n"]) {
  27. [textView resignFirstResponder]; // 隐藏键盘
  28. self.sendTextBlock ? self.sendTextBlock() : nil;
  29. return NO; // 防止输入换行符
  30. }
  31. return YES;
  32. }
  33. - (void)textViewDidChange:(UITextView *)textView {
  34. // 检查输入完成
  35. NSString *markedText = [textView textInRange:textView.markedTextRange];
  36. if (!markedText) {
  37. [self highlightMentions];
  38. }
  39. }
  40. - (void)cleanTheNSAttributedString{
  41. // 清除所有背景颜色属性
  42. NSString *text = self.text;
  43. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
  44. [attributedString removeAttribute:NSBackgroundColorAttributeName range:NSMakeRange(0, attributedString.length)];
  45. self.attributedText = attributedString;
  46. }
  47. //- (void)highlightMentions {
  48. // NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
  49. // [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, attributedString.length)];
  50. //
  51. // NSString *text = self.text;
  52. // NSRange range = NSMakeRange(0, text.length);
  53. // while (range.location != NSNotFound) {
  54. // range = [text rangeOfString:@"@" options:0 range:range];
  55. // if (range.location != NSNotFound) {
  56. // NSRange spaceRange = [text rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet] options:0 range:NSMakeRange(range.location, text.length - range.location)];
  57. // if (spaceRange.location == NSNotFound) {
  58. // spaceRange.location = text.length;
  59. // }
  60. // NSRange mentionRange = NSMakeRange(range.location, spaceRange.location - range.location);
  61. // [attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:mentionRange];
  62. // range = NSMakeRange(NSMaxRange(mentionRange), text.length - NSMaxRange(mentionRange));
  63. // }
  64. // }
  65. //
  66. // self.attributedText = attributedString;
  67. //}
  68. - (void)highlightMentions {
  69. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
  70. [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, attributedString.length)];
  71. NSString *text = self.text;
  72. NSRange searchRange = NSMakeRange(0, text.length);
  73. NSRange foundRange;
  74. while (searchRange.location < text.length) {
  75. foundRange = [text rangeOfString:@"@" options:0 range:searchRange];
  76. if (foundRange.location != NSNotFound) {
  77. NSRange mentionRange = NSMakeRange(foundRange.location, text.length - foundRange.location);
  78. NSRange spaceRange = [text rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] options:0 range:mentionRange];
  79. if (spaceRange.location != NSNotFound) {
  80. mentionRange.length = spaceRange.location - foundRange.location;
  81. } else {
  82. mentionRange.length = text.length - foundRange.location;
  83. }
  84. [attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:mentionRange];
  85. searchRange.location = NSMaxRange(mentionRange);
  86. searchRange.length = text.length - searchRange.location;
  87. } else {
  88. break;
  89. }
  90. }
  91. self.attributedText = attributedString;
  92. }
  93. - (BOOL)keyboardInputShouldDelete:(UITextView *)textView {
  94. NSRange selectedRange = self.selectedRange;
  95. if (selectedRange.length == 0 && selectedRange.location > 0) {
  96. NSAttributedString *attributedText = self.attributedText;
  97. NSRange range;
  98. id attribute = [attributedText attribute:NSBackgroundColorAttributeName atIndex:selectedRange.location - 1 effectiveRange:&range];
  99. if (attribute) {
  100. NSRange newRange = NSMakeRange(range.location, NSMaxRange(range) - range.location);
  101. NSMutableAttributedString *mutableAttributedText = [self.attributedText mutableCopy];
  102. [mutableAttributedText deleteCharactersInRange:newRange];
  103. self.attributedText = mutableAttributedText;
  104. self.selectedRange = NSMakeRange(range.location, 0);
  105. return NO;
  106. }
  107. }
  108. return YES;
  109. }
  110. @end