| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- //
- // MOMentionTextView.m
- // MiMoLive
- //
- // Created by SuperC on 2024/6/18.
- //
- #import "MOMentionTextView.h"
- @interface MOMentionTextView ()<UITextViewDelegate>
- @end
- @implementation MOMentionTextView
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- self.delegate = self;
- }
- return self;
- }
- - (instancetype)initWithCoder:(NSCoder *)coder{
- self = [super initWithCoder:coder];
- if (self) {
- self.delegate = self;
- }
- return self;
- }
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
- if ([text isEqualToString:@"\n"]) {
- [textView resignFirstResponder]; // 隐藏键盘
- self.sendTextBlock ? self.sendTextBlock() : nil;
- return NO; // 防止输入换行符
- }
- return YES;
- }
- - (void)textViewDidChange:(UITextView *)textView {
- // 检查输入完成
- NSString *markedText = [textView textInRange:textView.markedTextRange];
-
- if (!markedText) {
- [self highlightMentions];
- }
- }
- - (void)cleanTheNSAttributedString{
- // 清除所有背景颜色属性
- NSString *text = self.text;
- NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
- [attributedString removeAttribute:NSBackgroundColorAttributeName range:NSMakeRange(0, attributedString.length)];
- self.attributedText = attributedString;
- }
- //- (void)highlightMentions {
- // NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
- // [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, attributedString.length)];
- //
- // NSString *text = self.text;
- // NSRange range = NSMakeRange(0, text.length);
- // while (range.location != NSNotFound) {
- // range = [text rangeOfString:@"@" options:0 range:range];
- // if (range.location != NSNotFound) {
- // NSRange spaceRange = [text rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet] options:0 range:NSMakeRange(range.location, text.length - range.location)];
- // if (spaceRange.location == NSNotFound) {
- // spaceRange.location = text.length;
- // }
- // NSRange mentionRange = NSMakeRange(range.location, spaceRange.location - range.location);
- // [attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:mentionRange];
- // range = NSMakeRange(NSMaxRange(mentionRange), text.length - NSMaxRange(mentionRange));
- // }
- // }
- //
- // self.attributedText = attributedString;
- //}
- - (void)highlightMentions {
- NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
- [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, attributedString.length)];
-
- NSString *text = self.text;
- NSRange searchRange = NSMakeRange(0, text.length);
- NSRange foundRange;
-
- while (searchRange.location < text.length) {
- foundRange = [text rangeOfString:@"@" options:0 range:searchRange];
- if (foundRange.location != NSNotFound) {
- NSRange mentionRange = NSMakeRange(foundRange.location, text.length - foundRange.location);
- NSRange spaceRange = [text rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] options:0 range:mentionRange];
- if (spaceRange.location != NSNotFound) {
- mentionRange.length = spaceRange.location - foundRange.location;
- } else {
- mentionRange.length = text.length - foundRange.location;
- }
- [attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:mentionRange];
- searchRange.location = NSMaxRange(mentionRange);
- searchRange.length = text.length - searchRange.location;
- } else {
- break;
- }
- }
-
- self.attributedText = attributedString;
- }
- - (BOOL)keyboardInputShouldDelete:(UITextView *)textView {
- NSRange selectedRange = self.selectedRange;
- if (selectedRange.length == 0 && selectedRange.location > 0) {
- NSAttributedString *attributedText = self.attributedText;
- NSRange range;
- id attribute = [attributedText attribute:NSBackgroundColorAttributeName atIndex:selectedRange.location - 1 effectiveRange:&range];
- if (attribute) {
- NSRange newRange = NSMakeRange(range.location, NSMaxRange(range) - range.location);
- NSMutableAttributedString *mutableAttributedText = [self.attributedText mutableCopy];
- [mutableAttributedText deleteCharactersInRange:newRange];
- self.attributedText = mutableAttributedText;
- self.selectedRange = NSMakeRange(range.location, 0);
- return NO;
- }
- }
- return YES;
- }
- @end
|