ios UITextView:如何真正禁用编辑?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10067853/
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-08-30 17:45:50  来源:igfitidea点击:

UITextView: How to really disable editing?

iosobjective-cuitextview

提问by Otium

I am trying to disable editing on my UITextView. I have tried [aboutStable setUserInteractionEnabled: NO], but it causes the page to not be accessible.

我正在尝试禁用对我的UITextView. 我试过了[aboutStable setUserInteractionEnabled: NO],但它导致页面无法访问。

Here is the current code.

这是当前的代码。

- (void)loadTextView1 {
    UITextView *textView1 = [[UITextView alloc] init];
    [textView1 setFont:[UIFont fontWithName:@"Helvetica" size:14]];
    [textView1 setText:@"Example of editable UITextView"];
    [textView1 setTextColor:[UIColor blackColor]];
    [textView1 setBackgroundColor:[UIColor clearColor]];
    [textView1 setTextAlignment:UITextAlignmentLeft];
    [textView1 setFrame:CGRectMake(15, 29, 290, 288)];
    [self addSubview:textView1];
    [textView1 release];
}

回答by Alec Gorge

First of all, you are using setter methods when you could just be using properties. Secondly, you are setting a whole bunch of unnecessary properties that are very close to the default. Here is a much simpler and perhaps what you intended with your code:

首先,当您可以只使用属性时,您正在使用 setter 方法。其次,您正在设置一大堆非常接近默认值的不必要属性。这是一个更简单的代码,也许是您想要的代码:

Objective-C

目标-C

- (void)loadTextView1 {
    UITextView *textView1 = [[UITextView alloc] initWithFrame:CGRectMake(15, 29, 290, 288)];
    textView1.text = @"Example of non-editable UITextView";
    textView1.backgroundColor = [UIColor clearColor];

    textView1.editable = NO;

    [self addSubView:textView1];
    [textView1 release];
}

Swift

迅速

func loadTextView1() {
    let textView1 = UITextView(frame: CGRect(x: 15, y: 29, width: 290, height: 288))
    textView1.text = "Example of non-editable UITextView"
    textView1.backgroundColor = .clear

    textView1.isEditable = false

    addSubView(textView1)
}

回答by Otium

You can use the property editable

您可以使用该物业 editable

textView.editable = NO;

回答by Dan Beaulieu

Swift 2.0 Version

斯威夫特 2.0 版本

self.textView.editable = false

More details can be found in the apple UIKit Framework Reference.

更多细节可以在苹果UIKit 框架参考中找到



Additional UITextView Attributes to consider:

要考虑的其他 UITextView 属性:

  • text
  • attributedText
  • font
  • textColor
  • editable
  • allowsEditingTextAttributes
  • dataDetectorTypes
  • textAlignment
  • typingAttributes
  • linkTextAttributes
  • textContainerInset
  • 文本
  • 属性文本
  • 字体
  • 文字颜色
  • 可编辑
  • 允许编辑文本属性
  • 数据检测器类型
  • 文本对齐
  • 打字属性
  • 链接文本属性
  • 文本容器插入

回答by kkakkurt

For Swift 3.0 and Swift 4.0:

对于 Swift 3.0 和 Swift 4.0:

textView.isEditable = false

回答by ronak patel

Swift 3.0

斯威夫特 3.0

func loadTextView1()
 {
    let textView1 = UITextView()
    textView1.text = "Example of non-editable UITextView"
    textView1.backgroundColor = UIColor.clear
    textView1.frame = CGRect(x: 15, y: 29, width: 290, height: 288)
    textView1.isEditable = false
    addSubView(textView1)
}

Otherwise in Xcode's Interface Builder uncheck "Editable" in the Attributes Inspector for the text view.

否则,在 Xcode 的 Interface Builder 中,在文本视图的 Attributes Inspector 中取消选中“Editable”。

回答by Tad

If you are using interface builder, you can just uncheck "Editable" in the Attributes Inspector.

如果您使用的是界面构建器,您可以在属性检查器中取消选中“可编辑”。

Attributes Inspector

属性检查器

回答by Tad

If you want to prevent all user interaction you need to do the 2 following things:

如果你想阻止所有用户交互,你需要做以下两件事:

    self.commentText.isEditable = false
    self.commentText.isSelectable = false

回答by Mick Stupp

Wow, this one's certainly being done to death! Sorry to add another but I should mention you can remove any user uncertainty with:

哇,这个肯定是被干死了!很抱歉添加另一个,但我应该提到您可以通过以下方式消除任何用户不确定性:

commentTextView.isUserInteractionEnabled = false