OGeek|极客世界-中国程序员成长平台

标题: ios - 选择一个 textView 里面没有选择文本? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:08
标题: ios - 选择一个 textView 里面没有选择文本?

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

我想要类似信使应用的东西:

enter image description here

目前我有:

enter image description here

提前感谢您!



Best Answer-推荐答案


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

步骤如下:

  1. 创建UITextView的子类
  2. 覆盖canPerformAction: withSender: 用于过滤菜单的默认操作 弹出。
  3. 配置 textView 以便无法编辑选择
  4. 编辑 UIMenuController 项,提供菜单上的操作和按钮
  5. 为每个UIMenuItem ****添加不同的选择器(那是因为选择器的发送者不是UIMenuItem,而是UIMenuController 这导致了另一件事。查看 here 了解更多信息。我的 gist)

代码

废话不多说,代码如下:

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)customTextViewEBCustomTextView *)textView didSelectMenuActionEBCustomTextViewMenuAction)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" actionselector(copyMenuItemPressed)];
    UIMenuItem *item1 = [[UIMenuItem alloc] initWithTitle"Define" actionselector(defineMenuItemPressed)];
    UIMenuItem *item2 = [[UIMenuItem alloc] initWithTitle"Read" actionselector(readMenuItemPressed)];

    return @[item, item1, item2];
}


#pragma mark - Actions

- (void)copyMenuItemPressed {
    if ([self.menuActionDelegate respondsToSelectorselector(customTextView:didSelectMenuAction]) {
        [self.menuActionDelegate customTextView:self didSelectMenuAction:EBCustomTextViewMenuActionCopy];
    }
}

- (void)defineMenuItemPressed {
    if ([self.menuActionDelegate respondsToSelectorselector(customTextView:didSelectMenuAction]) {
        [self.menuActionDelegate customTextView:self didSelectMenuAction:EBCustomTextViewMenuActionDefine];
    }
}

- (void)readMenuItemPressed {
    if ([self.menuActionDelegate respondsToSelectorselector(customTextView:didSelectMenuAction]) {
        [self.menuActionDelegate customTextView:self didSelectMenuAction:EBCustomTextViewMenuActionRead];
    }
}

#pragma mark - Private

- (void)menuItemPressedAtIndexNSInteger)index {
    currentAction = index;

    if ([self.menuActionDelegate respondsToSelectorselector(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 setMenuVisibletheMenu.isMenuVisible) ? NO : YES animated:YES];
}

#pragma mark - Overridings

- (BOOL)canPerformActionSEL)action withSenderid)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)touchesEndedNSSet *)touches withEventUIEvent *)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;
}

协议(protocol)一致性

#pragma mark - Delegation

#pragma mark EBCustomTextViewMenuActionDelegate 

- (void)customTextViewEBCustomTextView *)textView didSelectMenuActionEBCustomTextViewMenuAction)action {
    switch (action) {
        case EBCustomTextViewMenuActionCopy:
            NSLog(@"Copy Action");
            break;

        case EBCustomTextViewMenuActionRead:
            NSLog(@"Read Action");
            break;

        case EBCustomTextViewMenuActionDefine:
            NSLog(@"Define Action");
            break;

        default:
            break;
    }
}

输出

enter image description here

享受

关于ios - 选择一个 textView 里面没有选择文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35865135/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4