- (BOOL) canBecomeFirstResponder
{
return YES;
}
- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
if ( [UIMenuController sharedMenuController] ) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return YES;
}
如下:将会全部显示出来:
其中下边这些是可以访问的公开方法
- (void)cut:(nullableid)senderNS_AVAILABLE_IOS(3_0);
- (void)copy:(nullableid)senderNS_AVAILABLE_IOS(3_0);
- (void)paste:(nullableid)senderNS_AVAILABLE_IOS(3_0);
- (void)select:(nullableid)senderNS_AVAILABLE_IOS(3_0);
- (void)selectAll:(nullableid)senderNS_AVAILABLE_IOS(3_0);
- (void)delete:(nullableid)senderNS_AVAILABLE_IOS(3_2);
- (void)makeTextWritingDirectionLeftToRight:(nullableid)senderNS_AVAILABLE_IOS(5_0);
- (void)makeTextWritingDirectionRightToLeft:(nullableid)senderNS_AVAILABLE_IOS(5_0);
现在有个需求如下图:添加一个"添加为模版"
第一种代码
代码如下:自定义了一个PHTTextView 继承UITextView 中加入一下代码
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action ==@selector(copy:) ||
action ==@selector(selectAll:)||
action ==@selector(cut:)||
action ==@selector(select:)||
action ==@selector(paste:)) {
return[supercanPerformAction:actionwithSender:sender];//
}
returnNO;
}
在使用PHTTextView的controller中添加如下代码
UIMenuItem*menuItem = [[UIMenuItemalloc]initWithTitle:@"添加为模板"action:@selector(addToTemplate:)];
UIMenuController*menu = [UIMenuControllersharedMenuController];
[menusetMenuItems:[NSArrayarrayWithObjects:menuItem,nil]];
实现效果上图那样. 点击"全选"之后 全选按钮 就没有了
第三种代码
把 PHTTextView 中的 -(BOOL)canPerformAction:(SEL)action withSender:(id)sender方法改为第一种代码
让后在viewcontroller中添加了 如下 代码
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action ==@selector(addToTemplate:)) {//添加为模板方法
if(self.contentView.selectedRange.length>0) {
returnYES;
}else{
returnNO;
}
return[supercanPerformAction:actionwithSender:sender];
}
效果如下