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];
}
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)
}
You can use the property editable
textView.editable = NO;
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
For Swift 3.0 and Swift 4.0:
textView.isEditable = false
If you are using interface builder, you can just uncheck "Editable" in the Attributes Inspector.
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
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.
If you want to prevent all user interaction you need to do the 2 following things:
self.commentText.isEditable = false
self.commentText.isSelectable = false
cell.txtViewAllGyms.isEditable = false
Related
I am trying to make a label programmatically and for some reason it is not appearing. I have declared it on the .h file and this is what I have on the .m:
TitleLabel.textColor = [UIColor whiteColor];
TitleLabel.frame =CGRectMake(65, 30, 200, 50);
TitleLabel.textAlignment = NSTextAlignmentCenter;
TitleLabel.alpha = 1;
TitleLabel.font = [UIFont fontWithName:#"BrandonGrotesque-Black" size:23];
TitleLabel.backgroundColor = [UIColor blackColor];
TitleLabel.text = #"TEST";
Can anyone help me with this minor issue. Thanks
You're missing the init, do this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width ,height)];
First of all you need to create label ojbect with alloc/init methods:
titleLabel = [[UILabel alloc] init];
or
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 30, 200, 50)];
And at the end you need to add it on the view:
[self.view addSubview:titleLabel];
And also, just a little advice - use CamelCase coding style:
http://en.wikipedia.org/wiki/CamelCase
It is best practice to name instances of classes from lower case, like titleLabel, not TitleLabel
I am trying to create dynamic UITextViewController for my iOS application.So from my ApplicationViewController I am trying to get the TextView.
Textview is coming properly but the input cursor for my UITextView in coming down.How to get the blue input cursor from the top?
TextViewController.m
+(UITextField *)prepareUITextView
{
UITextView *uiTextView= [[UITextView alloc] initWithFrame:CGRectMake(
10, 80, 300, 100)];
uiTextView.layer.borderWidth = 1.0;
uiTextView.layer.cornerRadius = 8;
uiTextView.font = [UIFont systemFontOfSize:15];
uiTextView.autocorrectionType = UITextAutocorrectionTypeNo;
uiTextView.keyboardType = UIKeyboardTypeDefault;
uiTextView.returnKeyType = UIReturnKeyDone;
[uiTextView becomeFirstResponder];
uiTextView.selectedRange = NSMakeRange(0, 0);
return uiTextView;
}
ApplicationViewController.m
[self.view addSubview:[TextViewController prepareUITextView]];
Try this code
uiTextView.contentInset = UIEdgeInsetsMake(-7.0,0.0,0,0.0);
Adjust the Top value the way you want. this will solve your issue.
I've programmatically created two UITextFields in my iOS app, and set their text to the _minPrice and _minPrice variables, respectively.
The _minPrice and _maxPrice values appear correctly in the two fields, but tapping on them doesn't allow the user to edit them, they just remain those static values, backspacing doesn't work. Is there anything about my code thats preventing the text fields from being edited?
// Min Price
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(-25, -76, 70, 30)];
tf.textColor = [UIColor blackColor];
tf.font = [UIFont fontWithName:#"Helvetica-Neue" size:14];
tf.backgroundColor=[UIColor whiteColor];
tf.text= _minPrice;
tf.textAlignment = NSTextAlignmentCenter;
tf.layer.cornerRadius=8.0f;
tf.layer.masksToBounds=YES;
tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf.layer.borderWidth= 1.0f;
// Max Price
UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, -76, 70, 30)];
tf1.textColor = [UIColor blackColor];
tf1.font = [UIFont fontWithName:#"Helvetica-Neue" size:14];
tf1.backgroundColor=[UIColor whiteColor];
tf1.text= _maxPrice;
tf1.textAlignment = NSTextAlignmentCenter;
tf1.layer.cornerRadius=8.0f;
tf1.layer.masksToBounds=YES;
tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
tf1.layer.borderWidth= 1.0f;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 400, 400)];
[view addSubview:tf];
[view addSubview:tf1];
[self.view addSubview:view];
Your issue is clearly the frames you're setting...
Setting the color of the view you add the labels to to blue reveals your problem:
If you ensure that the labels are actually within the view you add them to (i.e. not negative), editing will be fine. All I did was change the negative x and y values in tf to positive, and they were editable:
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(25, 76, 70, 30)];
Try this! Maybe there is another view at the top of the textField
your_textfield.userInteractionEnabled = YES;
If this doesn't work
add another line
[self.view bringSubviewToFront: your_textfield];
Try to add delegate methods on your textfields. Like
- (void) textFieldDidEndEditing:(UITextField *)textField
{
// ADD BREAKPOINT HERE.
}
Check if it goes to that line of code. If not maybe there's a view on top of it. Or you can try to bring textfield to front like .
[self.view bringSubviewToFront:yourTextfield];
But this isn't a good example of how you fix the problem. Just to test if there is a view on top of it.
I am facing on problem, I want to have a static text in UITextField.
Text like "UserName:" should appear constantly to the left side of UITextField. Editing the UITextField should start from where the "UserName:" text ends.
For example, in iPhone settings, if we go to Twitter app and try to add new account. The way the user name and password textfield looks like. I want to develop same like that.
I don't understand why everyone has has posted such off the track answers to such a simple issue.
You simply do this:
Create a UILabel, with UI complimenting you textField.
You set it's text (Username:, etc).
Assign it to your textField's leftView property.
That's it. You will have to check frames, but that is basic, and I believe you can do that without much effort.
UPDATE: Sample code
UITextField* aField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];
aField.placeholder = #"Please enter a username";
aField.layer.borderWidth = 1.0f;
aField.backgroundColor = [UIColor whiteColor];
aField.layer.borderColor = [UIColor lightGrayColor].CGColor;
aField.layer.cornerRadius = 3.0f;
UILabel* aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 90, 38)];
aLabel.text = #"Username:";
aLabel.font = aField.font;
aLabel.textColor = [UIColor grayColor];
aField.leftView = aLabel;
aField.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:aField];
Only needs minor adjustments.
Try to use two text fields (one partially overlap with other) or Implement the below code.
-(void)viewDidAppear:(BOOL)animated{
yourTextField.text = #"UserName:";
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//handle back spaces and error conditions
NSString *editedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
if([editedText isEqualToString: #"UserName"])
{
return NO;
}
else
{
yourTextField.text = editedText;
return YES;
}
}
Below code may cater your need.
UIView *vis = [[UIView alloc]initWithFrame:CGRectMake(0, 200, 320, 50)];
UILabel *lblUserName = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 120, 40)];
UITextField *txtUserName =[[UITextField alloc]initWithFrame:CGRectMake(125, 5, 180, 40)];
lblUserName.text= #"User Name";
txtUserName.placeholder = #"#User Name";
[vis addSubview:lblUserName];
[vis addSubview:txtUserName];
[vis setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:vis];
Regards,
Amit
Please try this code. I am sure this will help you.
UILabel *lblLeft = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
lblLeft.backgroundColor = [UIColor clearColor];
lblLeft.text = #"Name: ";
lblLeft.textColor = [UIColor lightGrayColor];
lblLeft.font = [UIFont systemFontOfSize:14.0];
txtYourTextField.leftView = lblLeft;
Happy Coding :)
Deepak
For quick solution, do that with two overlapping textfields like this:
Then change the textfield border style to No border style and disable the user interaction of the below textfield:
How to give the style for the custom textview as password in xcode ios4.3
I think this is what you are looking for
UITextField *textFieldPassword = [[UITextField alloc] initWithFrame:CGRectMake(120, 90, 150, 30)];
textFieldPassword.borderStyle = UITextBorderStyleRoundedRect;
textFieldPassword.textColor = [UIColor blackColor];
textFieldPassword.font = [UIFont systemFontOfSize:12.0];
[textFieldPassword setSecureTextEntry:YES];
Suppose your UITextField is password than just do this
[password setSecureTextEntry:YES];
Hope it help you.