我在UITextView
内有一个UITableViewCell
。我希望UITextView
可以选择但不是文本。当Menu Controller
出现并且我想执行复制操作时,复制所有文本而不仅仅是部分内容。
我想要一些像messenger app:
目前我有:
提前谢谢!
答案 0 :(得分:1)
事实上它并不那么容易,因为在路上会出现一些奇怪的东西,但我设法创建了一个定制的。
步骤如下:
UITextView
canPerformAction:
withSender:
以过滤菜单的默认操作
弹出。textView
以便无法修改或
的选择 UIMenuController
项目UIMenuItem
****添加不同的选择器(这是因为选择器的发件人不是UIMenuItem
,而是UIMenuController
导致另一个问题。检查{ {3}}了解更多信息。here由我提供)不再这么说了,这里是代码:
<强> EBCustomTextView.h 强>
//
// EBCustomTextView.h
// TestProject
//
// Created by Erid Bardhaj on 3/8/16.
// Copyright © 2016 Erid Bardhaj. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, EBCustomTextViewMenuAction) {
EBCustomTextViewMenuActionCopy,
EBCustomTextViewMenuActionDefine,
EBCustomTextViewMenuActionRead
};
@class EBCustomTextView;
@protocol EBCustomTextViewMenuActionDelegate <NSObject>
- (void)customTextView:(EBCustomTextView *)textView didSelectMenuAction:(EBCustomTextViewMenuAction)action;
@end
@interface EBCustomTextView : UITextView
@property (weak, nonatomic) id<EBCustomTextViewMenuActionDelegate> menuActionDelegate;
@end
<强> EBCustomTextView.m 强>
//
// EBCustomTextView.m
// TestProject
//
// Created by Erid Bardhaj on 3/8/16.
// Copyright © 2016 Erid Bardhaj. All rights reserved.
//
#import "EBCustomTextView.h"
@implementation EBCustomTextView {
EBCustomTextViewMenuAction currentAction;
}
- (void)awakeFromNib {
[super awakeFromNib];
// Configure the textView
self.editable = NO;
self.selectable = NO;
}
#pragma mark - Datasource
- (NSArray *)items {
UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"Copy" action:@selector(copyMenuItemPressed)];
UIMenuItem *item1 = [[UIMenuItem alloc] initWithTitle:@"Define" action:@selector(defineMenuItemPressed)];
UIMenuItem *item2 = [[UIMenuItem alloc] initWithTitle:@"Read" action:@selector(readMenuItemPressed)];
return @[item, item1, item2];
}
#pragma mark - Actions
- (void)copyMenuItemPressed {
if ([self.menuActionDelegate respondsToSelector:@selector(customTextView:didSelectMenuAction:)]) {
[self.menuActionDelegate customTextView:self didSelectMenuAction:EBCustomTextViewMenuActionCopy];
}
}
- (void)defineMenuItemPressed {
if ([self.menuActionDelegate respondsToSelector:@selector(customTextView:didSelectMenuAction:)]) {
[self.menuActionDelegate customTextView:self didSelectMenuAction:EBCustomTextViewMenuActionDefine];
}
}
- (void)readMenuItemPressed {
if ([self.menuActionDelegate respondsToSelector:@selector(customTextView:didSelectMenuAction:)]) {
[self.menuActionDelegate customTextView:self didSelectMenuAction:EBCustomTextViewMenuActionRead];
}
}
#pragma mark - Private
- (void)menuItemPressedAtIndex:(NSInteger)index {
currentAction = index;
if ([self.menuActionDelegate respondsToSelector:@selector(customTextView:didSelectMenuAction:)]) {
[self.menuActionDelegate customTextView:self didSelectMenuAction:currentAction];
}
}
#pragma mark Helpers
- (void)showMenuController {
UIMenuController *theMenu = [UIMenuController sharedMenuController];
theMenu.menuItems = [self items];
[theMenu update];
CGRect selectionRect = CGRectMake (0, 0, self.contentSize.width, self.contentSize.height);
[theMenu setTargetRect:selectionRect inView:self];
[theMenu setMenuVisible:(theMenu.isMenuVisible) ? NO : YES animated:YES];
}
#pragma mark - Overridings
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
// Filter any action for this textView except our custom ones
if (action == @selector(copyMenuItemPressed) || action == @selector(defineMenuItemPressed) || action == @selector(readMenuItemPressed)) {
return YES;
}
return NO;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *theTouch = [touches anyObject];
if ([theTouch tapCount] == 1 && [self becomeFirstResponder]) {
[self showMenuController];
}
}
@end
将textView的课程设置为EBCustomTextView
并符合EBCustomTextViewMenuActionDelegate
<强>接口强>
@interface ViewController () <EBCustomTextViewMenuActionDelegate>
<强> viewDidLoad中强>
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.textView.menuActionDelegate = self;
}
协议一致性
#pragma mark - Delegation
#pragma mark EBCustomTextViewMenuActionDelegate
- (void)customTextView:(EBCustomTextView *)textView didSelectMenuAction:(EBCustomTextViewMenuAction)action {
switch (action) {
case EBCustomTextViewMenuActionCopy:
NSLog(@"Copy Action");
break;
case EBCustomTextViewMenuActionRead:
NSLog(@"Read Action");
break;
case EBCustomTextViewMenuActionDefine:
NSLog(@"Define Action");
break;
default:
break;
}
}
享受:)
答案 1 :(得分:0)
你必须使用
[UITextView selectAll:self];
或(具有特定范围)
UITextView.selectedRange = NSMakeRange(0, 5);
或添加txtview.delegate = self;
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
dispatch_async(dispatch_get_main_queue(), ^{
[textView selectAll:nil]; //nil or self as per needed
});
return YES;
}
答案 2 :(得分:0)
使用UILongPressGestureRecognizer手势
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
{
UITextView * txtV;
UILongPressGestureRecognizer * longPressGestureRecognizer;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
txtV=[[UITextView alloc]initWithFrame:CGRectMake(20, 50, 280, 300)];
txtV.text = @"Jai Maharashtra";
txtV.userInteractionEnabled=YES;
txtV.selectable=NO;
txtV.editable=NO;
txtV.font= [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
[self.view addSubview:txtV];
longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
longPressGestureRecognizer.delegate=self;
[txtV addGestureRecognizer:longPressGestureRecognizer];
}
- (void)longPressGesture:(UILongPressGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
txtV.selectable=YES;
[txtV selectAll:self];
}
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
[txtV selectAll:self];
if (action == @selector(cut:))
{
return YES;
}
if (action == @selector(selectAll:))
{
return YES;
}
if (action == @selector(copy:))
{
return YES;
}
if (action == @selector(delete:))
{
return YES;
}
txtV.selectable=NO;
return YES;
}
答案 3 :(得分:0)
首先,我从单元格UITextView
中的每个UILongPressGestureRecognizer
开始创建,以识别长按。
在cellForRowAtIndexPath
我只需添加该代码:
UILongPressGestureRecognizer *recognizer1 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressText1:)];
messageRecognizer.delaysTouchesBegan = YES;
UILongPressGestureRecognizer *recognizer2 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressText2:)];
translateRecognizer.delaysTouchesBegan = YES;
[cell.backgroundViewText1 addGestureRecognizer:recognizer1];
[cell.backgroundViewText2 addGestureRecognizer:recognizer2];
我使用两个不同的选择器,因为我需要知道从单元格中获取哪个文本。
方法之后:
-(void)handleLongPressText1:(UILongPressGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
CGPoint p = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
if (indexPath == nil){
NSLog(@"couldn't find index path");
} else {
[self becomeFirstResponder];
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *resetMenuItem = [[UIMenuItem alloc] initWithTitle:@"Copy" action:@selector(copyMethod)];
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Read" action:@selector(readButtonAction)];
[menuController setMenuItems:[NSArray arrayWithObjects:resetMenuItem,menuItem, nil]];
CGRect rect =gestureRecognizer.view.frame;
NSLog(@"%@", NSStringFromCGRect(rect));
[menuController setTargetRect:gestureRecognizer.view.frame inView:[[gestureRecognizer view] superview]];
[menuController setMenuVisible:YES animated:YES];
}
}
}
-(void)handleLongPressText2:(UILongPressGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
CGPoint p = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
if (indexPath == nil){
NSLog(@"couldn't find index path");
} else {
[self becomeFirstResponder];
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *resetMenuItem = [[UIMenuItem alloc] initWithTitle:@"Copy" action:@selector(copyMethod)];
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Read" action:@selector(readButtonAction)];
[menuController setMenuItems:[NSArray arrayWithObjects:resetMenuItem,menuItem, nil]];
CGRect rect =gestureRecognizer.view.frame;
NSLog(@"%@", NSStringFromCGRect(rect));
[menuController setTargetRect:gestureRecognizer.view.frame inView:[[gestureRecognizer view] superview]];
[menuController setMenuVisible:YES animated:YES];
}
}
}
要禁用UIMenuController
中的所有默认项,请使用上面的代码
- (BOOL)canPerformAction:(SEL)iAction withSender:(id)iSender {
SEL copySelector = NSSelectorFromString(@"copyMethod");
SEL readSeletor = NSSelectorFromString(@"readButtonAction");
if (iAction == copySelector) {
return YES;
}else if (iAction ==readSeletor){
return YES;
}else{
return NO;
}
return NO;
}