我正在创建一个应用程序。在那个应用程序中,我有一小段文本生成的文本短语。现在,当用户触摸该文本时,特定的文本块将突出显示,并且它将在文本顶部显示自定义按钮而不是复制。那么我该怎么做呢?
这是我尝试过的。我已将 mu 文本放入 textview 并使 textview 不可编辑,因此不会显示键盘,我可以获取选定的文本。如果您有任何其他更好的方法来实现这一点,那么请提出建议。
UITextView * resionTV = [[UITextView alloc] initWithFrame:CGRectMake(118, 224, 190, 90)];
resionTV.backgroundColor = [UIColor clearColor];
resionTV.font = [UIFont fontWithName:@"Arial" size:14];
resionTV.text = xstr;
resionTV.editable = NO;
// resionTV.delegate = self;
resionTV.autocorrectionType = UITextAutocorrectionTypeNo;
[self.view addSubview:resionTV];
我从苹果开发者网站获得的这段代码,但不确定它是否是我正在寻找的,它不会做任何事情,所以在这里也有帮助。
- (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] == 2) {
[self becomeFirstResponder];
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Change Color" action:@selector(changeColor:)];
UIMenuController *menuCont = [UIMenuController sharedMenuController];
[menuCont setTargetRect:CGRectMake(10, 10, 50, 50) inView:self.view];
menuCont.arrowDirection = UIMenuControllerArrowLeft;
menuCont.menuItems = [NSArray arrayWithObject:menuItem];
[menuCont setMenuVisible:YES animated:YES];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {}