iOS ZHATTextView - 微信,新浪微博,@他人等特殊文本變色功能 textView 封裝

今天項(xiàng)目新增的話題功能,類似于新浪微博,可以@他人、并且@他人和話題的文本必須是高亮狀態(tài)。


at_1.gif

其實(shí)實(shí)現(xiàn)名字高亮狀態(tài)特別的簡單,只需要通過正則表達(dá)式進(jìn)行字符搜索,將特定字符改變顏色

NSString *pattern1 = @"\\@(.*?)\\ ";
NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:pattern1 options: NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches1 = [regex1 matchesInString:string options:0 range:NSMakeRange(0, string.length)];
for (NSTextCheckingResult *result1 in matches1) {
    NSRange range1 = [result1 rangeAtIndex:0];
    NSAttributedString *talkTitle_A = [attrStr attributedSubstringFromRange:NSMakeRange(range1.location + 1, range1.length - 2)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(range1.location, length1)];
}

其中“\@(.*?)\ ”這個(gè)表示搜索從“@”符號(hào)到“空格”符號(hào)的范圍的結(jié)果,根據(jù)這個(gè)搜索結(jié)果將特定范圍的字符改變顏色;
但是并不是所有的@符號(hào)到空格符號(hào)里面的內(nèi)容都是用戶名稱,所以我特意加了一個(gè)判斷,通過查找是否在@他人里列表里面;

NSString *pattern1 = @"\\@(.*?)\\ ";
NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:pattern1 options: NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches1 = [regex1 matchesInString:string options:0 range:NSMakeRange(0, string.length)];
for (NSTextCheckingResult *result1 in matches1) {
    NSRange range1 = [result1 rangeAtIndex:0];
    NSAttributedString *talkTitle_A = [attrStr attributedSubstringFromRange:NSMakeRange(range1.location + 1, range1.length - 2)];
    for (Friend *friend in _chooseDataDic.allValues) {
        if ([friend.nickName isEqualToString:talkTitle_A.string]) {
            NSString *subStr1 = [attrStr.string substringWithRange:range1];
            NSUInteger length1 = subStr1.length;
            [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(range1.location, length1)];
            [attrStr addAttribute:NSFontAttributeName value:textView.font range:NSMakeRange(0, string.length)];
        }
    }
}

這寫代碼都是放在UITextView 的代理

 - (void)textViewDidChange:(UITextView *)textView

然后又發(fā)現(xiàn)了一個(gè)問題,在刪除的時(shí)候發(fā)現(xiàn)只能通過一個(gè)字符一個(gè)字符的刪除,查看了微博的發(fā)布頁面,發(fā)現(xiàn)發(fā)布頁面也是通過一個(gè)字符一個(gè)字符進(jìn)行刪除,但是微信是可以將名字和@符號(hào)一起刪除,所以又實(shí)現(xiàn)了下面方法。


at_2.gif

通過在UITextView 的代理

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

實(shí)現(xiàn)監(jiān)聽刪除空格的時(shí)候,查詢是否存在用戶名稱和@符號(hào)然后進(jìn)行統(tǒng)一刪除

 if ([text isEqualToString:@""]) {
    NSRange rangeDefault = textView.selectedRange;
    NSString *last = [textView.text substringWithRange:NSMakeRange(rangeDefault.location - 1, 1)];
    if ([last isEqualToString:@" "]) {
        NSString *textViewStr = [textView.text substringToIndex:rangeDefault.location - 1];
        NSRange nameRange = [textViewStr rangeOfString:@"@" options:NSBackwardsSearch];
        if (nameRange.length > 0) {
            NSString *nameStr = [textViewStr substringFromIndex:nameRange.location + 1];
            for (Friend *friend in _chooseDataDic.allValues) {
                if ([nameStr isEqualToString:friend.nickName]) {
                    NSMutableString *newtextView = [NSMutableString stringWithFormat:@"%@",textView.text];
                    [newtextView deleteCharactersInRange:NSMakeRange(nameRange.location + 1, nameStr.length + 1)];
                    textView.text = newtextView;
                }
            }
        }
    }
}

最后又發(fā)現(xiàn)了光標(biāo)的問題,我在移動(dòng)光標(biāo)到特定@他人的名字中插入數(shù)據(jù),但是對應(yīng)的字符并沒有發(fā)生變化,對此,網(wǎng)上有一下方法是通過控制光標(biāo)不能跳轉(zhuǎn)到指定的位置,我認(rèn)為這中方法也是可行的。我在這里的處理方式是任你插入數(shù)據(jù),只是如果你插入數(shù)據(jù),我查詢判斷你沒有在我@用戶的列表里面,我自動(dòng)將顏色改變?yōu)楹谏?/p>

at_3.gif
- (void)textViewDidChange:(UITextView *)textView
{
if (textView.text.length <= lowerLimit) {
    self.textView.text = [NSString stringWithFormat:@"#%@#",_info.talk_title];
}
_footRightLab.text = [NSString stringWithFormat:@"%lu/500",(unsigned long)self.textView.text.length];
UITextRange *selectedRange = [textView markedTextRange];
UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
if (!position) {
    [self findAllKeywordsChangeColor:textView];
}
}
- (void)findAllKeywordsChangeColor:(UITextView *)textView
{
NSString *string = textView.text;
NSRange rangeDefault = textView.selectedRange;
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:string];
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range: NSMakeRange(0, string.length)];
NSString *pattern = @"\\#(.*?)\\#";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options: NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
if (!matches || matches.count == 0) {
    return;
}
NSTextCheckingResult *result = matches[0];
NSRange range = [result rangeAtIndex:0];
if (range.location > 0) {
    [attrStr deleteCharactersInRange:NSMakeRange(0, range.location)];
    textView.text = attrStr.string;
    [self findAllKeywordsChangeColor:textView];
    return;
}
NSAttributedString *talkTitle = [attrStr attributedSubstringFromRange:range];
if (![talkTitle.string isEqualToString:[NSString stringWithFormat:@"#%@#",_info.talk_title]]) {
    [attrStr replaceCharactersInRange:range withString:[NSString stringWithFormat:@"#%@#",_info.talk_title]];
    textView.text = attrStr.string;
    [self findAllKeywordsChangeColor:textView];
    return;
}

NSString *pattern1 = @"\\@(.*?)\\ ";
NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:pattern1 options: NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches1 = [regex1 matchesInString:string options:0 range:NSMakeRange(0, string.length)];
for (NSTextCheckingResult *result1 in matches1) {
    NSRange range1 = [result1 rangeAtIndex:0];
    NSAttributedString *talkTitle_A = [attrStr attributedSubstringFromRange:NSMakeRange(range1.location + 1, range1.length - 2)];
    for (Friend *friend in _chooseDataDic.allValues) {
        if ([friend.nickName isEqualToString:talkTitle_A.string]) {
            NSString *subStr1 = [attrStr.string substringWithRange:range1];
            NSUInteger length1 = subStr1.length;
            [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(range1.location, length1)];
            [attrStr addAttribute:NSFontAttributeName value:textView.font range:NSMakeRange(0, string.length)];
        }
    }
}
NSString *subStr = [attrStr.string substringWithRange:range];
NSUInteger length = subStr.length;
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(range.location, length)];
[attrStr addAttribute:NSFontAttributeName value:textView.font range:NSMakeRange(0, string.length)];
textView.attributedText = attrStr;
NSRange rangeNow = NSMakeRange(rangeDefault.location, 0);
textView.selectedRange = rangeNow;
_hiddentextStr = textView.text;
}

dome

https://github.com/Zhangyunjiang123/ZHATTextView

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容