将语音添加到自定义UIMenuController [英] Adding Speech to custom UIMenuController

查看:106
本文介绍了将语音添加到自定义UIMenuController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIWebView 中创建了自定义 UIMenuController ,但它似乎摆脱了Speak Selection选项之后在 UIMenuController 中。说话选择选项在所有测试设备的首选项中打开,并显示在其他应用程序中,包括非Apple应用程序。是否有可访问性服务或 sharedMenuController 的一部分我可以调用以获取此项目?

I created a custom UIMenuController in a UIWebView but it seems to get rid of the "Speak Selection" option in the UIMenuController after that. The speak selection option is turned on in Preferences on all test devices and it appears in other apps, including non-Apple apps. Is there an accessibility service or part of the sharedMenuController that I can call to get this item?

UIMenuItem *copyMenuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Copy", @"Copy menu item") action:@selector(myappCopy:)];

UIMenuItem *highlightMenuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Highlight", @"Highlight menu option") action:@selector(myappHighlight:)];

UIMenuItem *unhighlightMenuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Remove Highlight", @"Remove Highlight menu option")
                                                           action:@selector(myappRemoveHighlight:)];

UIMenuItem *noteMenuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Note", @"Note menu options") action:@selector(myappNote:)];

[UIMenuController sharedMenuController].menuItems = [NSArray arrayWithObjects:copyMenuItem, highlightMenuItem, unhighlightMenuItem, noteMenuItem, nil];

[copyMenuItem release];
[highlightMenuItem release];
[unhighlightMenuItem release];
[noteMenuItem release];

我甚至试图在开始时解析现有的共享菜单项,但我什么都没看到倾倒在日志中。该方法将在应用启动时调用。

I even tried to parse the existing shared menu items at the start, but I don't see anything dumped in the log. The method is getting called on app launch.

在方法顶部尝试此操作:

Tried this at top of method:

    for (UIMenuItem *menuItem in [UIMenuController sharedMenuController].menuItems) {
        NSLog(@"title: %@", menuItem.title);
        NSLog(@"action: %@", menuItem.action);
    }

非常感谢任何帮助!谢谢 - Eric

Any help is much appreciated! Thanks - Eric

推荐答案

在UIKit框架中的UIResponder.h中可以找到一些UIMenuController项。

Some of the UIMenuController items can be found in UIResponder.h in UIKit framework.

@interface NSObject(UIResponderStandardEditActions)   // these methods are not implemented     in NSObject

 - (void)cut:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
 - (void)copy:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
 - (void)paste:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
 - (void)select:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
 - (void)selectAll:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
 - (void)delete:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2);
 - (void)makeTextWritingDirectionLeftToRight:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
 - (void)makeTextWritingDirectionRightToLeft:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);

 @end

但那里没有说话文字选项。事实证明,如果您在UIWebView或UITextField的子类中覆盖canPerformAction:WithSelector:,您还将获得发送给self的所有操作的列表,包括UIMenuController选项。

But there is no speak text option there. It turns out if you override "canPerformAction: WithSelector:", within a subclass of your UIWebView or UITextField as listed below, you will also get a listing of all of the actions sent to self including the UIMenuController options.

// Override
- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
    NSLog(@"%@",NSStringFromSelector(action));

    //if you are customizing your menu, return NO except for your specific selectors
    return YES;
}

您会找到几种您可能感兴趣的方法,包括_accessibilitySpeak:和_accessibilityPauseSpeaking :和_define :(请注意这三个选择器仅限iOS 5)。下划线表示它们是私有的,因此请记住,您不能使用经典[类选择器]语法直接调用它们。

You'll find several methods that may interest you, including _accessibilitySpeak: and _accessibilityPauseSpeaking: and _define: (please note these three selectors are iOS 5 only). The underscore means that they are private, so also keep in mind that you can't directly call them with the classic [class selector] syntax.

请记住,这些是系统menuItems,这意味着Apple会将它们粘贴在您添加的任何菜单项的前面,通常会通过点击>箭头将您的菜单项留在第二层中。如果要控制项目的显示顺序和/或将Apple的系统项目与项目混合,则需要为这些操作创建自定义菜单项,以便在类中调用方法,如下所示:

Remember, these are system menuItems, which means Apple will stick them in front of any menu items you add, often leaving your menu items in a second layer accessed by tapping the > arrow. If you want to control the order in which the items are display, and/or mix Apple's system items with your items, you will need to create custom menu items for these actions that call a method in your class like this:

- (void) myAppSpeak: (UIMenuController*) sender
{
    [super performSelector:@selector(_accessibilitySpeak:)];
}

请记住,这些方法需要在类的子类中实现已经实现了这些,例如UIWebView的子类....而不是UIWebViewController的子类。

Keep in mind that these methods need to be implemented in a subclass of a class that implements these already, such as a sub class of UIWebView....not a subclass of UIWebViewController.

然后在控制器内部,或者在构建UIMenuController的任何地方,创建调用此方法的自定义按钮。确保如果您处于Web视图中,则引用的是子类类型的对象,而不是通用Web视图。否则,它将无法工作。

Then inside the controller, or wherever you build your UIMenuController, create the custom button that calls this method. Be sure if you are in a web view, you are referencing an object of type of your subclass, and not the generic webview. Otherwise, it won't work.

    UIMenuItem *speakMenuItem = [[UIMenuItem alloc] initWithTitle:@"Speak" action:@selector(myAppSpeak:)];

    [UIMenuController sharedMenuController].menuItems = [NSArray arrayWithObjects:speakMenuItem, etc. etc., nil];

即使您将其添加到菜单项中,除非您返回YES,否则它不会出现canPerformAction中的选择器:WithSelector:位于Web视图或文本字段的子类中。所以随意在这里添加可能是间接的项目。您可以在子类视图中使用逻辑对其进行排序。

Even though you are adding it to the your menu items, it will not appear unless you return YES for the selector in your canPerformAction: WithSelector: in your subclass of your web view or text field. So feel free to add items here that may be circumstantial otherwise. You can use logic in your subclassed view to sort that out.

这篇关于将语音添加到自定义UIMenuController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆