最近任务比较少,就看了一个剪贴板的空间
@interface ViewController ()
{
UILabel *_label ;
}
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];[self testForLabel];
}
-(void)showMenu
{
UIMenuController *menu = [UIMenuController sharedMenuController];
UIMenuItem *firstItem = [[UIMenuItem alloc] initWithTitle:@"copy" action:@selector(copy:)];
UIMenuItem *secondItem = [[UIMenuItem alloc] initWithTitle:@"approve" action:@selector(approve:)];
UIMenuItem *thiredItem = [[UIMenuItem alloc] initWithTitle:@"back" action:@selector(back:)];
UIMenuItem *forthItem = [[UIMenuItem alloc] initWithTitle:@"forge" action:@selector(forge:)];
[menu setMenuItems:@[firstItem,secondItem,thiredItem,forthItem]];
// 需要将menu 添加到对应的位置上
[menu setTargetRect:_label.frame inView:_label.superview];
[menu setMenuVisible:YES animated:YES];
}
-(void)copy:(UIMenuItem *)sender
{
NSLog(@"==== %s",func);
}
-(void)approve:(UIMenuItem *)app
{
NSLog(@"==== %s",func);
}
-(void)back:(UIMenuItem *)bac
{
NSLog(@"==== %s",func);
}
-(void)forge:(UIMenuItem *)forfge{
NSLog(@"==== %s",func);
}
-(void )testForLabel
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];
label.text = @"juhdjadhjejnkdjuhdjadhjejnkdjuhdjadhjejnkdjuhdjadhjejnkdjuhdjadhjejnkd";
label.font = [UIFont systemFontOfSize:15];
label.textColor = [UIColor blueColor];
label.numberOfLines = 0;
_label = label;
[self.view addSubview:label];
label.userInteractionEnabled = YES;
label.backgroundColor = [UIColor redColor];
UILongPressGestureRecognizer *pres = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[label addGestureRecognizer:pres];
}
-(void)longPress:(UITapGestureRecognizer *)pres
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"tishi" message:@"mess" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self showMenu];
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[self presentViewController:alert animated:YES completion:^{
}];
[alert addAction:action];
[alert addAction:action2];
}
// 这一句话是必须要有的,如果在没有添加这一句话可能会导致剪贴板不显示
-(BOOL)canBecomeFirstResponder
{
return YES;
}