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; 在这个协议方法中可以实 ...
随机推荐
- CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里
这两天在GitHub上传了一个自定义ViewPager:CoolViewPager,具有以下功能特征: 支持水平及垂直方向循环滚动 支持自动滚动 支持自动滚动方向.滚动时间.间隔时间的设置 支持调用n ...
- 关于Spring MVC中的表单标签库的使用
普通的MVC设计模式中M代表模型层,V代表视图层,C代表控制器,SpringMVC是一个典型的MVC设置模式的框架,对于视图和控制器的优化很多,其中就有与控制器相结合的JSP的表单标签库. 我们先简单 ...
- Python标准库:内置函数hasattr() getattr() setattr() 函数使用方法详解
hasattr(object, name) 本函数是用来判断对象object的属性(name表示)是否存在.如果属性(name表示)存在,则返回True,否则返回False.参数object是一个对象 ...
- 爬虫IP被禁的简单解决方法——切换UserAgent
[转载]Python爬虫之UserAgent 用到的库 https://github.com/hellysmile/fake-useragent
- Exchange邮件系统日志查看及管理
1.查看邮件服务器上某个时间段内的所有邮件信息: Get-MessageTrackingLog -ResultSize Unlimited -Start "3/6/2015 8:40AM&q ...
- 解决python3 UnicodeDecodeError: 'gbk' codec can't decode byte
本来想写个html来玩玩,结果读取文件得时候就BUG了.... 以下代码读取html中无中文没有问题. def handle_request(client): buf = client.recv(10 ...
- 4星|《助推(实践版)》:英国政府用AB测试检验政策效果的经验
助推:小行动如何推动大变革(实践版)(诺贝尔经济学奖得主理查德•塞勒的助推实践) 作者作为学者说服英国政府实施助推策略的经过,提到一些具体主推策略. 所谓的助推,很像IT业流行的AB测试,对政策的执行 ...
- 由JDK源码学习HashMap
HashMap基于hash表的Map接口实现,它实现了Map接口中的所有操作.HashMap允许存储null键和null值.这是它与Hashtable的区别之一(另外一个区别是Hashtable是线程 ...
- css3动画相关笔记
1.$(".aa").delay(2500).animate({width:0}); // 延迟 2.setTimeout(function(){ --> css3 anim ...
- calayer defaultValueForKey
例如:我们新建一个SubLayer类继承自CALayer,则在SubLayer.m中重写此方法.如下: + (id)defaultValueForKey:(NSString *)key { if ([ ...