iOS蘋果內建UIMenuController

來源:互聯網
上載者:User

標籤:nsobject   select   uil   內容   for   lsh   use   options   效果   

一、UIMenuController認識
  • 1.預設情況下,UITextView / UITextFiled / UIWebView 都有蘋果內建的有UIMenuController功能

  • 2.UITextFiled 的快顯功能表效果系統內建的

        
     
  • 3.當然,系統只給某些控制項提供的該功能,但是我們自己給可以給指定控制項添加該功能效果,至於如何添加,我們先簡單學習一下,如何使用系統提供的UIMenuController功能

二、UIMenuController基本使用
  • 如何學UIMenuController使用,查看標頭檔是最直接的方式了。

 
  • 可得:UIMenuController繼承NSObject;我們可以自訂;通過sharedMenuController擷取單例對象;必須手動設定彈窗菜單可見;指定彈窗相對哪個View的哪個位置顯示;指定其顯示方向(上下左右);指定Item多個(數組);而且可以指定menuFrame;不僅如此,系統也提供了可以監聽menu的通知(即將顯示/完全顯示、即將隱藏/完全隱藏、menu的frame改變)

  • 1.獲得菜單 menu

    UIMenuController *menu = [UIMenuController sharedMenuController];
  • 2.設定菜單最終顯示的位置
      // 菜單最終顯示的位置  CGRect rect = CGRectMake(100, 100, 100, 100); [menu setTargetRect:rect inView:self.label]; /* targetRect:menuController指向的矩形框 targetView:targetRect以targetView的左上方為座標原點 */
  • 3.手動設定需要,顯示菜單
    // 顯示菜單[menu setMenuVisible:YES animated:YES];
  • 4.必須要得通過第一響應者,來告訴MenuController它內部應該顯示什麼內容

    • 4.1 那如何告訴顯示內容呢?
      • 讓第一響應者,實現下面方法,來告訴顯示內容,監聽哪些操作action
    /*** 通過這個方法告訴UIMenuController它內部應該顯示什麼內容* 返回YES,就代表支援action這個操作*/- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{ NSLog(@"%@", NSStringFromSelector(action)); if (action == @selector(cut:) || action == @selector(copy:) || action == @selector(paste:)) { return YES; // YES -> 代表我們只監聽 cut: / copy: / paste:方法 } return NO; // 除了上面的操作,都不支援}// 列印如下:2015-7-28 10:06:25.578 UIMenuController[4735:388013] cut:2015-7-28 10:06:25.581 UIMenuController[4735:388013] copy:2015-7-28 10:06:25.581 UIMenuController[4735:388013] select:2015-7-28 10:06:25.582 UIMenuController[4735:388013] selectAll:2015-7-28 10:06:25.582 UIMenuController[4735:388013] paste:2015-7-28 10:06:25.582 UIMenuController[4735:388013] delete:2015-7-28 10:06:25.582 UIMenuController[4735:388013] _promptForReplace:2015-7-28 10:06:25.583 UIMenuController[4735:388013] _transliterateChinese:2015-7-28 10:06:25.583 UIMenuController[4735:388013] _showTextStyleOptions:2015-7-28 10:06:25.583 UIMenuController[4735:388013] _define:2015-7-28 10:06:25.583 UIMenuController[4735:388013] _addShortcut:2015-7-28 10:06:25.583 UIMenuController[4735:388013] _accessibilitySpeak:2015-7-28 10:06:25.583 UIMenuController[4735:388013] _accessibilitySpeakLanguageSelection:2015-7-28 10:06:25.583 UIMenuController[4735:388013] _accessibilityPauseSpeaking:2015-7-28 10: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 設定第一響應者
      • 前提是:必須要有第一響應者,讓第一響應者,實現上面方法,告訴顯示什麼內容。實現下面方法,可以讓某個視圖或者控制器,成為第一響應者: canBecomeFirstResponder方法。
    /*** 說明控制器可以成為第一響應者*/- (BOOL)canBecomeFirstResponder{  return YES;}
  • 5.實現監聽菜單內容的對應的action方法
 - (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.當然,我們還可以監聽到menu菜單的顯示與隱藏與frame改變的通知.

    • 如下:監聽menu即將顯示的通知

      • 1.註冊通知監聽
        // 註冊監聽 菜單即將顯示 通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(show:) name:UIMenuControllerWillShowMenuNotification object:nil];
      • 2.實現監聽到menu菜單顯示調用方法
      - (void)dealloc{  // 移除監聽通知  [[NSNotificationCenter defaultCenter] removeObserver:self];}
      • 3.dealloc方法中,移除通知監聽
      - (void)dealloc{   // 移除監聽通知  [[NSNotificationCenter defaultCenter] removeObserver:self];}
  • 7.當然系統提供的menuItem的標題,預設是英文的,我們可以設定menu支援中文,顯示中文 ->修改軟體應用支援中文


    Snip20151028_12.png
  • 8.那麼如何自訂meun顯示自己定義的文字

    • 解決方案:查看UIMenuController的標頭檔,我們發現有個屬性menuItems數組,通過它我們可以增加額外的功能表項目。
注意:
  • 1.建立只能通過[UIMenuController sharedMenuController];單例方式擷取,不能通過init方式建立,否則報如下錯誤
 // 獲得菜單 -> 回報如下錯誤UIMenuController *menu = [[UIMenuController alloc] init];  Terminating app due to uncaught exception ‘NSInternalInconsistencyException‘, reason: ‘There can only be one UIMenuController instance.‘
三、應用1. 如何給Label添加UIMenuController功能
  • 1.設定UILabel允許互動
  • 2.給UILabel添加手勢,
  • 3.在UILabel手勢監聽方法中,建立UIMenuController-》menu
  • 4.設定menu位置,利用UIMenuController的對象方法setTargetRect: inView:方法來設定menu顯示在在那個控制項的那個位置
  • 6.顯示menu, -》 menu setMenuVisible: animation:
  • 7.設定menu顯示內容
    • 注意:得通過第一響應者,來告訴menu它內部顯示什麼內容。如果顯示中文標題,就需要手動設定APP支援中文
    • 實現:
      • 7.1讓label成為第一響應者(注意:不一定第一響應者必須是控制器)
      • 7.2設定menu顯示menuItem,告訴menu可以顯示什麼內容。
#import "JPLabel.h"@implementation JPLabel- (void)awakeFromNib{ // 給Label添加手機 [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)]];}- (void)initWithFrame:(CGRect)rect{ if(self = [super initWithFrame:rect]){ // 給Label添加手機 [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)]]; }}- (void)labelClick{ // 讓label成為第一響應者 [self becomeFirstResponder]; // 獲得菜單 UIMenuController *menu = [UIMenuController sharedMenuController]; // 設定菜單內容,顯示中文,所以要手動設定app支援中文 menu.menuItems = @[ [[UIMenuItem alloc] initWithTitle:@"頂" action:@selector(ding:)], [[UIMenuItem alloc] initWithTitle:@"回複" action:@selector(reply:)], [[UIMenuItem alloc] initWithTitle:@"舉報" action:@selector(warn:)] ]; // 菜單最終顯示的位置 [menu setTargetRect:self.bounds inView:self]; // 顯示菜單 [menu setMenuVisible:YES animated:YES];}#pragma mark - UIMenuController相關/** * 讓Label具備成為第一響應者的資格 */- (BOOL)canBecomeFirstResponder{ return YES;}/** * 通過第一響應者的這個方法告訴UIMenuController可以顯示什麼內容 */- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{ if ( (action == @selector(copy:) && self.text) // 需要有文字才能支援複製 || (action == @selector(cut:) && self.text) // 需要有文字才能支援剪下 || action == @selector(paste:) || action == @selector(ding:) || action == @selector(reply:) || action == @selector(warn:)) return YES; return NO;}#pragma mark - 監聽MenuItem的點擊事件- (void)cut:(UIMenuController *)menu{ // 將label的文字儲存到粘貼板 [UIPasteboard generalPasteboard].string = self.text; // 清空文字 self.text = nil;}- (void)copy:(UIMenuController *)menu{ // 將label的文字儲存到粘貼板 [UIPasteboard generalPasteboard].string = self.text;}- (void)paste:(UIMenuController *)menu{ // 將粘貼板的文字賦值給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蘋果內建UIMenuController

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.