删除textview上的attributedtext一词 [英] Delete a word of attributedtext on textview

查看:170
本文介绍了删除textview上的attributedtext一词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施类似Facebook提及的内容.用户可以在文本视图中输入他们的文本消息并提及用户名.用户名将使用NSAttributedString突出显示.

I am implementing something like facebook mention. User can type their text message into the textview and mention user names. User name will be highlighted using NSAttributedString.

当用户要删除textview上的文本时,如果文本是提及名称的一部分,则我想整体删除提及名称.它不应影响textview上的其他文本和其他提及名称.

When user wants to delete the text on the textview, I want to delete the mention name as a whole if the text is part of the mention name. It should not affect other text and other mention names on the textview.

我正在尝试检测用户的更改

I am trying to detect user change in

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

但是我似乎无法找到一种方法来确定当前replaceText的attributedstring并在不更改文本其他部分的情况下将其删除.

But I cannot seem to find a way to determine the attributedstring of the current replacementText and remove it without changing other parts of the text.

感谢您的帮助.

推荐答案

也许您正在寻找类似的东西?

Maybe you are looking for something like this?

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    return [self removeUsernameText:textField.text withUsername:@"#John"];

}

- (BOOL)removeUsernameText:(NSString *)text withUsername:(NSString *)username{

    if([text containsString:username]){

        NSMutableString *string = [[NSMutableString alloc] init];

        [string replaceCharactersInRange:[text rangeOfString:username] withString:@""];

        _textField.text = string;

        return YES;

    }else{

        return NO;
    }

}

这篇关于删除textview上的attributedtext一词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆