12

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

这是当前代码。

- (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];
}
4

8 回答 8

33

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

Objective-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];
}

迅速

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)
}
于 2012-04-09T01:15:09.703 回答
22

您可以使用该物业editable

textView.editable = NO;
于 2012-04-09T01:04:38.673 回答
6

斯威夫特 2.0 版本

self.textView.editable = false

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


要考虑的其他 UITextView 属性:

  • 文本
  • 属性文本
  • 字体
  • 文字颜色
  • 可编辑
  • 允许编辑文本属性
  • 数据检测器类型
  • 文本对齐
  • 打字属性
  • 链接文本属性
  • textContainerInset
于 2015-09-01T18:28:40.093 回答
6

对于 Swift 3.0 和 Swift 4.0:

textView.isEditable = false
于 2017-11-29T08:09:01.453 回答
1

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

属性检查器

于 2019-02-14T13:19:06.080 回答
1

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

commentTextView.isUserInteractionEnabled = false
于 2020-02-18T17:05:07.770 回答
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)
}

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

于 2017-12-14T07:26:16.603 回答
0

如果要阻止所有用户交互,则需要执行以下 2 件事:

    self.commentText.isEditable = false
    self.commentText.isSelectable = false
于 2019-07-06T20:03:48.400 回答