xcode 在 UITextView (swift) 中禁用光标和复制/粘贴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28419861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:37:27  来源:igfitidea点击:

Disable cursor and copy/paste in UITextView (swift)

iosxcodeswiftuitextfielduipickerview

提问by rgamber

I have a UITextFieldfor which I am using UIPickerViewas its inputView. The user can select a value from the picker, and the selected value gets populated into my TextInput.
This arrangement works fine, but with the following issues:

我有一个UITextFieldUIPickerView用作它的inputView. 用户可以从选择器中选择一个值,所选值将填充到 my TextInput.
这种安排工作正常,但有以下问题:

1) I want to disable the cursor that still shows in the UITextField.
I tried disabling the UITextField, but then it does not respond to touches and then the UIPickerViewdoes not show - which makes it useless.
2) Since the picker is shown and the keyboard is not wen the user taps on the text-field, the user cannot type anything, but still can paste text copied from else where by long pressing. How can I disable this?

1)我想禁用仍然显示在UITextField.
我尝试禁用UITextField,但是它不响应触摸,然后UIPickerView不显示 - 这使它无用。
2)由于显示了选择器并且键盘不是用户点击文本字段时,用户无法输入任何内容,但仍然可以通过长按粘贴从其他地方复制的文本。我怎样才能禁用它?

I am not able to find the relevant information online. Should I be using a Labelor a Buttonfor this instead of the UITextInput?

我在网上找不到相关信息。我应该为此使用 aLabel还是 aButton而不是UITextInput

采纳答案by TonyMkenu

I think the easy way is to make it with a button instead UITextField

我认为最简单的方法是用一个按钮来代替 UITextField

Sorry - this is not for Swiftit's for Obj-Cbut this is the idea:

抱歉 - 这不是为了Swift它,Obj-C而是为了这个想法:

To do what you want with UITextFieldyou have to subclass the UITextFieldand try to use this code to disable/hide caret and input (copy/paste)

要执行您想要的操作,UITextField您必须子类化UITextField并尝试使用此代码禁用/隐藏插入符号和输入(复制/粘贴)

- (CGRect) caretRectForPosition:(UITextPosition*) position
{
    return CGRectZero;
}

- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
    return nil;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copy:) || action == @selector(selectAll:) || action == @selector(paste:))
    {
        returnNO;
    }
    return [super canPerformAction:action withSender:sender];
}

example from here: http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/

来自这里的示例:http: //b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/

Anyway... this is a "functional" example: https://github.com/hackiftekhar/IQDropDownTextField

无论如何......这是一个“功能”示例:https: //github.com/hackiftekhar/IQDropDownTextField

回答by mr.sosa

Like TonyMkenu said, you need to subclass UITextField and then implement the methods he has above. Here's the Swift for those of you who don't know Objective C:

就像 TonyMkenu 说的,你需要继承 UITextField ,然后实现他上面的方法。这是为那些不了解 Objective C 的人准备的 Swift:

override func caretRectForPosition(position: UITextPosition!) -> CGRect {
    return CGRect.zeroRect
}

override func selectionRectsForRange(range: UITextRange) -> [AnyObject] {
    return []
}

override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
    // Disable copy, select all, paste
    if action == Selector("copy:") || action == Selector("selectAll:") || action == Selector("paste:") {
        return false
    }
    // Default
    return super.canPerformAction(action, withSender: sender)
}

回答by Piyush Sanepara

SWIFT 4

斯威夫特 4

Create TextField.swift file & change import Foundation to import UIKit & add following code in TextFeild.swift file

创建 TextField.swift 文件并更改 import Foundation 以导入 UIKit 并在 TextFeild.swift 文件中添加以下代码

import UIKit

class TextFeild: UITextField {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // write code here what ever you want to change property for textfeild. 
    }

    override func caretRect(for position: UITextPosition) -> CGRect {
        return CGRect.zero
    }

    override func selectionRects(for range: UITextRange) -> [Any] {
        return []
    }

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.selectAll(_:)) || action == #selector(UIResponderStandardEditActions.paste(_:)) {
            return false
        }
        // Default
        return super.canPerformAction(action, withSender: sender)
    }
}

Go to main.storyboard, select that textfield & change its custom class to TextField

转到 main.storyboard,选择该文本字段并将其自定义类更改为 TextField

Xcode Image

Xcode 图像

回答by brandonscript

Totally silly hack, but if you set the text field's tint color in the UIViewsection of the Interface Builder property inspector to match the background color, the cursor will appear invisible:

完全愚蠢的 hack,但是如果您UIView在 Interface Builder 属性检查器的部分中设置文本字段的色调颜色以匹配背景颜色,则光标将不可见:

回答by user3386109

The UITextFielddelegate can implement the textFieldShouldBeginEditingmethod. If that method always returns NO, then the cursor will never appear and the long press won't be allowed.

UITextField委托可以实现的textFieldShouldBeginEditing方法。如果该方法总是返回NO,则光标将永远不会出现并且不允许长按。

When the method is called, you can display the UIPickerView. However, the UIPickerViewcannot be an inputView. It would need to be the child of a standard UIViewthat you animate in from the bottom. Or you could just use the hiddenproperty of the UIViewto hide/show the view as needed.

调用该方法时,您可以显示UIPickerView. 但是,UIPickerView不能是inputView. 它需要是UIView您从底部设置动画的标准的孩子。或者您可以根据需要使用 的hidden属性UIView来隐藏/显示视图。