菜鸟教程小白 发表于 2022-12-13 15:08:39

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


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

<p>我想要类似信使应用的东西:</p>

<p> <a href="/image/6FBYg.png" rel="noreferrer noopener nofollow"><img src="/image/6FBYg.png" alt="enter image description here"/></a> </p>

<p>目前我有:</p>

<p> <a href="/image/enNAB.png" rel="noreferrer noopener nofollow"><img src="/image/enNAB.png" alt="enter image description here"/></a> </p>

<p>提前感谢您! </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>事实上,这并不容易,因为在路上会出现一些奇怪的东西,但我设法创建了一个自定义的。</p>

<p>步骤如下:</p>

<ol>
<li>创建<code>UITextView</code>的子类</li>
<li>覆盖<code>canPerformAction:
withSender:</code> 用于过滤菜单的默认操作
弹出。</li>
<li>配置 <code>textView</code> 以便无法<strong>编辑</strong>或
<strong>选择</strong>。</li>
<li>编辑 <code>UIMenuController</code> 项,提供菜单上的操作和按钮</li>
<li>为每个<code>UIMenuItem</code> ****添加不同的选择器(那是因为选择器的发送者不是<code>UIMenuItem</code>,而是<code>UIMenuController</code> 这导致了另一件事。查看 <a href="https://stackoverflow.com/a/9874092/2664437" rel="noreferrer noopener nofollow">here</a> 了解更多信息。我的 <a href="https://gist.github.com/eridbardhaj/768351b7c3632ae015ac" rel="noreferrer noopener nofollow">gist</a>)</li>
</ol>

<h2>代码</h2>

<p>废话不多说,代码如下:</p>

<p><strong>EBCustomTextView.h</strong></p>

<pre><code>//
//EBCustomTextView.h
//TestProject
//
//Created by Erid Bardhaj on 3/8/16.
//Copyright © 2016 Erid Bardhaj. All rights reserved.
//

#import &lt;UIKit/UIKit.h&gt;

typedef NS_ENUM(NSInteger, EBCustomTextViewMenuAction) {
    EBCustomTextViewMenuActionCopy,
    EBCustomTextViewMenuActionDefine,
    EBCustomTextViewMenuActionRead
};

@class EBCustomTextView;
@protocol EBCustomTextViewMenuActionDelegate &lt;NSObject&gt;

- (void)customTextView:(EBCustomTextView *)textView didSelectMenuAction:(EBCustomTextViewMenuAction)action;

@end

@interface EBCustomTextView : UITextView

@property (weak, nonatomic) id&lt;EBCustomTextViewMenuActionDelegate&gt; menuActionDelegate;

@end
</code></pre>

<p><strong>EBCustomTextView.m</strong></p>

<pre><code>//
//EBCustomTextView.m
//TestProject
//
//Created by Erid Bardhaj on 3/8/16.
//Copyright © 2016 Erid Bardhaj. All rights reserved.
//

#import &#34;EBCustomTextView.h&#34;

@implementation EBCustomTextView {
    EBCustomTextViewMenuAction currentAction;
}

- (void)awakeFromNib {
    ;

    // Configure the textView
    self.editable = NO;
    self.selectable = NO;
}

#pragma mark - Datasource

- (NSArray *)items {
    UIMenuItem *item = [ initWithTitle:@&#34;Copy&#34; action:@selector(copyMenuItemPressed)];
    UIMenuItem *item1 = [ initWithTitle:@&#34;Define&#34; action:@selector(defineMenuItemPressed)];
    UIMenuItem *item2 = [ initWithTitle:@&#34;Read&#34; action:@selector(readMenuItemPressed)];

    return @;
}


#pragma mark - Actions

- (void)copyMenuItemPressed {
    if () {
      ;
    }
}

- (void)defineMenuItemPressed {
    if () {
      ;
    }
}

- (void)readMenuItemPressed {
    if () {
      ;
    }
}

#pragma mark - Private

- (void)menuItemPressedAtIndex:(NSInteger)index {
    currentAction = index;

    if () {
      ;
    }
}

#pragma mark Helpers

- (void)showMenuController {
    UIMenuController *theMenu = ;
    theMenu.menuItems = ;
    ;

    CGRect selectionRect = CGRectMake (0, 0, self.contentSize.width, self.contentSize.height);
    ;
    ;
}

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

    if ( == 1&amp;&amp; ) {
      ;
    }
}


@end
</code></pre>

<h2>实现</h2>

<p>将你的 textView 的类设置为 <code>EBCustomTextView</code> 并符合 <code>EBCustomTextViewMenuActionDelegate</code></p>

<p><strong>界面</strong></p>

<pre><code>@interface ViewController () &lt;EBCustomTextViewMenuActionDelegate&gt;
</code></pre>

<p><strong>viewDidLoad</strong></p>

<pre><code>- (void)viewDidLoad {
    ;
    // Do any additional setup after loading the view, typically from a nib.

    self.textView.menuActionDelegate = self;
}
</code></pre>

<p><strong>协议(protocol)一致性</strong></p>

<pre><code>#pragma mark - Delegation

#pragma mark EBCustomTextViewMenuActionDelegate

- (void)customTextView:(EBCustomTextView *)textView didSelectMenuAction:(EBCustomTextViewMenuAction)action {
    switch (action) {
      case EBCustomTextViewMenuActionCopy:
            NSLog(@&#34;Copy Action&#34;);
            break;

      case EBCustomTextViewMenuActionRead:
            NSLog(@&#34;Read Action&#34;);
            break;

      case EBCustomTextViewMenuActionDefine:
            NSLog(@&#34;Define Action&#34;);
            break;

      default:
            break;
    }
}
</code></pre>

<h2>输出</h2>

<p> <a href="/image/UuDPx.gif" rel="noreferrer noopener nofollow"><img src="/image/UuDPx.gif" alt="enter image description here"/></a> </p>

<p>享受:)</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 选择一个 textView 里面没有选择文本?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35865135/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35865135/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 选择一个 textView 里面没有选择文本?