iOS Apple comes with Uimenucontroller

Source: Internet
Author: User

First, Uimenucontroller know
    • 1. By default, Uitextview/uitextfiled/uiwebview has the Uimenucontroller function with Apple's own

    • 2.UITextFiled pop-up menu effect system comes with


    • 3. Of course, the system only provides this functionality to certain controls, but we can add that functionality to the specified control ourselves, and as for how to add it, let's take a quick look at how to use the system-provided Uimenucontroller functionality

Second, the basic use of Uimenucontroller
    • How to learn Uimenucontroller use, view header file is the most direct way.

  • Available: Uimenucontroller inherit nsobject; we can customize; Get singleton objects by Sharedmenucontroller; You must manually set the pop-up menu to be visible; Specify which location of the pop-up window is displayed relative to which view; Specifies its display direction (up and down), specifies multiple (array) of item, and can specify menuframe; not only that, the system also provides a notification that can listen to the menu (coming/fully displayed, soon to hide/completely hidden, the menu frame changed)

  • 1. Get Menu

    UIMenuController *menu = [UIMenuController sharedMenuController];
  • 2. Set the location of the final display of the menu
      // 菜单最终显示的位置  CGRect rect = CGRectMake(100, 100, 100, 100); [menu setTargetRect:rect inView:self.label]; /* targetRect:menuController指向的矩形框 targetView:targetRect以targetView的左上角为坐标原点 */
  • 3. Manual setup required, display menu
    // 显示菜单[menu setMenuVisible:YES animated:YES];
  • 4. It must be through the first responder to tell Menucontroller what it should look inside

      • 4.1 How do I tell what the display is?
        • Let the first responder, implement the following method, to tell the display, listen to what action action
    /*** This method tells Uimenucontroller what content it should display inside * Return Yes to support action this action */-(BOOL)Canperformaction:(SEL) ActionWithsender:(ID) sender{NSLog (@"%@", Nsstringfromselector (action));if (action = =@selector (Cut:) | | Action = =@selector (Copy:) | | Action = =@selector (Paste:)) {ReturnYES;YES, on behalf of us only listen to cut:/copy:/paste: Method}ReturnNO;Except for the above operation, it is not supported}Print as follows:2015-7-2810: 06: 25.578Uimenucontroller[4,735:388,013]Cut2015-7-2810: 06: 25.581Uimenucontroller[4,735:388,013]Copy2015-7-2810: 06: 25.581Uimenucontroller[4,735:388,013]Select2015-7-2810: 06: 25.582Uimenucontroller[4,735:388,013]SelectAll:2015-7-2810: 06: 25.582Uimenucontroller[4,735:388,013]Paste2015-7-2810: 06: 25.582Uimenucontroller[4,735:388,013]Delete2015-7-2810: 06: 25.582Uimenucontroller[4,735:388,013]_promptforreplace:2015-7-2810: 06: 25.583Uimenucontroller[4,735:388,013]_transliteratechinese:2015-7-2810: 06: 25.583Uimenucontroller[4,735:388,013]_showtextstyleoptions:2015-7-2810: 06: 25.583Uimenucontroller[4,735:388,013]_define:2015-7-2810: 06: 25.583Uimenucontroller[4,735:388,013]_addshortcut:2015-7-2810: 06: 25.583Uimenucontroller[4,735:388,013]_accessibilityspeak:2015-7-2810: 06: 25.583Uimenucontroller[4,735:388,013]_accessibilityspeaklanguageselection:2015-7-2810: 06: 25.583Uimenucontroller[4,735:388,013]_accessibilitypausespeaking:2015-7-2810:06:25.583 uimenucontroller[4735:388013] _share:2015-7-28 10:06:25.584  Uimenucontroller[4735:388013]  Maketextwritingdirectionrighttoleft:2015-7-28  10:06:25.584 uimenucontroller[4735:388013] maketextwritingdirectionlefttoright:        
      • 4.2 Setting the first responder
        • The premise is: Must have first responder, let the first responder, implement the above method, tell what content to show. Implement the following method to make a view or controller a first responder: the Canbecomefirstresponder method.
    /*** 说明控制器可以成为第一响应者*/- (BOOL)canBecomeFirstResponder{  return YES;}
  • 5. The corresponding action method to implement the listener menu content
 - (void)cut:(UIMenuController *)menu{    NSLog(@"%s %@", __func__, menu);}- (void)copy:(UIMenuController *)menu{ NSLog(@"%s %@", __func__, menu);}- (void)paste:(UIMenuController *)menu{ NSLog(@"%s %@", __func__, menu);}
  • 6. Of course, we can also hear the menu display and hidden and frame changes notice.

    • As follows: Listen to the notification that menu is about to display

        • 1. Registration Notification Monitoring
          // 注册监听 菜单即将显示 通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(show:) name:UIMenuControllerWillShowMenuNotification object:nil];
        • 2. Implement the Monitor to hear the menu display call method
      - (void)dealloc{  // 移除监听通知  [[NSNotificationCenter defaultCenter] removeObserver:self];}
        • 3.dealloc method, remove notification listener
      - (void)dealloc{   // 移除监听通知  [[NSNotificationCenter defaultCenter] removeObserver:self];}
  • 7. Of course the system provides the title of the MenuItem, the default is English, we can set the menu to support Chinese, display Chinese, modify software application support Chinese


    Snip20151028_12.png
  • 8. How to customize Meun display your own defined text

    • Solution: Looking at the Uimenucontroller header file, we found a property MenuItems array, through which we can add additional menu items.
Attention:
    • 1. Creation can only be obtained by [Uimenucontroller Sharedmenucontroller]; Singleton, cannot be created by INIT, otherwise the following error is reported
// 获得菜单 -> 回报如下错误UIMenuController *menu = [[UIMenuController alloc] init];  Terminating app due to uncaught exception ‘NSInternalInconsistencyException‘, reason: ‘There can only be one UIMenuController instance.‘
Third, the application of 1. How to add Uimenucontroller functionality to a label
    • 1. Set Uilabel allow interaction
    • 2. Add gestures to Uilabel,
    • 3. In Uilabel gesture monitoring method, create uimenucontroller-"menu
    • 4. Set the menu location and use the Uimenucontroller object method Settargetrect:inview: Method to set the menu display at that location in that control
    • 6. Display menu,-"menu setmenuvisible:animation:
    • 7. Set Menu Display content
      • Note: You have to tell the menu what's inside it by the first responder. If the Chinese title is displayed, you need to manually set the app support Chinese
      • Realize:
        • 7.1 Make the label the first responder (note: Not necessarily the first responder must be a controller)
        • 7.2 Set menu display MenuItem to tell menu what to display.
#import"JPLabel.h"@implementationjplabel-(void) awakefromnib{Add a phone to a label [Self addgesturerecognizer:[[UITapGestureRecognizer Alloc] Initwithtarget:Self action:@selector (Labelclick)];} - (void) initWithFrame: (CGRect) rect{IfSelf = [Super Initwithframe:rect]) {Add a phone to a label [Self addgesturerecognizer:[[UITapGestureRecognizer Alloc] Initwithtarget:Self action:@selector (Labelclick)]; }}- (void) labelclick{Make a label the first responder [Self Becomefirstresponder];Get MenuUimenucontroller *menu = [Uimenucontroller Sharedmenucontroller];Set menu contents, display Chinese, so to manually set app support Chinese menu.menuitems = @[[[UIMenuItem Alloc] Initwithtitle:@ "Top" action:@selector (ding:)], [[UIMenuItem Alloc] Initwithtitle:@ "Reply" Action:@selector (reply:)], [[UIMenuItem Alloc] Initwithtitle:@ "Report" Action:@selector (warn:)];Where the menu is finally displayed [menu Settargetrect:Self.bounds InView:Self];display menus [Menu setmenuvisible:YES Animated:YES];}#pragma mark-uimenucontroller Related/** * Let the label have the qualification to be the first responder */-(BOOL) canbecomefirstresponder{ReturnYES;}/** * This method of first responder tells Uimenucontroller what content can be displayed */-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{if (action = =@selector (Copy:) &&Self.text)Text is required to support replication | | (Action = =@selector (cut:) &&Self.text)Text is required to support clipping | | Action = =@selector (paste:) | | action = =@selector (ding:) | | action = =@selector (reply:) | | action = =@selector (warn:))ReturnYES;ReturnNO;}#pragma mark-Monitor the MenuItem click Event-(void) Cut: (Uimenucontroller *) menu{Save the label's text to the Pasteboard [Uipasteboard generalpasteboard].string =Self.text;Clear textSelf.text =Nil;} - (voidCopy: (Uimenucontroller *) menu{Save the label's text to the Pasteboard [Uipasteboard generalpasteboard].string =Self.text;} - (void) Paste: (uimenucontroller *) menu{ //assigns the text of the Pasteboard to label self.text = [uipasteboard Generalpasteboard]. string;} -(void) Ding: (uimenucontroller *) menu{ NSLog (@ "%s%@", __func__, menu);} -(void) Reply: (Uimenucontroller *) menu{ NSLog (@ "%s%@", __func__, menu);} -(void) warn: (uimenucontroller *) menu{ NSLog (@ "%s%@", __func__, menu);} @end


iOS Apple comes with Uimenucontroller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.