在使用UITextView的时候, 如何在光标的位置插入字符 或者 图片? 以下Demo为你解答:

应用背景:键盘自定义emoji表情

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//    NSString *newFaceName = change[@"new"];
    NSString *newFaceName = [change objectForKey:NSKeyValueChangeNewKey];
    
    if (!newFaceName || [newFaceName isKindOfClass:[NSNull class]]) {
        return;
    }
    // 在光标位置插入表情
    [self textWithString:newFaceName];
//    [self attributedTextWithString:newFaceName];
    
} #pragma mark - text && attributedText
- (void)textWithString:(NSString *)newFaceName
{     // 1.1 获取当前输入的文字
    NSMutableString *string = [NSMutableString stringWithString:_txView.text];
    
    // 1.2 获取光标位置
    NSRange rg = _txView.selectedRange;
    if (rg.location == NSNotFound) {
        
        // 如果没找到光标,就把光标定位到文字结尾
        rg.location = _txView.text.length;
    }
    
    // 1.3 替换选中文字
    [string replaceCharactersInRange:rg withString:newFaceName];
    
    _txView.text = string;
    
    // 1.4 定位光标
    _txView.selectedRange = NSMakeRange(rg.location + newFaceName.length, 0);
} // _txView.attributedText  && 虽然能在发送微博时显示图片
// 但是由于plist 文件中的 png名字与官方不一样
// 所以发送出去的内容微博不能识别 emoji 表情
- (void)attributedTextWithString:(NSString *)newFaceName
{     // 1.1 获取当前输入的文字
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
    // 1.1.1 拼接之前的文字(图片和文字)
    [attributedText appendAttributedString:_txView.attributedText];
    
    // 1.2 获取光标位置
    NSRange rg = _txView.selectedRange;
    if (rg.location == NSNotFound) {
        
        // 如果没找到光标,就把光标定位到文字结尾
        rg.location = _txView.text.length;
    }
    
    // 1.3 替换选中文字
    // 1.3.1 加载图片
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = [UIImage imageNamed:newFaceName];
    CGFloat attchWH = _txView.font.lineHeight;
    attachment.bounds = CGRectMake(0, -3, attchWH, attchWH);
    
    NSAttributedString *attributedString = [NSAttributedString attributedStringWithAttachment:attachment];
    
    // 1.3.2 拼接图片
    [attributedText insertAttributedString:attributedString atIndex:rg.location];
    
    // 1.3.3 设置字体大小,_txView.font--> null ?!
//    NSRange range = NSMakeRange(0, attributedText.length);
//    [attributedText addAttribute:NSFontAttributeName value:_txView.font range:range];
    
    // 1.3.4 替换文字
    _txView.attributedText = attributedText;
    
    // 1.4 定位光标
    _txView.selectedRange = NSMakeRange(rg.location + 1, 0);
}

利用KVO监听输入的emoji表情

if (!_faceView) {
_faceView = [[FaceView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)]; [_faceView.faceImgView addObserver:self forKeyPath:kFaceNameKVO options:NSKeyValueObservingOptionNew context:nil];
}

部分Demo:GitHub FaceViewDemo


UITextView 光标定位的更多相关文章

  1. UITextField限制中英文字数和光标定位以及第三方输入限制问题

    先自定义一个UITextField的子类HLNavTextField,在控制器里调用子类的- (void)limitHansLength:(int)hans otherLength:(int)othe ...

  2. input长度随输入内容动态变化 input光标定位在最右侧

    <input type="text" onkeydown="this.onkeyup();" onkeyup="this.size=(this. ...

  3. input输入框的光标定位的问题

    input输入框的光标定位的问题 在给input输入框赋值的时候,或者在我之前写模拟下拉框js组件的时候,时不时会碰到光标的小bug问题,比如键盘中的 上移或者下移操作,在浏览器中光标会先移到最前端, ...

  4. js实现类似微信网页版在可编辑的div中粘贴内容时过滤剪贴板的内容,光标始终在粘贴内容后面,以及将光标定位到最后的方法

    过滤剪贴板内容以及定位可编辑div光标的方法: <!DOCTYPE html><html lang="en"><head>  <meta ...

  5. 可编辑div,将光标定位到文本之后

    类似qq回复一样,某人评论之后,在对评论进行回复之后,将光标定位到文本之后: function set_focus() { el=document.getElementById('guestbook_ ...

  6. JS控制光标定位,定位到文本的某个位置

    这是一个数字密码,要能够智能的跳转到文本的某个位置,就需要通过JS来控制跳转! 1.onkeyup监听 <input class="put" id="number- ...

  7. TextBox光标定位到文本末尾

    private void RichTextBox1_TextChanged(object sender, EventArgs e) { this.richTextBox1.Select(richTex ...

  8. contenteditable 光标定位到最后

    在Vue做项目时,做了一个div[contenteditable=true]的组件作为文本输入框 在非手动输入值后,光标会丢失,经测试以下这段代码可用,直接将光标定位到最后 function keep ...

  9. IOS textView获取光标定位,以及选中

    当textview成为第一响应者的时候就会调用一个协议方法 - (void)textViewDidChangeSelection:(UITextView *)textView; 在这个协议方法中可以实 ...

随机推荐

  1. 理解JMeter聚合报告(Aggregate Report)

    Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告”.今天再次有同行问到这个报告中的各项数据表示什么意思,顺便在这里公布一下,以备大家查阅. 如果 ...

  2. flask代码统计作业

    用户表: create table userInfo( id int not null unique auto_increment, name )not null, password ) not nu ...

  3. auto_ptr与shared_ptr ZZ

    http://blog.csdn.net/rogeryi/article/details/1442700 Part(1) 这篇文章试图说明如何使用auto_ptr和shared_ptr,从而使得动态分 ...

  4. Oracle使用ODBC连接配置

    该配置是在windows 7 32位下进行的,程序已经通过了测试(使用VBS进行的测试) 1.文件下载 ------------------------------------------------ ...

  5. Linux常用基本指令——文件处理命令

    书籍方面的推荐就不做介绍,免得别人说我有广告嫌疑.大家可以直接上百度,书籍和视频遍地都是,Linux这些方面的知识都是自己在学习视频和看书的总结,内容上可能会不完美.如果有更多的见解,欢迎直接评论. ...

  6. 【Leetcode】【Hard】Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  7. SCOM发送邮件通知

    运行方式配置:1. 新建账户--Windows域账户,安全级别较高,将其分发到SCOM管理服务器2. 配置文件--通知账户--将上一步新建的账户添加到该配置文件中的 运行方式账户,管理 所有目标对象 ...

  8. SCOM中的通配符

    通配符模式匹配按从左到右的方式完成,一次匹配一个字符或基本通配符模式.模式和传入字符串必须完全匹配,因此,举例来说,模式“abc”与字符串“abcd”不匹配.复合模式包含由 (&) 号或波形符 ...

  9. laravel with嵌套的渴求式加载

    今天在通过需求表A查询场地类型表B,然后通过表B的场地类型id去查询表C场地类型名的时候遇到了一个小的问题. 需求表A的字段:id.user_id .name等等: 中间表B的字段:id.appeal ...

  10. 乘风破浪:LeetCode真题_021_Merge Two Sorted Lists

    乘风破浪:LeetCode真题_021_Merge Two Sorted Lists 一.前言 关于链表的合并操作我们是非常熟悉的了,下面我们再温故一下将两个有序链表合并成一个的过程,这是基本功. 二 ...