iOS @功能的部分實現思路


需求描述

1. 發布信息時,通過鍵盤鍵入@符號,或者點選相關功能鍵,喚醒@列表,進行選擇

2.選擇結束后,輸入欄改色顯示相關內容

3.刪除時,整體刪除@區塊,且不能讓光標落在@區塊之間

 

實現步驟

1. 鍵入@符號,觸發相關功能

 1 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
 2     //判斷鍵入的字符
 3     if ([text isEqualToString:@"@"]) {
 4         //觸發相關功能
 5         //[self pickRemaidBtnClick];
 6         return NO;
 7     }
 8 
 9     return YES;
10 }

 

2. @區塊顏色修改

(1)使用到的正則表達式

#define kATRegular @"@[\\u4e00-\\u9fa5\\w\\-\\_]+ "

(2)獲取匹配到的區塊下標數組

+ (NSArray *) getMatchsWithStr : (NSString *) text {
    // 找到文本中所有的@
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:kATRegular options:NSRegularExpressionCaseInsensitive error:nil];
    NSArray *matches = [regex matchesInString:text options:NSMatchingReportProgress range:NSMakeRange(0, [text length])];
    return matches;
}

(3)修改文本顏色

-(void)textViewDidChange:(UITextView *)textView {
    NSArray *matchs = [RemaidUserUtils getMatchsWithStr:textView.text];
    //改色
    [textView.textStorage addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#333333"] range:NSMakeRange(0, textView.text.length)];
    for (NSTextCheckingResult *match in matchs)
    {
        [textView.textStorage addAttribute:NSForegroundColorAttributeName value:BLUECOLOR range:NSMakeRange(match.range.location, match.range.length - 1)];
    }
}

 

3.刪除時整體刪除@區塊

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

    if ([text isEqualToString:@""])
    {
        NSRange selectRange = textView.selectedRange;
        if (selectRange.length > 0)
        {
            //用戶長按選擇文本時不處理
            return YES;
        }
        
        // 判斷刪除的是一個@中間的字符就整體刪除
        NSMutableString *string = [NSMutableString stringWithString:textView.text];
        NSArray *matches = [RemaidUserUtils getMatchsWithStr:string];
        
        BOOL inAt = NO;
        NSInteger index = range.location;
        for (NSTextCheckingResult *match in matches)
        {
            NSRange newRange = NSMakeRange(match.range.location + 1, match.range.length - 1);
            if (NSLocationInRange(range.location, newRange))
            {
                inAt = YES;
                index = match.range.location;
                [textView.textStorage replaceCharactersInRange:match.range withString:@""];
                textView.selectedRange = NSMakeRange(index, 0);
                [self textViewDidChange:textView];
                return NO;
                break;
            }
        }
    }
    
    return YES;
}

 

4.不允許光標落在@區塊之間

- (void)textViewDidChangeSelection:(UITextView *)textView {
    // 光標不能點落在@詞中間
    NSRange range = textView.selectedRange;
    if (range.length > 0)
    {
        // 選擇文本時可以
        return;
    }
    
    NSArray *matches = [RemaidUserUtils getMatchsWithStr:textView.text];
    
    for (NSTextCheckingResult *match in matches)
    {
        NSRange newRange = NSMakeRange(match.range.location + 1, match.range.length - 1);
        if (NSLocationInRange(range.location, newRange))
        {
            if (range.location == match.range.location + 1) {
                textView.selectedRange = NSMakeRange(match.range.location + match.range.length, 0);
            } else {
                textView.selectedRange = NSMakeRange(match.range.location , 0);
            }
            break;
        }
    }
}

 

BY: chu


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM