IOS UITextview实现聊天表情输入框

主要代码:
1.表情点击事件

void emotionda1_EmojiClicked(string name)
        {
            EmojiTextAttachment emojiTextAttachment = new EmojiTextAttachment ();
            emojiTextAttachment.emojiTag = new NSString(name);
            string plistPath = NSBundle.MainBundle.PathForResource ("Emoji", "plist");
            NSDictionary emojiPlistDic = new NSDictionary(plistPath);
            string imgpath=NSBundle.MainBundle.PathForResource ("Emoji/"+emojiPlistDic[name].ToString(), "png");
            UIImage emojiImage = UIImage.FromBundle (imgpath);
            UIFont font = UIFont.SystemFontOfSize (15);
            CGSize emojiSize = new CGSize (font.LineHeight,font.LineHeight);
            UIGraphics.BeginImageContextWithOptions (emojiSize, false,(nfloat)0);
            emojiImage.Draw (new CGRect (0, 0, emojiSize.Width, emojiSize.Height));
            UIImage resizedImage = UIGraphics.GetImageFromCurrentImageContext ();
            UIGraphics.EndImageContext ();
            emojiTextAttachment.Image = resizedImage;
            emojiTextAttachment.Bounds = new CGRect (0, -4, emojiSize.Width,  emojiSize.Height);
            NSAttributedString rep = NSAttributedString.CreateFrom (emojiTextAttachment);
            txtInputBox.TextStorage.Insert(rep,txtInputBox.SelectedRange.Location);
            txtInputBox.SelectedRange = new NSRange (txtInputBox.SelectedRange.Location + 1, txtInputBox.SelectedRange.Length);
}

2.Textview代理 获取输入的文本

public class MyTextViewDelegate:UITextViewDelegate
        {
            Chat chat;
            int bases = 0;
            NSMutableString plainString = new NSMutableString ();
            public MyTextViewDelegate(Chat chat)
            {
                this.chat=chat;
            }
            public override bool ShouldBeginEditing (UITextView textView)
            {

                return true;
            }
            public override bool ShouldChangeText (UITextView textView, NSRange range, string text)
            {
                if (text.Contains("\n")) {
                    NSRange textRange = new NSRange (0, textView.AttributedText.Length);
                    plainString.SetString(new NSString(textView.Text.ToString()));
                    textView.TextStorage.EnumerateAttributes(textRange,NSAttributedStringEnumeration.None,emojiBlock);
                    var sendTxt = textView.Text.Trim();
                    if (!string.IsNullOrEmpty(sendTxt))
                    {
                        textView.Text = "";
                        bases = 0;

                        Task.Run(() =>
                            {
                                //发送输入文本
                                chat.btn_send_Click(plainString.ToString());
                            });
                    }
                    plainString.SetString (new NSString(""));
                    return false;

                }
                return true;
            }
            private void emojiBlock(NSDictionary dict,NSRange range,ref bool stop)
            {
                if (dict["NSAttachment"] is EmojiTextAttachment) {

                    EmojiTextAttachment attachment = (EmojiTextAttachment)dict["NSAttachment"];
                    plainString.ReplaceCharactersInRange (new NSRange(range.Location + bases,range.Length), attachment.emojiTag);
                    bases += (int)attachment.emojiTag.Length - 1;
                }
            }
        }

3.EmojiTextAttachment

public class EmojiTextAttachment:NSTextAttachment
    {
        public NSString emojiTag;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值