如何检测用户是否在UIMenuController中点击了格式化按钮? [英] How can I detect that a user has tapped a formatting button in a UIMenuController?

查看:177
本文介绍了如何检测用户是否在UIMenuController中点击了格式化按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序允许用户使用我创建的一些自定义格式按钮来格式化UITextView中的文本。现在我要启用文本视图的 allowsEditingTextAttributes 属性,以便用户可以。这将在标准UIMenuController中为文本视图启用粗体/斜体/下划线按钮。这很好,但是当用户通过这个菜单更改格式时,我需要调用自定义方法来更新自定义格式按钮的状态。我如何检测到用户已经点击了UIMenuController的格式化按钮之一?

我在UITextView和UIMenuController类的引用中查找了通知,但是没有看到任何相关的信息。这些格式化按钮不会触发 UITextViewTextDidChangeNotification ,也不会触发UITextViewDelegate的 textViewDidChange:方法。我也尝试了UITextView类的子类重写一个方法,但我不知道什么方法重写。它看起来像一个 bold:方法不存在,例如,即使一个 paste:方法确实存在。有人知道什么类的方法是什么时候点击一个粗体/斜体/下划线按钮?



这些按钮没有提到在文本iOS编程指南 UIMenuController类参考,据我所知。

解决方案

这可能会更有帮助。
$ b

如果您在UITextView子类中实现此方法,则可以控制是否显示菜单选项。您还可以检查日志输出以查看调用的方法。在下面的例子中,我禁用了文本样式选项菜单。

$ p $ - (BOOL)canPerformAction:(SEL)action withSender :( id)sender {
LOG(@canPerformAction:called);
FLOG(@action%@,NSStringFromSelector(action));

if(action == @selector(_showTextStyleOptions :)){

return NO;

}

return [super canPerformAction:action withSender:sender];

$ / code $


这些是一些被选中的选择器:
$ b $

 复制:
选择:
selectAll:
粘贴:
删除:
_promptForReplace:
_showTextStyleOptions:***
_define:
等...

如果您选择文本样式选项菜单,那么您将得到以下内容:

  toggleBold面板:
toggleItalics:
toggleUnderline:

希望这有助于您。


My app allows users to format text in a UITextView by using some custom formatting buttons I've created. Now I want to enable the allowsEditingTextAttributes property of the text view so that users can copy and paste formatted text between text views. This enables the bold/italic/underline buttons in the standard UIMenuController for the text view. This is fine, but when users change the formatting through this menu, I need to call my custom method that updates the state of my custom formatting buttons. How can I detect that a user has tapped one of the UIMenuController's formatting buttons?

I looked for notifications in the UITextView and UIMenuController class references, but didn't see anything relevant. These formatting buttons don't trigger a UITextViewTextDidChangeNotification and don't trigger the textViewDidChange: method of the UITextViewDelegate. I also tried subclassing the UITextView class to override a method there, but I don't know what method to override. It looks like a bold: method doesn't exist, for example, even though a paste: method does exist there. Does anyone know what method of what class is called when tapping one of the bold/italic/underline buttons?

These buttons aren't mentioned in the Text Programming Guide for iOS or the UIMenuController class reference, as far as I can tell.

解决方案

This might be more helpful.

If you implement this method in UITextView subclass then you can control whether the menu options display or not. You can also check the log output to see what methods are getting called. In the example below I am disabling the text style options menu.

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    LOG(@"canPerformAction: called");
    FLOG(@" action %@", NSStringFromSelector(action));

    if (action == @selector(_showTextStyleOptions:)) {

                return NO;

    }

    return [super canPerformAction:action withSender:sender];
}

These are some of the selectors that get checked:

copy:
select:
selectAll:
paste:
delete:
_promptForReplace:
_showTextStyleOptions:  ***
_define:
etc...

If you select the Text Style Options menu then you get the following:

toggleBoldface:
toggleItalics:
toggleUnderline:

Hope this helps.

这篇关于如何检测用户是否在UIMenuController中点击了格式化按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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