iOS高仿微信表情输入功能代码分享
最近项目需求,要实现一个类似微信的的表情输入,于是把微信的表情扒拉出来,实现了一把。可以从这里下载源码。看起来表情输入没有多少东西,不外乎就是用nstextattachment来实现图文混排,结果在实现的过程中遇到了很多小问题,接下来会一一介绍遇到过的坑。先上一张效果图:
一、实现表情选择view(wkexpressionview)
具体的实现就不细说了,主要功能就是点击表情时,将对应表情的图片名称通知给delegate。
二、实现表情textview(wkexpressiontextview)
wkexpressiontextview继承自uitextview, 提供
- (void)setexpressionwithimagename:(nsstring *)imagename fontsize:(cgfloat)fontsize方法,用于根据图片插入表情。 具体实现:
//富文本 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字段,为了保存表情对应的文本,用于复制粘贴操作。
@interface wkexpressiontextattachment : nstextattachment @property (nonatomic, copy) nsstring *text; @end
wkexpressiontool的提供将普通字符串转换为富文本的方法,主要用于复制时生成表情。
主要方法
+ (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方法
- (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
@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高仿微信表情输入功能代码分享,希望对大家有所帮助