[Text Relatives II]

  When your app determines that the user has requested the edit menu—which could be the action of making a selection—you should complete the following steps to display the menu:

  1. Call the sharedMenuController class method of UIMenuController to get the global menu-controller instance.

  2. Compute the boundaries of the selection and with the resulting rectangle call the setTargetRect:inView: method. The edit menu is displayed above or below this rectangle, depending how close the selection is to the top or bottom of the screen.

  3. Call the setMenuVisible:animated: method (with YES for both arguments) to animate the display of the edit menu above or below the selection.

  Displaying the edit menu:

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *theTouch = [touches anyObject]; if ([theTouch tapCount] == && [self becomeFirstResponder]) { // selection management code goes here... // bring up edit menu.
UIMenuController *theMenu = [UIMenuController sharedMenuController];
CGRect selectionRect = CGRectMake (currentSelection.x, currentSelection.y, SIDE, SIDE);
[theMenu setTargetRect:selectionRect inView:self];
[theMenu setMenuVisible:YES animated:YES]; }
}

  Before the menu is displayed, however, the system sends a canPerformAction:withSender: message to the first responder, which in many cases is the custom view itself.

  Conditionally enabling menu commands:

 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
BOOL retValue = NO;
ColorTile *theTile = [self colorTileForOrigin:currentSelection]; if (action == @selector(paste:) )
retValue = (theTile == nil) &&
[[UIPasteboard generalPasteboard] containsPasteboardTypes:
[NSArray arrayWithObject:ColorTileUTI]];
else if ( action == @selector(cut:) || action == @selector(copy:) )
retValue = (theTile != nil);
else
retValue = [super canPerformAction:action withSender:sender];
return retValue;
}

  Implementing a Change Color menu item:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *theTouch = [touches anyObject];
if ([theTouch tapCount] == ) {
[self becomeFirstResponder];
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Change Color" action:@selector(changeColor:)];
UIMenuController *menuCont = [UIMenuController sharedMenuController];
[menuCont setTargetRect:self.frame inView:self.superview];
menuCont.arrowDirection = UIMenuControllerArrowLeft;
menuCont.menuItems = [NSArray arrayWithObject:menuItem];
[menuCont setMenuVisible:YES animated:YES];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {} - (BOOL)canBecomeFirstResponder { return YES; } - (void)changeColor:(id)sender {
if ([self.viewColor isEqual:[UIColor blackColor]]) {
self.viewColor = [UIColor redColor];
} else {
self.viewColor = [UIColor blackColor];
}
[self setNeedsDisplay];
}

  Dismiss the Edit Menu:

 [UIMenuController sharedMenuController].menuVisible = YES;

Text Relatives II的更多相关文章

  1. Text Relatives

    [Text Relatives] With TextKit the resources at your disposal range from framework objects—such as te ...

  2. Open Xml SDK 引文

    什么是Open Xml SDK? 什么是Open Xml? 首先,我们得知道,Open Xml为何物? 我们还是给她起个名字——就叫 “开放Xml”,以方便我们中文的阅读习惯.之所以起开放这个名字,因 ...

  3. JavaEE XML 基础知识

    JavaEE XML 基础知识 @author ixenos 1.    XML开头都需要一个声明 <?和?>表明这是一个处理指令 <?xml version=”1.0” encod ...

  4. freetype之PC机体验

    目录 freetype之PC机体验 引入 中文教程 官方教程 代码结构 字体概念 PC上安装 官方例子 宽字符保存显示中文 坐标框架体系 字符坐标信息获取 title: freetype之PC机体验 ...

  5. 拼接字符串,生成tree格式的JSON数组

    之前做的执法文书的工作,现在需要从C#版本移植到网页版,从Thrift接口获取数据,加载到对应的控件中 之前用的easyui的Tree插件,通过<ul><li><span ...

  6. oracle已知会导致错误结果的bug列表(Bug Issues Known to cause Wrong Results)

    LAST UPDATE:     1 Dec 15, 2016 APPLIES TO:     1 2 3 4 Oracle Database - Enterprise Edition - Versi ...

  7. spider_action

    spider from mobile to mobile to mobile from selenium import webdriver from selenium.webdriver.chrome ...

  8. firstChild.nodeValue

    var ia=document.getElementsByTagName("em");var t=600; for(var ii=0;ii<t;ii++){var it=ia ...

  9. Probabilistic PCA、Kernel PCA以及t-SNE

    Probabilistic PCA 在之前的文章PCA与LDA介绍中介绍了PCA的基本原理,这一部分主要在此基础上进行扩展,在PCA中引入概率的元素,具体思路是对每个数据$\vec{x}_i$,假设$ ...

随机推荐

  1. 【C++11】新特性 之 auto的使用

      C++11中引入的auto主要有两种用途:自己主动类型判断和返回值占位.auto在C++98中的标识暂时变量的语义,因为使用极少且多余.在C++11中已被删除.前后两个标准的auto,全然是两个概 ...

  2. Mysql中谓词使用date_format的优化

    优化前: SELECT a.* FROM t1 a, (SELECT obj_id,MAX(PRE_DETAIL_INST_ID) PRE_DETAIL_INST_ID FROM t1 WHERE D ...

  3. Javascript replace 为什么只替换一个字符?

    Javascript replace 为什么只替换一个字符? 如下代码,为什么结果是 "a2b1c1" ? 'a1b1c1'.replace('1', 2); 因为 javascr ...

  4. Hive之 hive的三种使用方式(CLI、HWI、Thrift)

    Hive有三种使用方式——CLI命令行,HWI(hie web interface)浏览器 以及 Thrift客户端连接方式. 1.hive  命令行模式 直接输入/hive/bin/hive的执行程 ...

  5. git hooks 的学习使用

    示例网址: https://my.oschina.net/u/3477605/blog/1806238 https://www.jianshu.com/p/935409ce4c9a https://w ...

  6. Non-standard evaluation, how tidy eval builds on base R

    As with many aspects of the tidyverse, its non-standard evaluation (NSE) implementation is not somet ...

  7. 删除TFS项目上的文件

    1.用vs(版本)开发人员命令提示输入命令进行删除 1.

  8. 事务的ACID和四个隔离级别

    在实际的业务场景中,并发读写引出了和事务控制的需求.优秀的事务处理能力是关系型数据库(特别是oracle等商用RDBMS)相对于正当风口的NoSQL数据库的一大亮点.但这也从另一方面说明了事务控制的复 ...

  9. 微软开源rDSN分布式系统开发框架

    摘要:微软亚洲研究院系统组开发的分布式系统开发框架——Robust Distributed System Nucleus(rDSN)正式在GitHub平台开源.据悉,rDSN是一个旨在为广大分布式系统 ...

  10. Java 遍历文件夹里面的全部文件、指定文件

    Java 手册 listFiles public File[] listFiles(FileFilter filter) 返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件 ...