user1277610
user1277610

Reputation:

UITextView: How to really disable editing?

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

Upvotes: 14

Views: 32129

Answers (9)

anusha.V
anusha.V

Reputation: 81

cell.txtViewAllGyms.isEditable = false

Upvotes: 1

Mick Stupp
Mick Stupp

Reputation: 119

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

Upvotes: 2

Alec Gorge
Alec Gorge

Reputation: 17390

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)
}

Upvotes: 35

user11182243
user11182243

Reputation:

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

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

Upvotes: 0

ronak patel
ronak patel

Reputation: 400

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.

Upvotes: 0

Tad
Tad

Reputation: 4784

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

Attributes Inspector

Upvotes: 1

Otium
Otium

Reputation: 1098

You can use the property editable

textView.editable = NO;

Upvotes: 22

kkakkurt
kkakkurt

Reputation: 2800

For Swift 3.0 and Swift 4.0:

textView.isEditable = false

Upvotes: 6

Dan Beaulieu
Dan Beaulieu

Reputation: 19954

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

Upvotes: 6

Related Questions