选择一个没有选择文本的textView?

时间:2016-03-08 10:39:52

标签: ios objective-c uitableview uitextview

我在UITextView内有一个UITableViewCell。我希望UITextView可以选择但不是文本。当Menu Controller出现并且我想执行复制操作时,复制所有文本而不仅仅是部分内容。

我想要一些像messenger app:

enter image description here

目前我有:

enter image description here

提前谢谢!

4 个答案:

答案 0 :(得分:1)

事实上它并不那么容易,因为在路上会出现一些奇怪的东西,但我设法创建了一个定制的。

步骤如下:

  1. 创建UITextView
  2. 的子类
  3. 覆盖canPerformAction: withSender:以过滤菜单的默认操作 弹出。
  4. 配置textView以便无法修改或 的选择
  5. 编辑提供菜单上的操作和按钮的UIMenuController项目
  6. 为每个UIMenuItem ****添加不同的选择器(这是因为选择器的发件人不是UIMenuItem,而是UIMenuController导致另一个问题。检查{ {3}}了解更多信息。here由我提供)
  7. 代码

    不再这么说了,这里是代码:

    <强> 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;
        }
    }
    

    输出

    gist

    享受:)

答案 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;
}