How do I prevent the editing of text in a UITextField
along with hiding the cursor/caret/magnifying glass, but still present the keyboard, as I am using an in
Subclass UITextField
//Disables caret
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
return CGRectZero;
}
//Disables magnifying glass
-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
{
gestureRecognizer.enabled = NO;
}
[super addGestureRecognizer:gestureRecognizer];
}
In your UITextFieldDelegate
//Prevent text from being copied and pasted or edited with bluetooth keyboard.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return NO;
}
Now just set your text programmatically from the result of your UIPickerView/UIDatePicker.
To disable any interaction with textfield except for making it a first responder you can just place a UIButton of the same size right over the textfield. Code for button tap event could be something like this:
- (IBAction)btnEditPhoneTapped:(id)sender
{
if (self.tfClientPhoneNo.isFirstResponder == NO) [self.tfClientPhoneNo becomeFirstResponder];
}
To have a UITextField
without interaction, but still work with an inputView
:
Subclass UITextField with these methods:
// Hide the cursor
- (CGRect)caretRectForPosition:(UITextPosition*)position
{
return CGRectZero;
}
// All touches inside will be ignored
// and intercepted by the superview
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return NO;
}
The last method will single-handedly prevent any editing and the magnifier glass, as you won't be able to tap the UITextField
.
This works great if you're using a textfield in a UITableViewCell
for example, and then can toggle firstResponder status through tableView:didSelectRowAtIndexPath:
.
I found the best solution was
- (CGRect) caretRectForPosition:(UITextPosition*) position
{
return CGRectZero;
}
- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return nil;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(copy:) || action == @selector(selectAll:) || action == @selector(paste:))
{
returnNO;
}
return [super canPerformAction:action withSender:sender];
}
http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/
I hope this will helpful for you.
Set cursor UIColor -> Empty. In UI, it will be hidden.
[[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"];
Hiding the cursor is a lot simpler in iOS 7. Still need some trickery to disable the loupe
textField.tintColor = [UIColor clearColor];