服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS高仿微信表情输入功能代码分享

iOS高仿微信表情输入功能代码分享

2021-02-18 15:38pretty guy IOS

最近项目需求,要实现一个类似微信的的表情输入功能,今天小编抽空给大家分享iOS高仿微信表情输入功能代码,非常不错,感兴趣的朋友参考下吧

最近项目需求,要实现一个类似微信的的表情输入,于是把微信的表情扒拉出来,实现了一把。可以从这里下载源码。看起来表情输入没有多少东西,不外乎就是用nstextattachment来实现图文混排,结果在实现的过程中遇到了很多小问题,接下来会一一介绍遇到过的坑。先上一张效果图:

iOS高仿微信表情输入功能代码分享

一、实现表情选择view(wkexpressionview)

具体的实现就不细说了,主要功能就是点击表情时,将对应表情的图片名称通知给delegate。

二、实现表情textview(wkexpressiontextview)

wkexpressiontextview继承自uitextview, 提供
- (void)setexpressionwithimagename:(nsstring *)imagename fontsize:(cgfloat)fontsize方法,用于根据图片插入表情。 具体实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//富文本
wkexpressiontextattachment *attachment = [[wkexpressiontextattachment alloc] initwithdata:nil oftype:nil];
uiimage *image = [uiimage imagenamed:imagename];
attachment.image = image;
attachment.text = [wkexpressiontool getexpressionstringwithimagename:imagename];
attachment.bounds = cgrectmake(0, 0, fontsize, fontsize);
nsattributedstring *insertattributestr = [nsattributedstring attributedstringwithattachment:attachment];
nsmutableattributedstring *resultattrstring = [[nsmutableattributedstring alloc] initwithattributedstring:self.attributedtext];
//在当前编辑位置插入字符串
[resultattrstring insertattributedstring:insertattributestr atindex:self.selectedrange.location];
nsrange temprange = self.selectedrange;
self.attributedtext = resultattrstring;
self.selectedrange = nsmakerange(temprange.location + 1, 0);
[self.textstorage addattributes:@{nsfontattributename : [uifont systemfontofsize:_defaultfontsize]} range:nsmakerange(0, self.attributedtext.length)];
[self scrollrangetovisible:self.selectedrange];
[self textchanged];

其中wkexpressiontextattachment继承自nstextattachment, 并新增text字段,为了保存表情对应的文本,用于复制粘贴操作。

?
1
2
3
@interface wkexpressiontextattachment : nstextattachment
@property (nonatomic, copy) nsstring *text;
@end

wkexpressiontool的提供将普通字符串转换为富文本的方法,主要用于复制时生成表情。

主要方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
+ (nsattributedstring *)generateattributestringwithoriginalstring:(nsstring *)originalstring fontsize:(cgfloat)fontsize
{
nserror *error = null;
nsmutableattributedstring *resultattrstring = [[nsmutableattributedstring alloc] initwithstring:originalstring];
nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:@"\\[[a-za-z0-9\u4e00-\u9fa5]{1,}\\]" options:nsregularexpressionallowcommentsandwhitespace error:&error];
nsarray *results = [regex matchesinstring:originalstring options:nsmatchingreportcompletion range:nsmakerange(0, originalstring.length)];
if (results) {
for (nstextcheckingresult *result in results.reverseobjectenumerator) {
nsrange resultrange = [result rangeatindex:0];
nsstring *stringresult = [originalstring substringwithrange:resultrange];
nslog(@"%s %@\n", __function__, stringresult);
nsattributedstring *expressionattrstring = [self getattributestringwithexpressionstring:stringresult fontsize:fontsize];
[resultattrstring replacecharactersinrange:resultrange withattributedstring:expressionattrstring];
}
}
return resultattrstring;
}
 
/**
* 通过表情生成富文本
*
* @param expressionstring 表情名
* @param fontsize 对应字体大小
*
* @return 富文本
*/
+ (nsattributedstring *)getattributestringwithexpressionstring:(nsstring *)expressionstring fontsize:(cgfloat)fontsize
{
nsstring *imagename = [self getexpressionstringwithimagename:expressionstring];
wkexpressiontextattachment *attachment = [[wkexpressiontextattachment alloc] initwithdata:nil oftype:nil];
uiimage *image = [uiimage imagenamed:imagename];
attachment.image = image;
attachment.text = [wkexpressiontool getexpressionstringwithimagename:imagename];
attachment.bounds = cgrectmake(0, 0, fontsize, fontsize);
nsattributedstring *appendattributestr = [nsattributedstring attributedstringwithattachment:attachment];
return appendattributestr;
}

至此,基本功能实现完成。 接下来说说遇到的小问题

编辑是应该对应selectedrange

复制粘贴操作需要重新实现

textview在插入nstextattachment后,会默认把font的size修改为12,需要记录默认的size

对应selectedrange操作

具体的操作查看源码

重新实现copy、cut方法

进行复制、粘贴操作会发现,不能对图片进行复制,所以需要自己重写copy、cut方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (void)copy:(id)sender
{
nsattributedstring *selectedstring = [self.attributedtext attributedsubstringfromrange:self.selectedrange];
nsstring *copystring = [self parseattributetexttonormalstring:selectedstring];
uipasteboard *pboard = [uipasteboard generalpasteboard];
if (copystring.length != 0) {
pboard.string = copystring;
}
}
- (void)cut:(id)sender
{
[self copy:sender];
nsmutableattributedstring *originalstring = [[nsmutableattributedstring alloc] initwithattributedstring:self.attributedtext];
[originalstring deletecharactersinrange:self.selectedrange];
self.attributedtext = originalstring;
nslog(@"--%@", nsstringfromrange(self.selectedrange));
[self textchanged];
}

记录默认font的size

利用实例变量defaultfontsize,在wkexpressiontextview实例化时记录self.font.pointsize,以后需要取font的size时,直接取defaultfontsize

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@interface wkexpressiontextview : uitextview
@property (nonatomic, assign) cgfloat defaultfontsize;
@end
@implementation wkexpressiontextview
{
cgfloat _defaultfontsize;
}
- (void)awakefromnib
{
[self setup];
}
- (instancetype)initwithframe:(cgrect)frame
{
self = [super initwithframe:frame];
if (self) {
[self setup];
}
return self;
}
- (void)setup
{
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(textchange:) name:uitextviewtextdidchangenotification object:self];
_defaultfontsize = self.font.pointsize;
self.delegate = self;
}

以上所述是小编给大家介绍的ios高仿微信表情输入功能代码分享,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/pretty-guy/p/5427224.html

延伸 · 阅读

精彩推荐
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25