IOSUITextView图文混排显示文本和表情

本文介绍如何使用JTATEmoji库将表情符号集成到iOS应用中,包括实现步骤和代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考资料:https://github.com/joeytat/JTATEmoji

ViewController类

using System;

using UIKit;
using Foundation;

namespace EmojiDemo
{
    public partial class ViewController : UIViewController
    {
        public ViewController (IntPtr handle) : base (handle)
        {
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            NSString normalStr = new NSString("This category[kiss] allows you to [hug]convert string into given image like this [kiss] and this55");
            UIFont font = UIFont.SystemFontOfSize (15);
            txtEmoji.TextColor = UIColor.Red;
            txtEmoji.AttributedText = new NSAttributedStringAndJATEmoji ().emojiAttributedString (normalStr, font);
        }

        public override void DidReceiveMemoryWarning ()
        {
            base.DidReceiveMemoryWarning ();
        }
    }
}

NSAttributedStringAndJATEmoji类

using System;

using UIKit;
using Foundation;
using System.Drawing;
using CoreGraphics;
namespace EmojiDemo
{
    public partial class NSAttributedStringAndJATEmoji 
    {
        public NSAttributedString  emojiAttributedString(NSString str,UIFont font)
        {

            NSError error = new NSError ();
            UIStringAttributes s = new UIStringAttributes ();
            s.Font = font;
            s.ForegroundColor = UIColor.Red;
            NSMutableAttributedString parsedOutPut = new NSMutableAttributedString (str,s);
            NSString strs = new NSString (parsedOutPut.ToString ());
            string plistPath = NSBundle.MainBundle.PathForResource ("Emoji", "plist");
            NSDictionary emojiPlistDic = new NSDictionary(plistPath);
            CGSize emojiSize = new CGSize (font.LineHeight, font.LineHeight);
            var list =System.Text.RegularExpressions.Regex.Matches(strs, @"\[[A-Za-z0-9]*\]");
            for (int i = list.Count; i > 0; i--) {
                NSRange matchRange = new NSRange (list[i-1].Index, list[i-1].Length);
                string placeholder = parsedOutPut.ToString ().Substring(list[i-1].Index,list[i-1].Length);
                string imgpath=NSBundle.MainBundle.PathForResource ("Images/"+emojiPlistDic[placeholder].ToString(), "png");
                UIImage emojiImage = UIImage.FromBundle (imgpath);
                UIGraphics.BeginImageContextWithOptions (emojiSize, false,(nfloat)0);
                emojiImage.Draw (new CGRect (0, 0, emojiSize.Width, emojiSize.Height));
                UIImage resizedImage = UIGraphics.GetImageFromCurrentImageContext ();
                UIGraphics.EndImageContext ();
                NSTextAttachment textAtachment = new NSTextAttachment ();
                textAtachment.Image = resizedImage;
                textAtachment.Bounds = new CGRect (0, -4, emojiSize.Width,  emojiSize.Height);
                NSAttributedString rep = NSAttributedString.CreateFrom (textAtachment);
                parsedOutPut.Replace (matchRange, rep);
            }
            return new NSAttributedString (parsedOutPut);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值