2

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

我需要禁用 UITextField 上的编辑,但保留复制/粘贴功能

当我使用

textField.enabled = NO;

然后禁用复制/粘贴功能;

textField.editing = NO;

Xcode 写“不允许使用‘readonly’属性分配给属性

4

1 回答 1

0

你可以在这里找到帮助:

这是 UILabel 的子类,你可以用 UITextField 做类似的事情。并将类从 UITextField 更改为这个类。

@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
于 2013-01-26T17:37:01.887 回答