UITextView 光标定位
在使用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 光标定位的更多相关文章
- UITextField限制中英文字数和光标定位以及第三方输入限制问题
先自定义一个UITextField的子类HLNavTextField,在控制器里调用子类的- (void)limitHansLength:(int)hans otherLength:(int)othe ...
- input长度随输入内容动态变化 input光标定位在最右侧
<input type="text" onkeydown="this.onkeyup();" onkeyup="this.size=(this. ...
- input输入框的光标定位的问题
input输入框的光标定位的问题 在给input输入框赋值的时候,或者在我之前写模拟下拉框js组件的时候,时不时会碰到光标的小bug问题,比如键盘中的 上移或者下移操作,在浏览器中光标会先移到最前端, ...
- js实现类似微信网页版在可编辑的div中粘贴内容时过滤剪贴板的内容,光标始终在粘贴内容后面,以及将光标定位到最后的方法
过滤剪贴板内容以及定位可编辑div光标的方法: <!DOCTYPE html><html lang="en"><head> <meta ...
- 可编辑div,将光标定位到文本之后
类似qq回复一样,某人评论之后,在对评论进行回复之后,将光标定位到文本之后: function set_focus() { el=document.getElementById('guestbook_ ...
- JS控制光标定位,定位到文本的某个位置
这是一个数字密码,要能够智能的跳转到文本的某个位置,就需要通过JS来控制跳转! 1.onkeyup监听 <input class="put" id="number- ...
- TextBox光标定位到文本末尾
private void RichTextBox1_TextChanged(object sender, EventArgs e) { this.richTextBox1.Select(richTex ...
- contenteditable 光标定位到最后
在Vue做项目时,做了一个div[contenteditable=true]的组件作为文本输入框 在非手动输入值后,光标会丢失,经测试以下这段代码可用,直接将光标定位到最后 function keep ...
- IOS textView获取光标定位,以及选中
当textview成为第一响应者的时候就会调用一个协议方法 - (void)textViewDidChangeSelection:(UITextView *)textView; 在这个协议方法中可以实 ...
随机推荐
- IEC62304软件维护框架
软件维护计划的任务 建立接收.记录.评估.解决和追踪医疗器械软件发行后的反馈 制定确认反馈是否是问题的标准 使用风险管理过程 使用配置管理过程 制定升级.补丁以及遗留问题修正计划 问题和修改分析的任务 ...
- vue3.0端口号修改
module.exports = { // 基本路径 baseUrl: '/', // 输出文件目录 outputDir: 'dist', // 生产环境是否生成 sourceMap 文件 produ ...
- miniblast_hash算法c语言实现
对于一组基因文件中的基因序列,选取一段基因片段,作为索引,利用hash表,查找固定的基因片段.有一定的并且容忍错误. 简单讲就是自己实现一个hashtable,将选出特定字符串建立索引,便于查询.输出 ...
- 为OS X增加环境变量
1.创建并以 TextEdit 的方式打开 ~/.bash_profile 文件 touch ~/.bash_profile; open -t ~/.bash_profile 2.新增环境变量 exp ...
- December 27th 2016 Week 53rd Tuesday
A journey of one thousand miles begins with one step. 千里之行始于足下. No matter how slowly you walk, as lo ...
- FLV视频封装格式详解
FLV视频封装格式详解 分类: FFMpeg编解码 2012-04-04 21:13 1378人阅读 评论(2) 收藏 举报 flvheaderaudiovideocodecfile 目录(?)[-] ...
- 左右值无限级分类 MVC + EntityFramework 的简单实现
在度娘上查了大半个月的资料,最后发现每个网友分享的文章都有一定的错误(PS:大家是故意的么?).最后是在看了一个ASP版本后知道了大概流程:看了一个存储过程实现的文章后知道了大概需要的功能:看了一个S ...
- ECharts 图表设置标记的大小 symbolSize 和获取标记的值
ECharts 是百度出品,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Safari等) ...
- Protocols, Generics, and Existential Containers — Wait What?
For the longest time now, I thought that the two functions above were the same. But in actuality, wh ...
- 跨路径读取cookie
同域下,即使设置了cookie的路径还是能将不同路径cookie读出来. 1.在/ctf/day3/ 目录设置一个cookie 2.其他目录下是不能访问这个cookie的 3.通过iframe可以实现 ...