在UITextField上启用复制和粘贴,而不使其可编辑

时间:2009-12-17 09:40:35

标签: ios cocoa-touch uitextfield uilabel copy-paste

我希望UITextField(或理想情况下,UILabel)中的文字不可编辑,但同时让用户能够复制它要贴在别处。

6 个答案:

答案 0 :(得分:25)

我的最终解决方案如下:

我创建了UILabel的子类(UITextField应该工作相同),在点击后显示UIMenuController。 CopyableLabel.m看起来像这样:

@implementation CopyableLabel


- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if(action == @selector(copy:)) {
    return YES;
}
else {
    return [super canPerformAction:action withSender:sender];
}
}


- (BOOL)canBecomeFirstResponder {
return YES;
}


- (BOOL)becomeFirstResponder {
if([super becomeFirstResponder]) {
    self.highlighted = YES;
    return YES;
}
return NO;
}


- (void)copy:(id)sender {
UIPasteboard *board = [UIPasteboard generalPasteboard];
[board setString:self.text];
self.highlighted = NO;
[self resignFirstResponder];
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if([self isFirstResponder]) {
    self.highlighted = NO;
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setMenuVisible:NO animated:YES];
    [menu update];
    [self resignFirstResponder];
}
else if([self becomeFirstResponder]) {
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setTargetRect:self.bounds inView:self];
    [menu setMenuVisible:YES animated:YES];
}
}


@end

答案 1 :(得分:6)

这个问题很老了,我很惊讶没有人发布没有子类化的解决方案。在@mrueg的答案中提出的想法是正确的,但你不需要将任何东西子类化。我刚刚遇到这个问题并解决了这个问题:

在我的视图控制器中:

- (void)viewDidLoad {
    self.textField.delegate = self;
    self.textField.text = @"Copyable, non-editable string.";
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)copyTextFieldContent:(id)sender {
    UIPasteboard* pb = [UIPasteboard generalPasteboard];
    pb.string = self.textField.text;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // UIKit changes the first responder after this method, so we need to show the copy menu after this method returns.
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         [self becomeFirstResponder];
         UIMenuController* menuController = [UIMenuController sharedMenuController];
         UIMenuItem* copyItem = [[UIMenuItem alloc] initWithTitle:@"Copy"
                                                           action:@selector(copyTextFieldContent:)];
         menuController.menuItems = @[copyItem];
         CGRect selectionRect = textField.frame;
         [menuController setTargetRect:selectionRect inView:self.view];
         [menuController setMenuVisible:YES animated:YES];
     });
     return NO;
}

如果你想让UILabel工作,只需添加一个点击手势识别器而不是使用委托方法,它的工作方式应该相同。

答案 2 :(得分:3)

请尝试UITextView(我怀疑它会像UILabel一样对你而言)。我在editable属性设置为NO的情况下对此进行了测试,并且双击复制为我工作。

答案 3 :(得分:1)

另一种解决方案是保持UITextField已启用,但以编程方式阻止对其进行编辑。这是通过以下委托方法完成的:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return NO;
}

我不知道可能的限制,但目前适合我的需要。

答案 4 :(得分:1)

这将满足您的一切需求。将是可复制的。但不可编辑,也不会显示键盘或光标。

class ViewController: UIViewController {

    @IBOutlet weak var copyableUneditableTextfield: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        copyableUneditableTextfield.delegate = self
        copyableUneditableTextfield.inputView = UIView()   //prevents keyboard     
        copyableUneditableTextfield.tintColor = .clear     //prevents cursor
        copyableUneditableTextfield.text = "Some Text You Want User To Copy But Not Edit"

    }

}

extension ViewController: UITextFieldDelegate {

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return false //prevents editing
    }

}

答案 5 :(得分:0)

以下代码保存了我。

textField.addTarget(target, action: "textFieldEditingDidEndAction:", forControlEvents: [.EditingDidEnd])

似乎Paste是一个完整的编辑事件。