草庐IT

ios - 选择里面没有选择文本的textView?

coder 2024-01-23 原文

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

我想要像 Messenger 应用这样的东西:

目前我有:

提前致谢!

最佳答案

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

步骤如下:

  1. 创建UITextView的子类
  2. 覆盖 canPerformAction: withSender: 用于过滤菜单的默认 Action 弹出。
  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)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;
}

协议(protocol)一致性

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

输出

享受 :)

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

有关ios - 选择里面没有选择文本的textView?的更多相关文章

  1. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  2. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  3. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  4. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  5. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  6. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  7. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

  8. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  9. ruby - Rails 3 的 RGB 颜色选择器 - 2

    状态:我正在构建一个应用程序,其中需要一个可供用户选择颜色的字段,该字段将包含RGB颜色代码字符串。我已经测试了一个看起来很漂亮但效果不佳的。它是“挑剔的颜色”,并托管在此存储库中:https://github.com/Astorsoft/picky-color.在这里我打开一个关于它的一些问题的问题。问题:请建议我在Rails3应用程序中使用一些颜色选择器。 最佳答案 也许页面上的列表jQueryUIDevelopment:ColorPicker为您提供开箱即用的产品。原因是jQuery现在包含在Rails3应用程序中,因此使用基

  10. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

随机推荐