但项目中需要通过点击一个表情按钮来弹出emoji面板,所以需要知道如何通过代码来调出emoji面板,在网上找了一年,终于找到了,给大家分享一下,点击表情按钮时,如果输入框没有获取焦点的话,我们需要通过代码来让输入框获取焦点,并将光标定位到字符串末尾,具体代码很简单,也有注释,就不赘述了,直接上代码:
public override void ViewDidLoad()
{
base.ViewDidLoad();
NSTextField tf1 = new NSTextField(new CoreGraphics.CGRect(100,100,100,30));
tf1.StringValue = "1";
this.View.AddSubview(tf1);
NSTextField tf2 = new NSTextField(new CoreGraphics.CGRect(250, 100, 100, 30));
tf2.StringValue = "2";
this.View.AddSubview(tf2);
NSButton emojiBtn = new NSButton(new CoreGraphics.CGRect(400,100,100,30));
emojiBtn.Title = "打开emoji表情面板";
emojiBtn.Activated += (sender, e) => {
if (tf1.CurrentEditor==null)
{
//CurrentEditor为null说明输入框没有获取焦点
tf1.SelectText(this);//让输入框获取焦点并且选中所有文本
tf1.CurrentEditor.SelectedRange = new NSRange(tf1.StringValue.Length, 0);//将输入光标定位到字符串末尾
}
//打开emoji面板
NSApplication.SharedApplication.OrderFrontCharacterPalette(this);
};
this.View.AddSubview(emojiBtn);
// Do any additional setup after loading the view.
}