在使用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. C#字节流通信格式

    类似通信格式的实现关键点: byte数组转float的实现,BitConvetor.toSingle() float类型转by,BitConverter.GetBytes #客户端发送public b ...

  2. Value与Sql Value

    在使用Value作为参数传递给SqlServer时 实际上传递的是SqlValue 为其赋值的一种方式,可以将datetime类型转换成string类型(yyyy-MM-dd HH:mm:ss)

  3. linux Mint 安装网易云音乐

    第一天从官网下来了最新版netease-cloud-music_1.1.0_amd64_ubuntu.deb安装,用的挺好的,第二天就打不开了郁闷,查了下很多人都遇到了这样的问题,需要root权限运行 ...

  4. Python学习---抽屉框架分析[点赞功能/文件上传分析]0317

    点赞功能分析 前台传递过来新闻id[new_id]和session[session内有用户ID和用户之间的信息]到后台 后台News数据库内用户和新闻是多对多的关系,查看第三张表中的内容,判读用户Id ...

  5. Asp.Net MVC 开发技巧(一)

    开发程序时的流程: 1.设计数据模型. 数据模型最为重要,不仅关系到数据的存储,同时程序的可扩展性,效率也受影响,甚至决定开发工作量.所以要极其认真的设计数据库的表和相关字段. 建完基本的数据模型后, ...

  6. 在Android Studio2.3中配置OpenCV4Android SDK

    在Android Studio2.3中配置OpenCV4Android SDK 一,OpenCV4Android下载地址 [2.4.11]http://onhdz331f.bkt.clouddn.co ...

  7. fzu_oop_east 第二次作业

    这次有四题: 题目1:(这题本身没难度,就是听说格式比较坑,好像) 代码: #include<iostream> #include<cstdio> using namespac ...

  8. 1、关于python第三方工具操作xls和xlsx格式的excel文档选型的吐血经历

    首先,最近看了python的一本书,其中第7章是关于文章操作的,就计划把python操作excel,word,txt,xml,html,json等格式的文档做个总结,并实现一些功能,但是,第一步就要把 ...

  9. Java 读取Excel数据——POI-3.11 XSSF

    POI  - the Java API for Microsoft Documents 1.在Apache官网下载Apache最新poi版本:poi-bin-3.11-20141221.zip,解压: ...

  10. D3——scale

    d3.scale 比例尺 “Scales are functions that map from an input domain to an output range” Domains 定义域 和 R ...