Dean Le
Dean Le

Reputation: 2094

Display edit menu (copy/paste/select) on TextView upon focus()

I'm using cross-platform framework NativeScript 1.5 and I'm trying to show the edit menu on TextView upon it is on focus. In this case with iOS, I tried to create a thing called UIMenuController like this:

var menu = UIMenuController.sharedMenuController(); console.log(menu);

The system printed out that menu is a UIMenuController object. After that, I want the edit menu pop up right away after the cursor is blinking on the textview, so:

textview.focus(); menu.menuVisible = true; However, nothing happened. Am I doing wrong or something? Or is there any way better to do this? Any comments is much appreciated.

Upvotes: 0

Views: 531

Answers (1)

Nathanael
Nathanael

Reputation: 5399

Update my exact code

XML:

<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded">
  <StackLayout>
    <Button text="Menu" tap="menu" />
    <TextField id="te" text="Hi this is a test of the emergency system.  panic now."/>
  </StackLayout>
</Page>

JS:

var page;
exports.pageLoaded = function(args) {
    page = args.object;
};

exports.menu = function() {
  var menu = UIMenuController.sharedMenuController();
  var te = page.getViewById('te');
  te.focus();
  menu.setTargetRectInView(page._nativeView.frame, te.ios);
  menu.setMenuVisibleAnimated(true, true);
}

Ok, the main issue is you need to set a target initially; without the target it doesn't know where to display the menu apparently. In my tests because I clicked into the field initially to type data; the menu got a target so the setMenuVisibileAnimated worked. But if you don't "tap" into the field it apparently doesn't get a Target Rectangle. So by telling it where it needs to display; it works.

Upvotes: 1

Related Questions