UITextView: how to really disable editing?

I am trying to disable editing on my UITextView . I tried [aboutStable setUserInteractionEnabled: NO] , but this causes the page to be inaccessible.

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]; } 
+7
source share
7 answers

First of all, you use installation methods when you can just use properties. Secondly, you set up a bunch of unnecessary properties that are very close to the default settings. Here is much simpler and possibly what you intended using your code:

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

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) } 
+21
source

You can use the editable property

 textView.editable = NO; 
+20
source

Swift Version 2.0

 self.textView.editable = false 

More information can be found in the Apple UIKit Framework Reference .


Additional UITextView attributes to consider:

  • text
  • attributedText
  • font
  • Textcolor
  • editable
  • allowsEditingTextAttributes
  • dataDetectorTypes
  • TextAlignment
  • typingAttributes
  • linkTextAttributes
  • textContainerInset
+6
source

For Swift 3.0 and Swift 4.0:

 textView.isEditable = false 
+4
source

Swift 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 the Xcode Interface Developer, uncheck the "Editable" checkbox in the Attributes Inspector for the text view.

0
source

If you use the interface designer, you can simply uncheck the "Editable" checkbox in the Attributes Inspector.

Attributes Inspector

0
source

If you want to prevent any user interaction, you need to do 2 of the following things:

  self.commentText.isEditable = false self.commentText.isSelectable = false 
0
source

Source: https://habr.com/ru/post/912706/


All Articles