13

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

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];
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Any way to do this without changing my code? Just by adding something? –  Apr 09 '12 at 01:17

9 Answers9

34

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:

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)
}
Community
  • 1
  • 1
Alec Gorge
  • 17,110
  • 10
  • 59
  • 71
22

You can use the property editable

textView.editable = NO;
Otium
  • 1,098
  • 8
  • 20
  • Sorry, I'm pretty new to Objective-C... Where do I put that? –  Apr 09 '12 at 01:09
  • So if you want it to not be editable when your view is loaded put textView1.editable = NO; in the loadTextView1 method – Otium Apr 09 '12 at 01:14
6

Swift 2.0 Version

self.textView.editable = false

More details 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
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
6

For Swift 3.0 and Swift 4.0:

textView.isEditable = false
kkakkurt
  • 2,790
  • 1
  • 30
  • 33
1

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

Attributes Inspector

Tad
  • 4,668
  • 34
  • 35
1

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
Mick Stupp
  • 109
  • 1
  • 2
0

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 Xcode's Interface Builder uncheck "Editable" in the Attributes Inspector for the text view.

shim
  • 9,289
  • 12
  • 69
  • 108
ronak patel
  • 400
  • 5
  • 17
0

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

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

cell.txtViewAllGyms.isEditable = false

anusha.V
  • 71
  • 2