iOS UIMenuController菜单
//1:普通
//
// ViewController.m
// DemoTest
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - menu
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self becomeFirstResponder];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:CGRectMake(0, 0, 300, 400) inView:self.view];
[menu setMenuVisible:YES animated:YES];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", NSStringFromSelector(action));
return YES;
}
/** 剪切 */
- (void)cut:(id)sender {
[UIPasteboard generalPasteboard].string = @"cut";
//置nil
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
/** 拷贝 */
- (void)copy:(id)sender {
[UIPasteboard generalPasteboard].string = @"copy";
//不置nil
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
/** 粘贴 */
- (void)paste:(id)sender {
// NSString * tempStr = [UIPasteboard generalPasteboard].string;
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
/** 选择 */
- (void)select:(id)sender {
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
/** 全选 */
- (void)selectAll:(id)sender {
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
/** 删除 */
- (void)delete:(id)sender {
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
/** 文本写作方向从左到右 */
- (void)makeTextWritingDirectionLeftToRight:(id)sender {
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
/** 文本写作方向从右到左 */
- (void)makeTextWritingDirectionRightToLeft:(id)sender {
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
/** 切换黑体 */
- (void)toggleBoldface:(id)sender {
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
/** 切换斜体 */
- (void)toggleItalics:(id)sender {
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
/** 切换下划线 */
- (void)toggleUnderline:(id)sender {
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
/** 增加尺寸 */
- (void)increaseSize:(id)sender {
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
/** 减小尺寸 */
- (void)decreaseSize:(id)sender {
NSLog(@"-=-=-=-=-=-=-=-==-=-==%@", sender);
}
//替换
//_promptForReplace:
//简<=>繁
//_transliterateChinese:
//B/U
//_showTextStyleOptions:
//查询
//_lookup:
//添加快捷方式
//_addShortcut:
//Speak
//_accessibilitySpeak:
//Speak语言选择
//_accessibilitySpeakLanguageSelection:
//Speak暂停
//_accessibilityPauseSpeaking:
//共享
//_share:
@end
//2:在tabelview
//
// ViewController.m
// DemoTest
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.view.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.tableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - tableView menu
- (UITableView *)tableView {
if (!_tableView) {
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor greenColor];
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
return _tableView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 33;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = @"LPC";
cell.textLabel.userInteractionEnabled = YES;
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressCellHandle:)];
[cell.textLabel addGestureRecognizer:longPressGesture];
return cell;
}
//1:长按cell弹出Menu菜单
///** 允许menu菜单 */
//- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
// return YES;
//}
///** 每个cell Menu菜单 */
//- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
// return YES;
//}
//2:添加长按手势
-(void)longPressCellHandle:(UILongPressGestureRecognizer *)gesture {
[gesture.view becomeFirstResponder];
UIMenuController *menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:gesture.view.frame inView:gesture.view.superview];
[menuController setMenuVisible:YES animated:YES];
}
-(void)menuCopyBtnPressed:(UIMenuItem *)menuItem {
[UIPasteboard generalPasteboard].string = @"";
}
/** 按钮操作 */
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
if(action == @selector(copy:)) {
[UIPasteboard generalPasteboard].string = @"copy";
}
if(action == @selector(cut:)) {
[UIPasteboard generalPasteboard].string = @"cut";
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
if(action == @selector(paste:)) {
NSString *pasteString = [UIPasteboard generalPasteboard].string;
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
@end