iOS textview中增加@人功能

textview中增加@人功能

第一步實(shí)現(xiàn)textView代理

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"@"]) {
        [self selectFriendVc];
        return NO;
    }
    
    if ([text isEqualToString:@""]) {
        return ![self deleteAtUser:range];
    }
    
    if ([text isEqualToString:@"\n"]) {
        [self dClickSendBtnActiond];
        return NO;
    }
    return YES;
}

第二步判斷"@"符號(hào),跳轉(zhuǎn)

第三步實(shí)現(xiàn)刪除功能

- (BOOL)deleteAtUser:(NSRange)range{
    // 因?yàn)楣鈽?biāo)在 rang中間也要?jiǎng)h除  所以用for循環(huán)
    NNUserInfoModel *delemodel = [[NNUserInfoModel alloc]init];
    for (NNUserInfoModel *model in self.selectedFriend) {
        NSString *atName = [NSString stringWithFormat:@"@%@",model.userName];
        NSRange atRange = [self.inputTextView.text rangeOfString:atName];
        if (range.location>=atRange.location && range.location < atRange.location + atRange.length) {
            NSMutableString *mStr = [[NSMutableString alloc]initWithString:self.inputTextView.text];
            [mStr deleteCharactersInRange:atRange];
            delemodel = model;
            self.inputTextView.text = mStr;
            self.inputTextView.selectedRange = NSMakeRange(atRange.location, 0);//定位光標(biāo)
            break;
        }
    }
    if (delemodel.uid > 0) {
        [self.selectedFriend removeObject:delemodel];
        [self textViewDidChange:self.inputTextView];
        return YES;
    }
    return NO;
}

第四步 內(nèi)容改變刷新位置

-(void)textViewDidChange:(UITextView *)textView {
    
    UITextRange *markedTextRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:markedTextRange.start offset:0];
    
    if (position) {
        return;
    }
    
    NSRange selectedRange = textView.selectedRange;
    
    NSMutableAttributedString *attributedComment = [[NSMutableAttributedString alloc] initWithString:textView.text attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:14], NSForegroundColorAttributeName: pUIColorFromRGBp(0x242323)}];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 5.0;
    [attributedComment addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedComment.length)];
    [self.selFriendRangeDict removeAllObjects];
    for (NNUserInfoModel *model in self.selectedFriend) {
        NSRange range = [textView.text rangeOfString:[NSString stringWithFormat:@"@%@",model.userName]];
        [self.selFriendRangeDict setValue:@{@"location":@(range.location),@"length":@(range.length), @"selectModel":model} forKey:[NSString stringWithFormat:@"%ld",(long)(range.location+range.length)]];
        
        [attributedComment addAttribute:NSForegroundColorAttributeName
                                  value:MainDistort
                                  range:range];
    }
    NSUInteger offset = textView.attributedText.length - attributedComment.length;
    textView.attributedText = attributedComment;
    textView.selectedRange = NSMakeRange(selectedRange.location - offset, 0);
    CGFloat height = textView.contentSize.height+20+7;
    if ([textView.text length] > scoopCommentLength) {
        self.inputTextView.text = [textView.text substringWithRange:NSMakeRange(0, scoopCommentLength)];
    }else{
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(height < 40 ? 40 : height);
        }];
        [UIView animateWithDuration:0.3 animations:^{
            [self.superview layoutIfNeeded];
        }];
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • UI調(diào)試是每一個(gè)APP開(kāi)發(fā)者或者前端開(kāi)發(fā)者必備的技術(shù)。相對(duì)來(lái)說(shuō),iOS開(kāi)發(fā)者調(diào)試UI是最苦逼的。無(wú)論是用Story...
    黑暗中的孤影閱讀 5,927評(píng)論 24 57
  • 在傳統(tǒng)的Android應(yīng)用開(kāi)發(fā)中,布局文件通常只負(fù)責(zé)應(yīng)用界面的布局工作,如果需要實(shí)現(xiàn)頁(yè)面交互就需要調(diào)用setCon...
    烏托邦式的愛(ài)情閱讀 2,607評(píng)論 0 4
  • 本文總結(jié)了iOS中最常見(jiàn)的視頻播放方法,不同的方法都各具特點(diǎn),我希望能夠總結(jié)它們的不同,方便在開(kāi)發(fā)中選擇合適的技術(shù)...
    KiVin丶閱讀 1,267評(píng)論 0 0
  • Android中Mosby MVP的使用 1 入門 1.1 MVP簡(jiǎn)介 MVP的出發(fā)點(diǎn)是關(guān)注點(diǎn)分離,將視圖和業(yè)務(wù)邏...
    LuckyXiang閱讀 2,739評(píng)論 2 3
  • 非常感謝大家利用自己寶貴的時(shí)間來(lái)閱讀我的文章 , 今天給大家?guī)?lái)的是一個(gè)集成訊飛語(yǔ)音識(shí)別功能的小demo,為tex...
    筱貳筆閱讀 728評(píng)論 0 0

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