IOS UITextView支持输入、复制、粘贴、剪切自定义表情
UITextView是ios的富文本编辑控件,除了文字还可以插入图片等。今天主要介绍一下UITextView对自定义表情的处理。
1、首先识别出文本中的表情文本,然后在对应的位置插入NSTextAttachment对象,该对象存放的就是自定义表情。
static NSString *emojiTextPttern = @"\\[[0-9a-zA-Z\\u4e00-\\u9fa5]+\\]";
_emojiDic = @{@"[大笑]":@"smile",@"[爱心]":@"love"};
-(NSMutableAttributedString*)getEmojiText:(NSString*)content{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:content attributes:self.typingAttributes];
static NSRegularExpression *regExpress = nil;
if(regExpress == nil){
regExpress = [[NSRegularExpression alloc]initWithPattern:emojiTextPttern options: error:nil];
}
//通过正则表达式识别出emojiText
NSArray *matches = [regExpress matchesInString:content options: range:NSMakeRange(, content.length)];
if(matches.count > ){
for(NSTextCheckingResult *result in [matches reverseObjectEnumerator]){
NSString *emojiText = [content substringWithRange:result.range];
//构造NSTextAttachment对象
NSTextAttachment *attachment = [self createEmojiAttachment:emojiText];
if(attachment){
NSAttributedString *rep = [NSAttributedString attributedStringWithAttachment:attachment];
//在对应的位置替换
[attributedString replaceCharactersInRange:result.range withAttributedString:rep];
}
}
}
return attributedString;
}
2、构造NSTextAttachment的过程为:
-(NSTextAttachment*)createEmojiAttachment:(NSString*)emojiText{
if(emojiText.length==){
return nil;
}
NSString *imageName = _emojiDic[emojiText];
if(imageName.length == ){
return nil;
}
UIImage *image = [UIImage imageNamed:imageName];
if(image == nil){
return nil;
}
//把图片缩放到符合当前textview行高的大小
CGFloat emojiWHScale = image.size.width/1.0/image.size.height;
CGSize emojiSize = CGSizeMake(self.font.lineHeight*emojiWHScale, self.font.lineHeight);
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , emojiSize.width, emojiSize.height)];
imageView.image = image;
//防止模糊
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, [UIScreen mainScreen].scale);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *emojiImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
EmojiTextAttachment *attachment = [[EmojiTextAttachment alloc]init];
attachment.image = emojiImage;
attachment.emojiText = emojiText;
attachment.bounds = CGRectMake(, -, emojiImage.size.width, emojiImage.size.height);
return attachment;
}
其中EmojiTextAttachment继承了NSTextAttachment类,主要是为了记住自定义表情对应的emojiText,在后面实现copy和cut需要用到。EmojiTextAttachment声明为:
@interface EmojiTextAttachment : NSTextAttachment /**
保存emojiText的值
*/
@property (nonatomic, strong) NSString *emojiText;
@end
3、实现对自定义表情的粘贴
重新paste方法即可
-(void)paste:(id)sender{
UIPasteboard *defaultPasteboard = [UIPasteboard generalPasteboard];
if(defaultPasteboard.string.length>){
NSRange range = self.selectedRange;
if(range.location == NSNotFound){
range.location = self.text.length;
}
if([self.delegate textView:self shouldChangeTextInRange:range replacementText:defaultPasteboard.string]){
NSAttributedString *newAttriString = [self getEmojiText:defaultPasteboard.string];
[self insertAttriStringToTextview:newAttriString];
}
return;
}
[super paste:sender];
}
-(void)insertAttriStringToTextview:(NSAttributedString*)attriString{
NSMutableAttributedString *mulAttriString = [[NSMutableAttributedString alloc]initWithAttributedString:self.attributedText];
NSRange range = self.selectedRange;
if(range.location == NSNotFound){
range.location = self.text.length;
}
[mulAttriString insertAttributedString:attriString atIndex:range.location];
self.attributedText = [mulAttriString copy];
self.selectedRange = NSMakeRange(range.location+attriString.length, );
}
4、实现自定义表情的拷贝和剪切
拷贝和剪切自定义表情的时候,不是获取自定义表情对应的图片而是自定义表情对应的emojiText,这也是我们在上面要定义EmojiTextAttachment类的原因。具体代码如下:
-(void)copy:(id)sender{
NSRange range = self.selectedRange;
NSString *content = [self getStrContentInRange:range];
if(content.length>){
UIPasteboard *defaultPasteboard = [UIPasteboard generalPasteboard];
[defaultPasteboard setString:content];
return;
}
[super copy:sender];
}
-(void)cut:(id)sender{
NSRange range = self.selectedRange;
NSString *content = [self getStrContentInRange:range];
if(content.length>){
[super cut:sender];
UIPasteboard *defaultPasteboard = [UIPasteboard generalPasteboard];
[defaultPasteboard setString:content];
return;
}
[super cut:sender];
}
/**
把textview的attributedText转化为NSString,其中把自定义表情转化为emojiText
@param range 转化的范围
@return 返回转化后的字符串
*/
-(NSString*)getStrContentInRange:(NSRange)range{
NSMutableString *result = [[NSMutableString alloc]initWithCapacity:];
NSRange effectiveRange = NSMakeRange(range.location,);
NSUInteger length = NSMaxRange(range);
while (NSMaxRange(effectiveRange)<length) {
NSTextAttachment *attachment = [self.attributedText attribute:NSAttachmentAttributeName atIndex:NSMaxRange(effectiveRange) effectiveRange:&effectiveRange];
if(attachment){
if([attachment isKindOfClass:[EmojiTextAttachment class]]){
EmojiTextAttachment *emojiAttachment = (EmojiTextAttachment*)attachment;
[result appendString:emojiAttachment.emojiText];
}
}
else{
NSString *subStr = [self.text substringWithRange:effectiveRange];
[result appendString:subStr];
}
}
return [result copy];
}
通过上面的努力,我们已经实现了所有的功能。但是我们用起来的时候,会发现两个问题:
1、在自定义表情的后面输入文本,UITextview设置的属性(比如字体大小,颜色等)都消失,又变成了默认属性;
2、在ios 10.11系统上,长按自定义表情的时候,keyboard会退出,并且弹出保存图片的系统窗口,这样的体验也不好。
解决第一个问题:
我们在初始化的时候保存一下UITextview的typingAttributes属性,然后在每次UITextview的内容将要发生变化的时候,重置一下他的该属性。
@interface ViewController ()<UITextViewDelegate>
@property (nonatomic, strong)CustomTextView *textView; @property (nonatomic, strong)NSDictionary *typingAttributes;
@end -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
textView.typingAttributes = self.typingAttributes;
return YES;
}
解决第二个问题:
只需要实现一个delegate方法就行,直接返回NO
-(BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{
return NO;
}
IOS UITextView支持输入、复制、粘贴、剪切自定义表情的更多相关文章
- mac复制粘贴剪切
win下复制粘贴剪切: Ctrl+C,Ctrl+V,Ctrl+X; mac下lion之后已经有了一直让win用户吐槽的剪切功能: 复制粘贴剪切:Command+C,Command+V,Command+ ...
- Vim 下的复制/粘贴/剪切/撤销
一 了解 vim 有 12 个粘贴板,分别是 0.1.2.....9.a.“.+:用 :reg 命令可以查看各个粘贴板里的内容.在 vim 中简单用 y 命令只是复制到 "(双引号)粘贴板里 ...
- linux下nano中复制粘贴剪切的快捷键是什么
答: 1.复制:alt+6 2.剪切:ctrl+k 3.粘贴:ctrl+u 4.自由剪切: ctrl+6指定起始剪切位置,按上下左右键来选中内容,然后按下ctrl+k即可自由剪切 5.撤销: alt+ ...
- 富文本框编辑器实现:a、支持图片复制粘贴;b、支持word复制粘贴图文。
Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?Chrome高版本提供了可以将单张图片转换在BASE64字符串的功能.但是无法 ...
- iOS UITextView 根据输入text自适应高度
转载自:http://www.cnblogs.com/tmf-4838/p/5380495.html #import "ViewController.h" @interface V ...
- input 禁止 复制 粘贴 剪切 操作
1.代码 <Input onCopy={(e)=>{ // 禁止拷贝 e.preventDefault(); }} onPaste={(e)=>{ // 禁止粘贴 e.prevent ...
- VB中复制-粘贴-剪切键实现
If Me.ActiveControl.GetType.BaseType.ToString = "System.Windows.Forms.TextBoxBase" Then Wi ...
- ios开发之--仿(微信)自定义表情键盘
先附上demo:https://github.com/hgl753951/CusEmoji.git 效果图如下:
- 【转】MFC 自定义edit 限制输入十六进制内容 响应复制粘贴全选剪切的功能
参考地址:MFC 自定义edit 限制输入内容 响应复制粘贴全选剪切的功能 Ctrl组合键ASCII码 ^Z代表Ctrl+z ASCII值 控制字符 AS ...
随机推荐
- LeetCode & Q1-Two Sum-Easy
Array Hash Table Question Given an array of integers, return indices of the two numbers such that th ...
- Python内置函数(51)——hasattr
英文文档: hasattr(object, name) The arguments are an object and a string. The result is True if the stri ...
- Mock API是如何在开发中发光发热的?
在长期的服务过程中,我们经常会遇到前来咨询的用户与我们反馈以下这种情况:咨询者是一个前端人员,在项目开发的过程中需要与后端进行对接,遇到后端还没完成数据输出的情况下,他只好写静态模拟数据,在遇到大型项 ...
- docker实践4
我的docker学习笔记4-守护式容器 $docker run -i -t ubuntu /bin/bash $ctrl-p 或 ctrl-q # 转到后台 $docker ps $docke ...
- 前端之BOM和DOM
BOM和DOM简介 BOM(Browser Object Model)是指浏览器对象模型,它使JavaScript有能力与浏览器进行“对话”. DOM(Document Object Model)是指 ...
- python Django之Form组件
python Django之Form组件 Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 小试 ...
- Python基础--函数的定义和调用
一.函数的作用: 提高代码的可读性,减少代码的冗余,方便调用和修改,组织结构清晰 二.函数的定义:函数遵循先定义后调用的原则 1.无参函数 def funcname(): #def 是关键字,后跟函数 ...
- Java基础语法<四> 控制流程
笔记整理 来源于<Java核心技术卷 I > <Java编程思想> if while do while for switch case case标签可以是: 类型为ch ...
- PHP简单判断手机设备的方法
本文实例讲述了PHP简单判断手机设备的方法.分享给大家供大家参考,具体如下: 现在移动互联网越来越发到,很多的网站都普及了手机端浏览,为了更好的让网页在手机端显示,我们都选择了使用CSS媒体查询制作响 ...
- *Boosting*笔记
集成算法之boosting 集成方法 1. Parallel methods: 1. bagging 2. Random Forest 2. Sequence methods: 1. ...